Solved

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

Posted on 2011-09-07
15
370 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 83

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 Malmstead
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 Malmstead
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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 Malmstead
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 Malmstead
ID: 36499028
Spaces at beginning or end of the string actually shouldn't affect it, but carriage return or newline char definitely would.
0
 
LVL 25

Assisted Solution

by:Ron Malmstead
Ron Malmstead 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 Malmstead
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 Malmstead
ID: 36499169
Please give the point to Jester, it was his solution.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Server 2012 management. 5 23
sql server insert 12 28
SQL Querying data from 3 tables, all with 1 common column 4 32
Help creating a spatial object in SQL Server 4 20
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

815 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

10 Experts available now in Live!

Get 1:1 Help Now