Trim Left Characters In Sql Server?
I want to write a sql statement to trim a string 'Hello' from the string 'Hello World'. Please suggest.
Solution 1:
To remove the left-most word, you'll need to use either RIGHT or SUBSTRING. Assuming you know how many characters are involved, that would look either of the following:
SELECTRIGHT('Hello World', 5)
SELECTSUBSTRING('Hello World', 6, 100)
If you don't know how many characters that first word has, you'll need to find out using CHARINDEX, then substitute that value back into SUBSTRING:
SELECTSUBSTRING('Hello World', CHARINDEX(' ', 'Hello World') +1, 100)
This finds the position of the first space, then takes the remaining characters to the right.
Solution 2:
selectsubstring( field, 1, 5 ) from sometable
Solution 3:
For 'Hello' at the start of the string:
SELECT STUFF('Hello World', 1, 6, '')This will work for 'Hello' anywhere in the string:
SELECT REPLACE('Hello World', 'Hello ', '')Solution 4:
You can use LEN in combination with SUBSTRING:
SELECTSUBSTRING(myColumn, 7, LEN(myColumn)) from myTable
Solution 5:
use "LEFT"
selectleft('Hello World', 5)
or use "SUBSTRING"
selectsubstring('Hello World', 1, 5)
Post a Comment for "Trim Left Characters In Sql Server?"