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
 Dates in multiple format in one column

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:11
11/23/02012 PM 04:42:12
1/13/2012 11:25:57 AM
1/1/2011 11:25:57 AM
21/1/2011 11:25:57 AM
31/11/2012 11:25:57 AM

How 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 @table1
select '2/4/2012' union all
select '24/11/2012'


select dtdate,case when dtdate like '_/_/%'
then '0'+left(dtdate,1)+'/'+'0'+right(left(dtdate,3),1)+right(dtdate,5)
else dtdate
end as newdate
from @table1

can it be converted in one date format ?

03/08/02012 PM 04:42:11
11/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]

Go to Top of Page

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_col
end
as datetime)
from table[/code]

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-13 : 04:35:03
this is the reason why we always say Use proper datatype for your columns

see

http://visakhm.blogspot.in/2011/12/why-iso-format-is-recommended-while.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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?
Go to Top of Page

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 format
but its giving arithmetic overflow error.

Seem the date is in many formats in column is cumbersome to solve problem.
Go to Top of Page
   

- Advertisement -