w3resource

AdventureWorks Database: Remove spaces at the end of a string

SQL Query - AdventureWorks: Exercise-71 with Solution

71. Write a query in SQL to remove the spaces at the end of a string.

Sample Solution:

-- Concatenating the string 'text then five spaces     ' with 'after space' and aliasing it as "Original Text"
SELECT CONCAT('text then five spaces     ','after space') as "Original Text",

-- Concatenating the trimmed version of the string 'text then five spaces     ' with 'after space' and aliasing it as "Trimmed Text(space removed)"
CONCAT(RTRIM('text then five spaces     '),'after space') as "Trimmed Text(space removed)";

Explanation:

  • The SQL query concatenates two strings together and provides two variations of the concatenation.
  • In the first concatenation:
    • The CONCAT() function concatenates the string 'text then five spaces ' with 'after space'.
    • The result is aliased as "Original Text".
  • In the second concatenation:
    • The RTRIM() function trims any trailing spaces from the string 'text then five spaces '.
    • The CONCAT() function then concatenates the trimmed string with 'after space'.
    • The result is aliased as "Trimmed Text(space removed)".

Sample Output:

Original Text                        |Trimmed Text(space removed)     |
-------------------------------------+--------------------------------+
text then five spaces     after space|text then five spacesafter space|

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: Return rightmost eight characters from product name.
Next: Fetch the product name ends with the letter 'S' or 'M' or 'L'.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/sql-exercises/adventureworks/sql-adventureworks-exercise-71.php