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)
 Using substring to get the first part

Author  Topic 

icw
Constraint Violating Yak Guru

378 Posts

Posted - 2008-12-02 : 04:59:10
Hi I have a text field with a value like this
'Product A :(Peter perfect)'

I want to use substring to get the first part out (i.e. 'Product A')
But i am having a bit of a mental block.
I know it's something like this
Substring(field1,1,?????)
I tried using LEN but couldn't make it work.

Any help appreciated

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-12-02 : 05:00:06
CHARINDEX.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-02 : 05:03:32
[code]SELECT LEFT(field,CASE WHEN CHARINDEX(':',field)>0 THEN CHARINDEX(':',field)-1 ELSE LEN(field) END) FROM table[/code]
Go to Top of Page

icw
Constraint Violating Yak Guru

378 Posts

Posted - 2008-12-02 : 05:15:01
Much appreciated
I was also tring instr and instring before realising that they can't be used in MSSQL

thanks again
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-02 : 06:11:00
quote:
Originally posted by icw

Much appreciated
I was also tring instr and instring before realising that they can't be used in MSSQL

thanks again


They are VB functions.
Go to Top of Page
   

- Advertisement -