convert date from 04/20/2014 to text or int 140420

I'm going a bit blank on trying to make the conversion. The other thing is that the output needs to be a string or int, not date format

thanks in advance.
damixaAsked:
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.

lcohanDatabase AnalystCommented:
SELECT cast(datepart(year, '04/20/2014') as sysname) + cast(datepart(month, '04/20/2014') as sysname) + cast(datepart(day, '04/20/2014') as sysname);
0
damixaAuthor Commented:
thanks for the answer. Is there a way to incorporate the leading zero as well?
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Copy-paste the below code into your SSMS, execute it to verify it meets your requirement, then motify
Declare @dt date = '2014-04-20'

SELECT RIGHT(CAST(YEAR(@dt) as varchar(4)),2)
   + RIGHT('0' + CAST(MONTH(@dt) as varchar(2)),2)
   + RIGHT('0' + CAST(DAY(@dt) as varchar(2)),2)

Open in new window

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
btw I executed the code in the first comment in this question in SQL 2012 SSMS and it returned 2014420, which is not correct.
0
Scott PletcherSenior DBACommented:
For char/varchar:
SELECT CONVERT(varchar(8), date_column, 112)

For int:
SELECT CAST(CONVERT(varchar(8), date_column, 112) AS int)
0
PortletPaulfreelancerCommented:
YYMMDD is style 12

convert(varchar, your_data_here ,12) -- varchar

convert(int,convert(varchar, your_data_here ,12)) -- now int

see:
http://www.experts-exchange.com/Database/MS-SQL-Server/A_12315-SQL-Server-Date-Styles-formats-using-CONVERT.html
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
damixaAuthor Commented:
Thanks
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 2008

From novice to tech pro — start learning today.