Solved

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

Posted on 2011-09-07
15
373 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

752 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