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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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

Vitor MontalvãoMSSQL Senior EngineerCommented:
A date without year? You should at least provide the year so the conversion can be made.
ManjuIT - Project ManagerAuthor Commented:
Apr-12 is 1st April 2012... and so on
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

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

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

Open in new window

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.
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.