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 2008 Forums
 SSIS and Import/Export (2008)
 get string between certain characters //

Author  Topic 

maihabib
Starting Member

22 Posts

Posted - 2014-12-15 : 11:05:24
i'm loading data from excel to sql and i have the date in a cell like this '10/2/14 to 10/2/14'.
how do i get the date only and insert to datetime column in sql table.
i tried to substring but couldnt figure out how to get the day from between the / /

help plz.
thanks,

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-12-15 : 17:22:41
one way:


declare @str varchar(500) = '10/2/14 to 10/2/14';

select left(v.s, _1st.blank-1) as first_date
, substring(v.s, _2nd.blank+1, len(v.s)) as second_date
from (values (@str)) v(s)
cross apply (select CHARINDEX(' ', v.s)) _1st(blank)
cross apply (select charindex(' ', v.s, _1st.blank+1)) _2nd(blank)
Go to Top of Page
   

- Advertisement -