954,560 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Extracting a substring from varchar field

Hi,
I have a table field with var char data type having values like TVM/345/2000,TVM/01/00,TSR/42/01 etc.I want to extract the sub string from first location to the second '/' location as TVM/345/.
Is there any built in function to solve is?

Thanks

divyakrishnan
Posting Whiz in Training
201 posts since May 2010
Reputation Points: 30
Solved Threads: 24
 

SYNTAX

select substring(COLNAME, 1, CHARINDEX('/',COLNAME, CHARINDEX('/',COLNAME)+1)) FROM TABLENAME

EXAMPLE

select substring('TVM/345/2000,TVM/01/00,TSR/42/01', 1,CHARINDEX('/','TVM/345/2000,TVM/01/00,TSR/42/01',CHARINDEX('/','TVM/345/2000,TVM/01/00,TSR/42/01')+1))
urtrivedi
Nearly a Posting Virtuoso
1,306 posts since Dec 2008
Reputation Points: 257
Solved Threads: 270
 

Thank You Very much..
There is one more thing I want to do.
Can I update the fields like TVM/01/00 as TVM/01/2000?

divyakrishnan
Posting Whiz in Training
201 posts since May 2010
Reputation Points: 30
Solved Threads: 24
 

before running this query keep backup of that column. or do it in dummy database

update tablename set colname=replace(colname,'TVM/01/00','TVM/01/2000')
urtrivedi
Nearly a Posting Virtuoso
1,306 posts since Dec 2008
Reputation Points: 257
Solved Threads: 270
 

It is not like that.The field is a dynamic field..I got the solution..Thanks for your support...

divyakrishnan
Posting Whiz in Training
201 posts since May 2010
Reputation Points: 30
Solved Threads: 24
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You