Simhadri
Simhadri

Reputation: 931

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.

Upvotes: 17

Views: 180771

Answers (6)

Aaditya Ardhapurkar
Aaditya Ardhapurkar

Reputation: 11

So you want to print only Hello right you can use this (works on command line client and workbench)

Select TRIM('World' from 'Hello World') as result;

Upvotes: 1

BradC
BradC

Reputation: 39916

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:

SELECT RIGHT('Hello World', 5)
SELECT SUBSTRING('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:

SELECT SUBSTRING('Hello World', CHARINDEX(' ', 'Hello World') + 1, 100)

This finds the position of the first space, then takes the remaining characters to the right.

Upvotes: 26

TKTS
TKTS

Reputation: 1261

You can use LEN in combination with SUBSTRING:

SELECT SUBSTRING(myColumn, 7, LEN(myColumn)) from myTable

Upvotes: 8

Joe Stefanelli
Joe Stefanelli

Reputation: 135799

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 ', '')

Upvotes: 9

Shane Castle
Shane Castle

Reputation: 1759

use "LEFT"

 select left('Hello World', 5)

or use "SUBSTRING"

 select substring('Hello World', 1, 5)

Upvotes: 2

Mark Wilkins
Mark Wilkins

Reputation: 41222

select substring( field, 1, 5 ) from sometable

Upvotes: 21

Related Questions