T-SQL - Convert TEXT to DATE

column DateOfBirth is varchar(08)
I need to convert that text to a date.

There are no leading 0's in that column, so Sept 20 1980 is stored as '9201980'

I need to convert that text to a proper date, or '09/20/1980'
declare @DateOfBirth varchar(08) = '9201980'
select CONVERT(varchar,CONVERT(datetime,@DateOfBirth ,  112),121)  -- This and several variances have failed

Open in new window

Thanks in advance.
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.

Brian CroweDatabase AdministratorCommented:
So how do you know if 1111980 is Nov. 1st 1980 or January 11th 1980?
Brian CroweDatabase AdministratorCommented:
Assuming the lack of leading zero is only for the month try this...

DECLARE @DOB	VARCHAR(8) = '9201980'

SELECT CONVERT(DATE, STUFF(STUFF(RIGHT('0' + @DOB, 8), 3, 0, '/'), 6, 0, '/'), 101)

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
Jason clarkDBA FreelancerCommented:
If your column is really text you need to convert to varchar before converting to datetime
select convert(datetime, convert(varchar(30), Remarks), 101)

Open in new window

Go this one it may be helpful to you.
Scott PletcherSenior DBACommented:
SELECT CAST(RIGHT(DateOfBirth, 4) + LEFT(RIGHT('0' + DateOfBirth, 8), 4) AS date) AS BirthDate
from (
    select '9201980' as DateOfBirth union
    select '1111980'
) as test_data
RickzzzAuthor Commented:
Thanks Brian, perfect.
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.