Solved

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

Posted on 2011-09-07
15
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

 Watch the Recording: Learning MySQL 5.7

MySQL 5.7 has a lot of new features. If you've dabbled with an older version of MySQL, it is definitely worth learning.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

617 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