Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SQL Server Precision And Scale ProblemsDo not truncate your ldf files! »
    comments

    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.

    About the Author

    George has been developing software professionally for 19 years, first for the department of defense, and then for various other companies. In 1998, George started his software company, Orbit Software, specializing in School Bus Transportation software. His specialty is refining SQL Server queries to deliver optimal performance.
    Social SitingsTwitterLTD RSS Feed
    InstapaperVote on HN

    11 comments

    Comment from: Ted Krueger (onpnt) [Member]
    *****
    Ted Krueger (onpnt) In the presence of greatness!

    Nice post there gmmastros
    11/20/08 @ 12:11
    Comment from: SQLDenis [Member] Email
    *****
    SQLDenis Nice post George, this is also the reason I always use ISO formats for my dates
    11/20/08 @ 12:21
    Comment from: Christiaan Baes (chrissie1) [Member]
    Christiaan Baes (chrissie1) He did it ;-)
    11/20/08 @ 23:21
    Comment from: Naomi Nosonovsky [Member]
    *****
    Naomi Nosonovsky Thanks, very useful info.
    04/08/09 @ 09:48
    Comment from: Kiche [Visitor] Email
    *****
    Kiche Been looking for this stuff for months, at last got it. Thanks
    09/16/09 @ 01:59
    Comment from: ugg boots [Visitor] · http://uggbootshop.net
    ugg boots the entry is good!
    01/21/10 @ 00:39
    Comment from: Tanger [Visitor] Email · http://www.tangertoday.com/
    Tanger No wonder I had problem with date inserted by PHP. Thanks this method is good practice.
    02/17/10 @ 14:10
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky Would be nice if there would be a language option in Convert function.
    05/12/10 @ 13:55
    Comment from: George Mastros (gmmastros) [Member]
    George Mastros (gmmastros) Naomi,

    I agree that it would be a nice option to have. Most people know that you can use the optional 3rd parameter (style) when converting from DateTime to varchar, but most people don't realize you can also use it when converting from varchar to DateTime. Ex:

    Set Language us_english

    Select Convert(DateTime, '4/6/2010',101), Convert(DateTime, '4/6/2010',103)

    Set Language 'Italian'

    Select Convert(DateTime, '4/6/2010',101), Convert(DateTime, '4/6/2010',103)

    Notice how the language setting has no effect on the actual converted date, but the style argument does.
    05/12/10 @ 14:05
    Comment from: uggeuro [Visitor]
    uggeuro Nice post George
    12/14/10 @ 01:12
    Comment from: PK [Visitor]
    PK Very useful, thanks
    11/16/11 @ 21:28

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

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