I have got several tables in my database (with data) that is formatted in the American standard of mm/dd/yyyy. Is there a way to convert the date to a British format (i.e. dd/mm/yyyy) that doesn't involve dropping and recreating the tables?
Thanks!
I have got several tables in my database (with data) that is formatted in the American standard of mm/dd/yyyy. Is there a way to convert the date to a British format (i.e. dd/mm/yyyy) that doesn't involve dropping and recreating the tables?
Thanks!
I set my data type to >date when I was creating my table. I can store dates in the format
mm/dd/yyyy
, but notdd/mm/yyyy
.
As I've mentioned in my comment, dates are not stored with their display format - in fact, you can say that dates have no display format - only string representation of dates have a display format.
Whenever dealing with string literals representing date and datetime values in SQL server, use ISO 8601 datetime format (yyyy-MM-ddTHH:mm:ss
or yyyyMMddTHHmmss
).
SQL Server guarantees to properly parse this string representation into date / datetime values, without ambiguity.
Please note the T
seperator between the date and the time. There is a very similar standard format, where the T
is replaced with a white-space, but the DateTime
data type have a bug parsing this format - and it is culture-dependent (Note that DateTime2
does not have that bug) - and that's another reason why you should never use datetime again.
When you use a string literal like '25/03/2018'
it's easy for a human to see that it stands for March 25th 2018
, but SQL Server will raise an error trying to parse this string into a date if the current value of DATEFORMAT
is not DMY
.
However, SQL Server will always parse ISO 8601 string representation of dates correctly, regardless of any local settings or previous set dateformat
or set language
statements etc'. '2018-02-01T15:40:50'
will always be parsed is February 1st 2018, 3:40:50 PM
.
Unless specified, As Martin Smith wrote in his comment, the default dateformat
depends on the defualt language settings of the current login - so a query that works for one login might raise an error for another login - and that's another good reason never to trust culture-specific string representation of datetime.
Are you referring to the date format displayed by SQL Server Management Studio or a similar application? The format is controlled by Windows Control Panel settings, not by SQL Server. There is no internal format for dates in SQL Server.
This is defined by default from the machine where is running MS SQL Server.
To see all available cultures please do:
select * from sys.syslanguages
Then, you can change SQL Server language using:
SET LANGUAGE BRITISH
... and the date format will always be like you want.
Note: this will change all the database (not just the date format), the other way is to change the date format using the FORMAT
function in T-SQL.