CHANGING DATE FORMAT OF SQL SERVER DATABASE

MOON 60 Reputation points
2025-09-13T14:29:28.2833333+00:00

I have a database in which smalldatetime field exists. Its format is YYYYDDMM, yet I prefer YYYYMMDD. It could be set using set dateformat command, but it is only valid for current connection. I am looking for a solution to change date format permanently unless the database is recreated.

SQL Server Database Engine
{count} votes

1 answer

Sort by: Most helpful
  1. Erland Sommarskog 127.4K Reputation points MVP Volunteer Moderator
    2025-09-14T15:49:07.54+00:00

    The best way to deal with this problem is to use the format YYYYMMDD without delimiters, for instance 20251021. This format is always interpreted in the same way with all date-related data types in SQL Server.

    The format YYYY-MM-DD is also always understood correctly with the newer types datetime2 and datetimeoffset, but not the older data types datetime and smalldatetime, but here the interpretation depends on the DATEFORMAT setting, which in its turn takes its default from the language setting. Which in its turn defaults to us_english when you install an English version of SQL Server. With us_english follows the format mdy, and some smart person in California(*) realised that if the string is NNNN-NN-NN, then this can't be month-day-year, but apparently the year must come first. But apparently, the very idea that no one would ever write a date with year first was so alien, that they did not bother to do a reality check to see if any of the formats were used in practice. (In which case they would have found that YYYY-MM-DD is ISO standard, and YYYY-DD-MM is extremely little used, if at all.)

    You can change your personal default language with this command:

    ALTER LOGIN youruser WITH DEFAULT_LANGUAGE = British
    

    From now on, your session will default to the DATEFORMAT setting dmy.

    It is also possible to set a server-wide default for all new logins that are created on the server.

    However, none of these options is nothing I recommend. The best is to stick to the unambiguous formats and leave the defaults as they are.

    (*) California is where Sybase, who originally designed SQL Server, are located.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.