SQL - convert string mm/dd/yyyy to yyyymmdd - SQL Server 2005

Hello experts,

I have a table with a date_of_birth column in varchar(10) format.  I have a need to convert the string into a similar string format shown  below:

Currently I have:

table: person

person_id         date_of_birth
123                   3/9/1963
124                   11/22/1935
125                   5/6/1943
126                   11/22/1935
127                   10/4/1955

I would like to end up with:

person_id         date_of_birth
123                   19630309                       --3/9/1963
124                   19351122                      --11/22/1935
125                   19430506                       --5/6/1943
126                   19351122                       --11/22/1935
127                   19551004                       --10/4/1955

When i try and do something like
update person
set date_of_birth = RIGHT(date_of_birth,4)+LEFT(date_of_birth,2)+SUBSTRING(date_of_birth,4,2)

it gives me trouble because of cases where there are single digit months or days like 5/22/2011where the month is a single digit, or 11/5/2004 where the day is a single digit.


Who is Participating?

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

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.

You should run a cursor through all items in the table.  Set the month, day, year to variables and then test the length. From that you can padd a leading 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
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You need to convert the string into a date (providing the correct format specifier), and than back to a varchar using another format specifier. CONVERT does that:

update person
set date_of_birth = convert(varchar(10), convert(datetime, date_of_birth, 101), 112)
where date_of_birth like '%/%';

See the Books Online, CAST und CONVERT for details.
Hi ...  try this one.

SET date_of_birth = Convert(VarChar,Cast(date_of_birth as DateTime),112)
You may try this please:
update person
set date_of_birth = right(date_of_birth,4)+replicate('0',len(substring(date_of_birth,1,charindex('/',date_of_birth)-1)) % 2)+substring(date_of_birth,1,charindex('/',date_of_birth)-1)+replicate('0',len(substring(date_of_birth,charindex('/',date_of_birth)+1,charindex('/',date_of_birth,4)-1)) % 2)+substring(date_of_birth,charindex('/',date_of_birth)+1,charindex('/',date_of_birth,4)-1)
robthomas09Author Commented:
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
Query Syntax

From novice to tech pro — start learning today.