Solved

Convert varchar to Date as MM/DD/YYYY in SQL

Posted on 2011-09-07
15
367 Views
Last Modified: 2012-05-12
I have a table contain user input [EXP Date] as varchar:
1/11/2011
3/9/2011
3/9/11
I am trying to convert them to date:
Convert(datetime, [EXP Date],101), but I got the error message:
Conversion failed when converting datetime from character string.

This issue have bothered me a long time and i tired walk around it, but this time I have to figure out something before I can move on to do some dates caculation.

Does any one have idea of this?

Thank  you very much.

Fighting Miao
0
Comment
Question by:FinghtingMiao
  • 7
  • 4
  • 3
  • +1
15 Comments
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 25 total points
ID: 36498951
Got to 'SET DATEFORMAT' on this page: http://msdn.microsoft.com/en-us/library/ms186724.aspx  Default date format in all the SQL databases that I have seen is YYYY/MM/DD .  You have to do something to get it to accept something different.  Looks like 'SET DATEFORMAT' might do what you want.
0
 
LVL 25

Expert Comment

by:Ron M
ID: 36498952
Is it possible you have " " spaces or a Cr or Lf  character present in the field?

Your syntax is fine and should work.
0
 
LVL 25

Expert Comment

by:Ron M
ID: 36498971
SELECT CONVERT(Datetime,'1/11/2011',101)

Works....


SELECT CONVERT(Datetime,'1/11/2011
',101)

Doesn't work...
Server: Msg 241, Level 16, State 1, Line 1
Syntax error converting datetime from character string.

0
 

Author Comment

by:FinghtingMiao
ID: 36498979
xuserx2000:
Yes, I awared that ""spaces ...etc will cause the problem, so I filter them out before I do the converstion.
Thank you.
0
 
LVL 21

Accepted Solution

by:
JestersGrind earned 150 total points
ID: 36498992
The issue is that you have a mix of 2 digit and 4 digit years.  Try this.

SELECT CONVERT(datetime, Convert(datetime, [EXP Date]), 101)

Greg

0
 
LVL 25

Expert Comment

by:Ron M
ID: 36499012
Try this to test...

SELECT CONVERT(Datetime,Replace(Replace([EXP Date],CHAR(13),''),CHAR(10),''),101)
0
 
LVL 25

Expert Comment

by:Ron M
ID: 36499028
Spaces at beginning or end of the string actually shouldn't affect it, but carriage return or newline char definitely would.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 25

Assisted Solution

by:Ron M
Ron M earned 75 total points
ID: 36499051
JestersGrind may have it.


This works for me...

SELECT CONVERT(Datetime,cast('3/9/11' as datetime),101)


This doesn't...

SELECT CONVERT(Datetime,'3/9/11',101)
0
 

Author Comment

by:FinghtingMiao
ID: 36499054
JestersGrind:
yes, when i do
Convert (datetime, '1/1/2011', 101) works
but if i do
convert (datetime,'3/9/11',101) then does not work.
Let me try your solutiuon to see if it work.
 thanks.
0
 
LVL 25

Expert Comment

by:Ron M
ID: 36499061
SELECT CONVERT(Datetime,cast([EXP Date] as datetime),101)

0
 
LVL 21

Assisted Solution

by:JestersGrind
JestersGrind earned 150 total points
ID: 36499081
I can give you a more complete explanation.  The 101 in your query is the style.  Anything over 100 is specifically for 4 digit dates.  Anything under 100 is for 2 digit dates.  You could just remove the style component and it should work for both.  If there is a particular format that you would like it in, let us know and we can help.

Greg

0
 
LVL 21

Assisted Solution

by:JestersGrind
JestersGrind earned 150 total points
ID: 36499111
Actually you could update your table so that all of the dates are in a 4 digit year format.  Something like this.

UPDATE YourTable SET [EXP Date] = CONVERT(VARCHAR(50), Convert(datetime, [EXP Date]), 101)

Greg

0
 

Author Comment

by:FinghtingMiao
ID: 36499135
JestersGrind:
and
xuserx2000:

The solution works!  The reason is mix of 2 and 4 digits of year.
Thank you guys very much for that quick response!
0
 

Author Closing Comment

by:FinghtingMiao
ID: 36499167
You guys are very efficient! thanks. I learned a lot from you all.
0
 
LVL 25

Expert Comment

by:Ron M
ID: 36499169
Please give the point to Jester, it was his solution.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now