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.
| Author |
Topic |
|
dainova
Starting Member
31 Posts |
Posted - 2009-07-30 : 12:17:18
|
| Hi, allin 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>.ThanksDai |
|
|
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) |
 |
|
|
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:55select [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 |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
dainova
Starting Member
31 Posts |
Posted - 2009-07-30 : 16:11:38
|
| Thanks, all guys |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2009-07-30 : 16:30:21
|
Try thisSELECT 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 ENDBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
|
|
|
|
|