question

David 2 1 avatar image
David 2 1 asked

How Do I Convert A Datetime Datatype Into YYYYMM Char Format?

At the moment I have:

select convert(varchar(10),getdate(),101);

Which produces 11/19/2009, however I need it to read 200911 i.e. (YYYYMM)

TIA

sql-server-2000datetime
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

TG avatar image
TG answered

Here's one way:

select convert(char(6), getdate(), 112)            
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kristen avatar image
Kristen answered

My advice would be to format the date in your application, not in SQL Serer. If you format the date in SQL Server it will be sent as a text string to the application, rather than as a date/time datatype, and thus the applicaiton can do other useful things with it in its native format.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.