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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Trim to right of character

Author  Topic 

offspring22
Starting Member

38 Posts

Posted - 2010-08-17 : 13:02:45
So I have a column named class, with data such as:

DC (pre 1P2007)
CC-MH
C-COR1 f3.0h23|DC (pre 1P2007)
R-1|M-1|S-UN|S-R|M-X1|R-2M|R-1s|R-1N|S-SPR|M-2 (pr

etc.

What I need to do is select everything to the left of the first | in each row. So from the above, i'd be left with:

DC (pre 1P2007)
CC-MH
C-COR1 f3.0h23
R-1

Any tips on how to do so?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-17 : 13:08:07
SELECT LEFT(field,CASE WHEN CHARINDEX('|',field)> 0 THEN CHARINDEX('|',field)-1 ELSE LEN(field) END)

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -