Date not Formatting to YYYYMMDD

Machinegunner
Machinegunner used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Database Analyst
Commented:
Something like this?


  SELECT cast(datepart(year, GETDATE()) as sysname) + cast(datepart(month, getdate()) as sysname) + cast(datepart(day, getdate()) as sysname);
lcohanDatabase Analyst

Commented:
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);

Author

Commented:
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)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial