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
 insert value into datetime

Author  Topic 

dainova
Starting Member

31 Posts

Posted - 2009-07-30 : 12:17:18
Hi, all
in input I have date-time string as <20090728103455> (i.e 7/28/09 10:34:55). How I can format it before doing insert into db table with column defined as <datetime>.

I assume that native format for datetime is <2007-10-28 22:11:19.7030000>.



Thanks
Dai

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2009-07-30 : 12:30:52
SELECT CAST(STUFF(STUFF(STUFF('20090728103455', 9, 0, ' '), 12, 0, ':'), 15, 0, ':') AS DATETIME)
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2009-07-30 : 12:37:49
Datetime does not have a format.

You just have to convert the string into something that SQL Server can figure out how to convert to datetime, like the following example.
Example: 20090728 10:34:55

select	[Date] =
convert(datetime,
stuff(stuff(stuff(
'20090728103455'
,13,0,':')
,11,0,':')
,9,0,' ')
)

Results:
Date
------------------------
2009-07-28 10:34:55.000

(1 row(s) affected)


CODO ERGO SUM
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-07-30 : 12:41:15
hmmmmmmmmmmmmmmmmmm

Looks familiar



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

dainova
Starting Member

31 Posts

Posted - 2009-07-30 : 16:11:38
Thanks, all guys
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-07-30 : 16:30:21
Try this


SELECT
CASE
WHEN ISDATE(CAST(STUFF(STUFF(STUFF('20090728103455', 9, 0, ' '), 12, 0, ':'), 15, 0, ':') AS DATETIME))= 1
THEN CAST(STUFF(STUFF(STUFF('20090728103455', 9, 0, ' '), 12, 0, ':'), 15, 0, ':') AS DATETIME)
ELSE 0
END





Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -