Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Char To Date Format 1

Status
Not open for further replies.

sa0309

Programmer
Joined
Apr 5, 2010
Messages
45
Location
US
I have columns with the following date format:

5-FEB-09
5-MAY-10
6-JUL-09
6-OCT-08


I want to convert this to mmddyyy format.

how do i do this?

Thank you.
 
Convert first to date and then back to varchar with the correct style. Like this:

Code:
Select Convert(VarChar(10), Convert(DateTime, YourColumnName), 101)
From   YourTableName


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
I get an error: Syntax error converting datetime from character string.(22007,241)

Thanks
 
The code works for the sample data you show. You must have some data that does not convert properly. To find it...

Code:
Select *
From   YourTableName
Where  IsDate(YourColumnName) = 0

You can ignore invalid dates and return NULL like this...

Code:
Select Convert(VarChar(10), Convert(DateTime, Case When IsDate(YourColumnName) = 1 Then YourColumnName Else NULL End), 101)
From   YourTableName

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Yes...I did data not converting properly. Your 1st suggestion worked perfectly.

Thanks for the help.
 
This thread brings up a problem I have in that I want to output mmddyyyy not mm/dd/yyyy and end up using REPLACE as no style exists. Luckily I more often then not need Style 112 for output.

djj
The Lord is My Shepard (Psalm 23) - I need someone to lead me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top