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 |
Vishal_sql
Posting Yak Master
102 Posts |
Posted - 2013-02-13 : 00:29:22
|
Hi All,I have a table which has column of datatype varchar.it contains dates in different format as below.03/08/02012 PM 04:42:1111/23/02012 PM 04:42:121/13/2012 11:25:57 AM1/1/2011 11:25:57 AM21/1/2011 11:25:57 AM31/11/2012 11:25:57 AMHow can it be converted in one format using T-SQL ?Any suggestions would be highly appreciated.I tried it with Left function but it is for just only one format. column has different date formats in varchar.e.g.declare @table1 table( dtdate varchar(100) )insert @table1select '2/4/2012' union allselect '24/11/2012'select dtdate,case when dtdate like '_/_/%'then '0'+left(dtdate,1)+'/'+'0'+right(left(dtdate,3),1)+right(dtdate,5) else dtdateend as newdatefrom @table1can it be converted in one date format ?03/08/02012 PM 04:42:1111/23/02012 PM 04:42:12 here year 02012 are not mistakes but are values in column. |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2013-02-13 : 02:03:50
|
quote: How can it be converted in one format using T-SQL ?
You can use CAST() or CONVERT() to do it. And you should convert it to date or datetime data type.However you will need to decide 01/02/2013 is Jan 2 or Feb 1 and handle it accordingly in your conversion logic KH[spoiler]Time is always against us[/spoiler] |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2013-02-13 : 04:18:07
|
[code]select cast(case when date_col like '%PM%' then replace(date_col,'PM','')+'PM' when date_col like '%AM%' then replace(date_col,'AM','')+'AM' else date_colendas datetime)from table[/code]MadhivananFailing to plan is Planning to fail |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-02-13 : 09:30:32
|
Following up on what khtan pointed out about Jan 2 vs Feb 2nd, I would think that this is an impossible situation. Assume that the dates have a uniform distribution over the day of the year. That means about 40 percent of the dates are ambiguous. Everything up to and including the 12th day of each month (with the exception of those days that happen to be have the same date as the month number such as 5/5/2012, 6/6/2012 etc.) are ambigous.So unless you have some other column that will help you decipher whether a given date is Jan 2 or Feb 1, not only can you resolve this programmatically, even manual intervention wouldn't help.Is there any possibility that you can get the data from the source in a deterministic format? |
|
|
Vishal_sql
Posting Yak Master
102 Posts |
Posted - 2013-02-13 : 09:44:28
|
Hi All,Thanks for the reply.I beleive by replacing '/' in the column the data could be converted in int and then compared with Left(intdate,4) > 1231 or < 3112.and then again converted back to date formatbut its giving arithmetic overflow error.Seem the date is in many formats in column is cumbersome to solve problem. |
|
|
|
|
|
|
|