x

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

more ▼

asked Nov 19 '09 at 11:01 AM in Default

David 2 1 gravatar image

David 2 1
412 42 49 51

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

Here's one way:

select convert(char(6), getdate(), 112)
more ▼

answered Nov 19 '09 at 12:23 PM

TG gravatar image

TG
1.8k 1 3

(comments are locked)
10|1200 characters needed characters left

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.

more ▼

answered Nov 19 '09 at 02:39 PM

Kristen gravatar image

Kristen ♦
2.2k 6 7 10

Thats why I always start with asking "Where do you want to show data?"
Nov 20 '09 at 03:41 AM Madhivanan
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x472
x91

asked: Nov 19 '09 at 11:01 AM

Seen: 12004 times

Last Updated: Nov 19 '09 at 11:01 AM