Link to home
Start Free TrialLog in
Avatar of Steve A
Steve AFlag for United States of America

asked on

Date not Formatting to YYYYMMDD

Hello,
I have a field that I want to convert to a format of YYYYMMDD and going with this scenario, it gives me a date of '04/18/19'

Declare @MemberDOB nvarchar(10)

Set @MemberDOB = '04/18/1960'

Select Convert(VarChar(8), @MemberDOB, 112)

I want it to be '19600418', but it doesn't.  (table uses nvarchar, and cannot be changed to another datatype initially)

Any Clues?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of lcohan
lcohan
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok your speciffic case would be:

Declare @MemberDOB nvarchar(10)
Set @MemberDOB = '04/18/1960'
SELECT cast(datepart(year, @MemberDOB) as sysname) + cast(datepart(month, @MemberDOB) as sysname) + cast(datepart(day, @MemberDOB) as sysname);
Avatar of Steve A

ASKER

Okay,
Thanks for the replies.

I tried this and seems to work:

Declare @MemberDOB nvarchar(10)

Set @MemberDOB = '04/18/1960'

Select Convert(VarChar(8), Cast(@MemberDOB As DateTime), 112)