Convert Varchar to Date

Experts -

How can i convert "Aug-12" varchar column to a datetime format?

Ex: "Aug-12" should be "mm/dd/yyyy" format after conversion.

Kindly help.
LVL 7
ManjuIT - Project ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
This is not possible, but when you remove the hyphen and add a year you can convert it:

DECLARE @var VARCHAR(255) = 'Aug-12';

SELECT  @var ,
        CONVERT(DATE, REPLACE(@var, '-', ' ') + ', 2015', 107);

Open in new window


btw, don't mix data types with their possible textual representations. So the above converts your value to a DATE, you may also use DATETIME. To get a specific format, you need to convert CONVET() it or use FORMAT():

DECLARE @var VARCHAR(255) = 'Aug-12';

SELECT  @var ,
        CONVERT(DATE, REPLACE(@var, '-', ' ') + ', 2015', 107);

DECLARE @date DATE = CONVERT(DATE, REPLACE(@var, '-', ' ') + ', 2015', 107);

SELECT  @date ,
        CONVERT(VARCHAR(255), @date, 101) ,
        FORMAT(@date, 'd', 'en-US');

Open in new window

0
Vitor MontalvãoMSSQL Senior EngineerCommented:
A date without year? You should at least provide the year so the conversion can be made.
0
ManjuIT - Project ManagerAuthor Commented:
Apr-12 is 1st April 2012... and so on
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Vitor MontalvãoMSSQL Senior EngineerCommented:
What's so on? How do you provide 2nd of April?
0
ste5anSenior DeveloperCommented:
HHCIB?

DECLARE @var VARCHAR(255) = 'Aug-12';

SELECT  @var ,
        CONVERT(DATE, '1 ' + REPLACE(@var, '-', ' ') , 107);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ManjuIT - Project ManagerAuthor Commented:
Victor - My Bad, the column has only the month and year in the format i gave. I dont need the date's as it wouldnt be necessary for me to run the query. Having said that, Stephan's query works like a charm.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.