I want to write some SQL which results in an automatic conversion of a
datetime to a string in a format suitable for the Language of the
connection (either by explicitly setting the Language in the
connection string, or by setting the default language in for the user
used for the connection.)
The casting from string to datetime uses the language setting:
Data Source=localhost\sqlexpress;Initial Catalog=master;Persist
Security Info=True; Language =BRITISH ENGLISH; Trusted_Connection=yes;
Application Name = Test Application;
select cast('13/01/2008' as datetime) --WORKS as expected
select cast('01/13/2008' as datetime) --FAILS as expected
and
Data Source=localhost\sqlexpress;Initial Catalog=master;Persist
Security Info=True; Language =ENGLISH; Trusted_Connection=yes;
Application Name = Test Application;
select cast('13/01/2008' as datetime) --FAILS as expected
select cast('01/13/2008' as datetime) --WORKS as expected
but implicit casting the other way ignores the "Language setting" (ie
the format is the same for both):
BRITISH ENGLISH
select cast(cast('12/01/2008' as datetime) as nvarchar(max)) --Jan 12
2008 12:00AM
select convert(nvarchar(max),cast('12/01/2008' as datetime)) --Jan 12
2008 12:00AM
ENGLISH
select cast(cast('12/01/2008' as datetime) as nvarchar(max)) --Dec 1
2008 12:00AM
select convert(nvarchar(max),cast('12/01/2008' as datetime)) -- Dec 1
2008 12:00AM
Is it possible to tell SQL Server "For language w convert datetimes to
strings using format x, but for language y use format z?"
Regards,
Dan