dbTalk Databases Forums  

set datetime from 2 strings

microsoft.public.sqlserver.programming microsoft.public.sqlserver.programming


Discuss set datetime from 2 strings in the microsoft.public.sqlserver.programming forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Sean
 
Posts: n/a

Default set datetime from 2 strings - 12-22-2004 , 09:43 AM






I have 2 varchar fields, OrigTrxDate and OrigTrxTime. The value is 031027 and
103520. I want to get a combined datetime field with the value 2003-10-27
10:35:20.000 if they are both valid value. How can I do in SQL?

Thanks.

Reply With Quote
  #2  
Old   
Alejandro Mesa
 
Posts: n/a

Default RE: set datetime from 2 strings - 12-22-2004 , 09:57 AM






With all the problems aroused because we were saving year value using two
digits and still we continue doing the same.

Try:

use northwind
go

declare @d char(8)
declare @t char(6)

set @d = '20' + '031027'
set @t = '103520'

select cast(convert(char(10), cast(@d as datetime), 126) + 'T' + left(@t, 2)
+ ':' + substring(@t, 3, 2) + ':' + right(@t, 2) as datetime)



AMB

"Sean" wrote:

Quote:
I have 2 varchar fields, OrigTrxDate and OrigTrxTime. The value is 031027 and
103520. I want to get a combined datetime field with the value 2003-10-27
10:35:20.000 if they are both valid value. How can I do in SQL?

Thanks.

Reply With Quote
  #3  
Old   
Itzik Ben-Gan
 
Posts: n/a

Default Re: set datetime from 2 strings - 12-22-2004 , 10:00 AM



Sean, here's how you can do this:

SELECT
CASE WHEN ISDATE(dtstr) = 1 THEN CAST(dtstr AS DATETIME) END AS dt,
other_columns
FROM (SELECT OrigTrxDate + ' '
+ SUBSTRING(OrigTrxTime, 1, 2) + ':'
+ SUBSTRING(OrigTrxTime, 3, 2) + ':'
+ SUBSTRING(OrigTrxTime, 5, 2) AS dtstr,
other_columns
FROM YourTable) AS D

--
BG, SQL Server MVP
www.SolidQualityLearning.com


"Sean" <Sean (AT) discussions (DOT) microsoft.com> wrote

Quote:
I have 2 varchar fields, OrigTrxDate and OrigTrxTime. The value is 031027
and
103520. I want to get a combined datetime field with the value 2003-10-27
10:35:20.000 if they are both valid value. How can I do in SQL?

Thanks.



Reply With Quote
  #4  
Old   
John Bell
 
Posts: n/a

Default Re: set datetime from 2 strings - 12-22-2004 , 10:23 AM



Hi

It is not clear what values you have but the following may help:

SELECT CAST(OrigTrxDate + ' ' + STUFF( STUFF( OrigTrxTime,5,0,':'), 3,0,':')
AS datetime )
FROM MyTable

John

"Sean" <Sean (AT) discussions (DOT) microsoft.com> wrote

Quote:
I have 2 varchar fields, OrigTrxDate and OrigTrxTime. The value is 031027
and
103520. I want to get a combined datetime field with the value 2003-10-27
10:35:20.000 if they are both valid value. How can I do in SQL?

Thanks.



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.