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
 Select data after string

Author  Topic 

daniel50096230
Yak Posting Veteran

99 Posts

Posted - 2009-07-30 : 00:20:36
I have the following data in my [content] column:

STORE NO : 908 FFDC SIMPANG PULAI (908) TEL : 03-42518482 FAX:03-42604102 FROM DATE : 01/01/09 TO DATE : 31/01/09

How should I write my sql statement so that I can only get the date after "FROM DATE: " and another data after "TO DATE: "???

GhantaBro
Posting Yak Master

215 Posts

Posted - 2009-07-30 : 00:27:34
Use patindex and substring functions.
Go to Top of Page

daniel50096230
Yak Posting Veteran

99 Posts

Posted - 2009-07-30 : 00:48:06
Is there any sample for me to review because i am not really understand of patindex
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-30 : 00:54:28
[code]
declare @str varchar(150)

select @str = 'STORE NO : 908 FFDC SIMPANG PULAI (908) TEL : 03-42518482 FAX:03-42604102 FROM DATE : 01/01/09 TO DATE : 31/01/09'

select FromDate = substring(@str, charindex('FROM DATE : ', @str) + 12, 8),
ToDate = substring(@str, charindex('TO DATE : ', @str) + 10, 8)
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

subhaoviya
Posting Yak Master

135 Posts

Posted - 2009-07-30 : 01:49:01
as per your query i got , 908 FFDC as result for from date, why?

why u add chaindex +12?

Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-07-30 : 02:07:55
it is length of FROM DATE : is 12 so that khtan added 12
in todate added 10 length of TO DATE : is 10

substring (columnname, startloction,length)
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-30 : 02:08:37
quote:
Originally posted by subhaoviya

as per your query i got , 908 FFDC as result for from date, why?

why u add chaindex +12?





use case when to check for result of CHARINDEX() when it can't find the string, it will return 0.

+ 12 is to skip the string "FROM DATE : "


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -