w3resource

AdventureWorks Database: Remove spaces from the beginning of a string

SQL Query - AdventureWorks: Exercise-66 with Solution

66. Write a query in SQL to remove the spaces from the beginning of a string.

Sample Solution:

-- Selecting a string containing five leading spaces followed by the text 'five space then the text', and aliasing it as "Original Text"
SELECT  '     five space then the text' as "Original Text",

-- Removing leading spaces from the string '     five space then the text' using the LTRIM() function, and aliasing the result as "Trimmed Text(space removed)"
LTRIM('     five space then the text') as "Trimmed Text(space removed)";

Explanation:

  • The SQL query selects a string containing five leading spaces followed by the text 'five space then the text', and aliases it as "Original Text".
  • The LTRIM() function is used to remove leading spaces from the string ' five space then the text'.
  • The result of the LTRIM() function, which is the string 'five space then the text' without leading spaces, is aliased as "Trimmed Text(space removed)".

Sample Output:

Original Text                |Trimmed Text(space removed)|
-----------------------------+---------------------------+
     five space then the text|five space then the text   |

SQL AdventureWorks Editor:

Practice Online


Contribute your code and comments through Disqus.

Previous: Find products between $1000 and $1220 in lower, upper, and lowerupper.
Next: Remove the substring 'HN' from the start of the productnumber.

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://w3resource.com/sql-exercises/adventureworks/sql-adventureworks-exercise-66.php