HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

sybase format datetime yyyymmdd

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
sybaseyyyymmddformatdatetime

Problem

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

Solution

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.)

Context

StackExchange Database Administrators Q#18120, answer score: 5

Revisions (0)

No revisions yet.