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 |
|
mastertx
Starting Member
1 Post |
Posted - 2008-04-30 : 21:00:00
|
| I have a column named DATEID in a table DATA that contains thefollowing format connected to each other(date and name):20071030Jones20071020Smith20070918Rogers20080122Williamsetc..What would the syntax be to insert a column to the right of this one,and extract the first 8 digits from the data in the DATEID column andinsert that into the new column DATE, therefore making it easier forme to query against an actual date?Thanks |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2008-04-30 : 21:18:42
|
| [code]select Date = convert(datetime,left(a.DATEID,8))from ( -- Test Data select DATEID = '20071030Jones' union all select DATEID = '20071020Smith' union all select DATEID = '20070918Rogers' union all select DATEID = '20080122Williams' ) aReply:Date---------------------------2007-10-30 00:00:00.0002007-10-20 00:00:00.0002007-09-18 00:00:00.0002008-01-22 00:00:00.000(4 row(s) affected)[/code]CODO ERGO SUM |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-05-02 : 05:42:41
|
quote: Originally posted by Michael Valentine Jones
select Date = convert(datetime,left(a.DATEID,8))from ( -- Test Data select DATEID = '20071030Jones' union all select DATEID = '20071020Smith' union all select DATEID = '20070918Rogers' union all select DATEID = '20080122Williams' ) aReply:Date---------------------------2007-10-30 00:00:00.0002007-10-20 00:00:00.0002007-09-18 00:00:00.0002008-01-22 00:00:00.000(4 row(s) affected) CODO ERGO SUM
To handle illegal dates, you may need to addwhere len(a.DATEID)=8 and isdate(a.DATEID)=1MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|