Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have date column which is varchar(50) and it contains mixed dates like dd/mm/yyyy and mm/dd/yyyy now i want to convert the date column values to smalldatetime
but it is giving error
Conversion failed when converting date and/or time from character string. for mm/dd/yyyy 
please give some suggestion so that i can convert this mixed data in smalldatetime  format
Thank you in advance.


What I have tried:

SELECT Convert(date,'01/14/2017',103)


declare  @tbl table
(
date varchar(20)
)


insert into @tbl
select '14/03/2017'

insert into @tbl
select '01/14/2017'


select convert(smalldatetime ,convert(varchar(10),date,103), 103) from @tbl

select convert(smalldatetime,convert(varchar(10),date,112),103) from @tbl
Posted
Updated 6-Jun-20 18:53pm
Comments
CHill60 16-May-17 7:51am    
You can't. Is "03/11/2016" - 11th March or 3rd November?
Paw Jershauge 16-May-17 7:53am    
How would you ever know what style to convert from ???

Assuming this table is under your control and you are trying to do some homework or similar ... NEVER store dates in text-type columns (char, nchar, varchar, nvarchar or text). ALWAYS store dates in columns of types DATE or DATETIME. Leave any formatting of the date data to the UI layer rather than when retrieving from the database.

If you are stuck with this data then you have problems determining which format to use ... it will be fine for any dates after the 12th of any month because the format DD/MM/YYYY or MM/DD/YYYY will be obvious (there is no 13th month so wherever that >12 value appears must be the DAY of the date. You might be able to process the bulk of that data using a WHILE loop, leaving the ambiguous data to be handled manually
 
Share this answer
 
Its is not something i would recomment, but this should work for the two posible combinations you specified
SQL
declare  @tbl table
(
	[date] varchar(20)
)
 

insert into @tbl
select '14/03/2017'
UNION ALL
select '01/14/2017'
 

SELECT CASE WHEN TRY_PARSE([date] AS smalldatetime) IS NULL THEN TRY_PARSE((SUBSTRING([date],4,2) + '/' + SUBSTRING([date],1,2) + '/' +SUBSTRING([date],7,4)) AS smalldatetime) ELSE TRY_PARSE([date] AS smalldatetime) END [convertedtype] FROM @tbl


Result:
convertedtype
2017-03-14 00:00:00
2017-01-14 00:00:00
 
Share this answer
 
Comments
CHill60 16-May-17 8:05am    
As per the comments in my solution it will potentially misinterpret dates - try adding
UNION ALL select '03/11/2017'
to the table populate - it comes out as 11th March, but the date is 3rd November here.
Paw Jershauge 16-May-17 8:38am    
@CHill60, i believe i said that i would not recommend it; but if the first cast fails i would try something else, i'm NOT saying that this is correct...
CHill60 17-May-17 17:26pm    
If you use the  Reply  button next to a comment then the poster is notified that you are responding. I found this response entirely by chance.
The point I am making is that there is NO programmatic way to determine which is the correct date - the OP can capture cast failures and deal with them, but the example I gave will not fail a CAST ... but could still be very very wrong
hi my solution is

SELECT Convert(datetime,'2017/05/05',103)

2017-05-05 00:00:00.000


SELECT Convert(date,cast('2017/05/05' as date),103)

2017-05-05

SELECT Convert(datetime,'05/05/2017',103)

2017-05-05 00:00:00.000


SELECT Convert(date,cast('05/05/2017' as date),103)

2017-05-05


SELECT Convert(smalldatetime,'2017/05/05',103)

2017-05-05 00:00:00.000


SELECT Convert(smalldatetime,cast('2017/05/05' as date),103)

2017-05-05 00:00:00.000
 
Share this answer
 
v2
Comments
CHill60 17-May-17 17:23pm    
Works fine for that date doesn't it. Now try '03/11/2017' ... is that 11th March or 3rd November? Are you sure?
vinuvasahanponniah 17-May-17 23:45pm    
no.wait
No... this is all wrong. If you have a column containing a mix of mm/dd/yyyy and dd/mm/yyyy, there is absolutely no way that you will be able to discern if something like 7/6/2000 is the 7th of June or the 6th of July just from the information in that column. You MUST talk with whomever provided you with the data a get them to settle on a format for dates. It would be best if you used the ISO format of YYYYMMDD. DON'T settle for YYYY-MM-DD because if it's for the French language, it comes up as YYYY-DD-MM.
 
Share this answer
 
Comments
CHill60 8-Jun-20 9:56am    
I pointed out the problem with certain dates 3 years ago.
As to the "date format", I disagree entirely. ISO 8601 allows for the '-' separator, in fact if only year and month are being used to indicate a date then the '-' must be used. I've handled dates for many cultural environments and have never come across YYY-MM-DD being misinterpreted for any language. Happy to stand corrected if you can provide the evidence.
However, dates should be stored as Dates, not strings, then there is no concept of "format".
Jeff Moden 8-Jun-20 14:47pm    
Yes... ISO 8601 allows for dashes as delimiters. And, no... the dashes do not need to be included in the YYYYMM format although I certainly think they make things much more readable and DO, in fact, identify that it's formatted correctly.

But... ISO 8601 says that dashes are an alternate format from the YYYYMMDD format, which is the primary format. "alternate" formats are frequently bastardized and, in SQL Server, if the language is French, the format using dashes is YYYY-DD-MM.

The real key here is that my suggestion of using YYYYMMDD is actually secondary and the OP must have folks settle on a standard that is completely unambiguous. Unfortunately, in SQL Server, YYYY-xx-xx is NOT completely unambiguous.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900