Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 LTRIM

Author  Topic 

reading2009
Starting Member

22 Posts

Posted - 2010-10-11 : 20:44:55
Hi I want to remove all charecters before \.
Example, in a column I have abc\smith, abcfgrt\john, retet\lauren,
er\peter.

All the charecters before \ and including \ should be removed.
So the result should be, smith, john, lauren, peter only.

How would i come up with one LTRIM that will suit all entries?

Any help here pleasE?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-11 : 20:47:56
declare @s varchar(50)

set @s = 'abc\smith'

select substring(@s, charindex('\', @s) + 1, datalength(@s))

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-11 : 20:48:37
You can't use LTRIM function as LTRIM specifically deals with leading spaces.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

reading2009
Starting Member

22 Posts

Posted - 2010-10-11 : 20:50:05
here you are setting the value of @s = 'abc\smith'.
But the column i have different entries in different left before \.

If not LTRIM, if there any other way to achive the output in a single query that can be applied to these data?


quote:
Originally posted by tkizer

declare @s varchar(50)

set @s = 'abc\smith'

select substring(@s, charindex('\', @s) + 1, datalength(@s))

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-11 : 20:55:25
Did you even try my solution? It'll work for all of your examples. @s is just an example. Switch it to your column and your table.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-11 : 20:56:03
select substring(YourColumn, charindex('\', YourColumn) + 1, datalength(YourColumn))
from YourTable
...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

reading2009
Starting Member

22 Posts

Posted - 2010-10-11 : 21:10:02
The substring worked just fine. Thank you Tara. I very much appriciate your timely help.

quote:
Originally posted by tkizer

select substring(YourColumn, charindex('\', YourColumn) + 1, datalength(YourColumn))
from YourTable
...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

Go to Top of Page
   

- Advertisement -