Sybase format datetime yyyymmdd

date formatsybase

This seems like something trivial would be easy, but I couldn't find a good way to do it.

This is what I came up with:

select convert(numeric,
                    (select convert(varchar,datepart(yy,getdate())) + 
                         right('0' + convert(varchar,datepart(mm,getdate())), 2) +
                         right('0' + convert(varchar,datepart(dd,getdate())), 2)
                    ))

EDIT: I know it doesn't need to have a subquery like that, but ignore that =p

Best Answer

Much simpler: CONVERT(varchar(8),GETDATE(),112)

You can find a full list of CONVERT styles in Sybase BOL, although the explanation of each style isn't particularly clear (and is actually flat wrong for style 12 & 112). The Complete Sybase ASE Reference Guide includes a full list of styles with examples. (Please note that the complete guide is externally published, not from Sybase. Also I'm not affiliated with the book or the publisher, just mentioning it because everyone working with ASE should have a copy of it.)