views:

406

answers:

3

Specifically, I wish to get the date format in a pure (ISO) format:

YYYY-MM-DD HH:mm:ss

I'm looking for a SET command or something that I can use.

I do not wish to rely on the culture setting of the server.

Note: I'm interested in the string format in which dates are returned, not entered.

A: 

the formatting of the datetime depends on the client. You can use the convert function to display it as a string in the format of your choice.

Learning
+5  A: 

To change the default format you need to add a new language (sp_addlanguage), set it's date format, then set the default language to it. More details can be found on this old technet article.

If you don't want to do that, then you can change it "per connection" using SET DATEFORMAT.

And if you don't want to do that, then you can use CONVERT to convert it to the relevent format string in each query.

Steven Robbins
SET DATEFORMAT isn't much help as it's only for inputting dates. The same things seems to be true about the language setting? Input only, no ouput?
Chris KL
Well the SQL DateTime type is a DateTime type, it's not a string of a particular format. Maybe you need to be looking at your client code?
Steven Robbins
We're talking PHP here - I only have raw string access. .Net of course parses the string to a DateTime, but no such luck with PHP.
Chris KL
Then CONVERT is probably your only option unfortunately, or abstract your data access away and convert all the dates in there.
Steven Robbins
Oh well :( Damn Microsoft - date formatting is an application job, not database :(
Chris KL
+1  A: 

Here's a fabby wee article I just googled up:

http://www.sqljunkies.ddj.com/Article/6676BEAE-1967-402D-9578-9A1C7FD826E5.scuk

What you're looking for is:

CONVERT(datetime,'05/08/2004',120)

This will return a date in the format you're after yyyy-mm-dd hh:mi:ss(24h) format (ODBC canonical, not ISO).

HTH
Kev

Kev