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 |
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2010-02-03 : 06:55:52
|
| Hi GuysI'm having a bit of a problem converting integer values to datetime values.I have a list of integers that resemble the following in a table:200601200602200603200604...I am trying to convert these values to a datetime format with the following code, but it doesn't seem to bringing back the correct value.SELECTMonthId,CONVERT(DATETIME,CONVERT(CHAR(10),MonthId))FROM TableReturns:200601 - 2020-06-01 00:00:00.000200602 - 2020-06-01 00:00:00.000200603 - 2020-06-01 00:00:00.000200604 - 2020-06-01 00:00:00.000Anyone have any ideas on what I can do?Thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-03 : 06:59:57
|
| [code]select dateadd(mm,(yourcol%100)-1,dateadd(yy,(yourcol/100)-1900,0)) from table[/code] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-02-03 : 07:20:42
|
| orselect cast(cast(your_col*100+1 as char(8)) as datetime) from your_tableMadhivananFailing to plan is Planning to fail |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2010-02-03 : 07:42:05
|
Cool solution Madhi..very simple - LumbagoIf the facts don't fit the theory, change the facts. Albert Einstein |
 |
|
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2010-02-03 : 07:48:38
|
| Hey I also found using CONVERT(DATETIME,CONVERT(VARCHAR(10),MonthLastYear_Id) + '01')does the trick. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-02-03 : 08:56:10
|
quote: Originally posted by Lumbago Cool solution Madhi..very simple - LumbagoIf the facts don't fit the theory, change the facts. Albert Einstein
Thanks MadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-02-03 : 08:56:58
|
quote: Originally posted by rcr69er Hey I also found using CONVERT(DATETIME,CONVERT(VARCHAR(10),MonthLastYear_Id) + '01')does the trick.
It is basically equivalent to my solutionBut mine has less code MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|