I would say you could use convert(datetime,left(yourval,4)+'-'+substring(yourval,5,2)+'-' etc. ,20) to force your varchar to look like a convertable string and then convert it, but just out of curiosity, how are you getting this varchar data? JHall
There are many ways to write such a function. I present two.
declare @strdate varchar(20)
set @strdate='20020304113200'
-- Function 1
Select DateTime1=
convert(datetime,left(@strdate,8),112) +
convert(datetime,stuff(stuff(right(@strdate,6),5,0,':'),3,0,':'),108)
-- Function 2
set @strdate=stuff(stuff(@strdate,13,0,':'),11,0,':')
set @strdate=stuff(stuff(stuff(@strdate,9,0,' '),7,0,'/'),5,0,'/')
select DateTime2=convert(datetime,@strdate)
I've created a page explaining "Date Handling in SQL Server." I willl post it as a FAQ in this forum when the FAQ submision process is fixed. In the meantime you canview it at...
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions in the SQL Server forum. Many of the ideas apply to all forums.
Thanks a lot for your response, its was very useful.
What do i have to make to convert the datetime back to varchar in the same format YYYYmmDDhhMMss.
jhall156 - This data is the rawdata generated by GSM Switch (its the mainframe on a cellular telephony system). Is grab in a binary file and bulk inserted in the database.
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions in the SQL Server forum. Many of the ideas apply to all forums.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.