Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

Your profile

Search

July 2009
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

XML Feeds

Authors

« SQL Server Precision And Scale ProblemsDo not truncate your ldf files! »
The Data Management Journal

Setting a standard DateFormat for SQL Server

by George Mastros


Permalink 20 Nov 2008 11:58 , Categories: Data Modelling & Design, Microsoft SQL Server Admin, Microsoft SQL Server

Each SQL Server has a default language. You can see what the default language is by executing these commands (in a query window).

  1. SP_CONFIGURE 'default language'

This will tell you what the default language is (sort of). It actually returns a config_value with an integer that represents the language id.

You can then run...

  1. SP_HELPLANGUAGE

You will see a list of languages that SQL Server supports.

The odd thing here is that the server's language setting will not solve your problem. This setting configures the default language for NEW users. By changing this setting, existing users will continue to have their original language. This is where it gets interesting because it's the login's language setting that determines SQL Server's date format.

For example, if user A has a default language of us_english, then a date of 4/6/2006 will be interpreted as April 6, 2006. If user B has a default language of 'British', then the date will be interpreted as June 4, 2006.

So far, I'm just presenting some background information and haven't really solved the problem.

The good news is that you can change the default language for a user so that subsequent logins will exhibit the correct interpretation of dates. Here's how:

You can set the default language for a user by issueing the following command.

  1. SP_DEFAULTLANGUAGE @loginame = 'LoginName', @LANGUAGE = 'Language'

After running this command, you will need to logout and back in to the database in order for the change to take affect. The good news is that the language setting only needs to be done once (for each user in the database).

There is an alternative method, but it only works for the current session. You can set the language in your query (much the same way the Set DateFormat works). When you disconnect from the database, the language setting is NOT saved. Set Language differs from Set DateFormat regarding weekday names and month names, for example:

  1. SET LANGUAGE 'us_english'
  2.  
  3. SELECT CONVERT(DATETIME, '4/6/2006'),
  4.        DATENAME(weekday, '4/6/2006'),
  5.        DATENAME(MONTH, '4/6/2006')
  6.  
  7. SET LANGUAGE 'Italian'
  8.  
  9. SELECT CONVERT(DATETIME, '4/6/2006'),
  10.        DATENAME(Weekday, '4/6/2006'),
  11.        DATENAME(MONTH, '4/6/2006')

Summary
You can set the default language for new logins by configuring the server's default language by using sp_configure.

You can change a user's default language by using sp_defaultlanguage.

You can temporarily change the language for a query by using Set Language

I hope you find this useful.

4 comments »Send a trackback » 4155 views

Trackback address for this post

Trackback URL (right click and copy shortcut/link location)

4 comments

Comment from: onpnt [Member] Email
*****
In the presence of greatness!

Nice post there gmmastros
20/11/08 @ 12:11
Comment from: SQLDenis [Member] Email
*****
Nice post George, this is also the reason I always use ISO formats for my dates
20/11/08 @ 12:21
Comment from: chrissie1 [Member] Email
He did it ;-)
20/11/08 @ 23:21
Comment from: Naomi [Member] Email
*****
Thanks, very useful info.
08/04/09 @ 09:48

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)