views:

17966

answers:

7

I need to change the date format from US (mm/dd/YYYY) to UK (dd/mm/YYYY) on a single database on a SQL server machine.

How can this be done?

I've seen statements that do this for the whole system, and ones that do it for the session, but I can't change the code now as it will have to go through QA again, so I need a quick fix to change the date time format.

Update

I realize that the date time has nothing to do with how SQL Server stores the data, but it does have a lot to do with how it parses queries.

I'm chucking raw data from an XML file into a database. The dates in the XML file are in UK date format.

A: 

You do realize that format has nothing to do with how SQL Server stores datetime, right?

You can use set dateformat for each session. There is no setting for database only.

If you use parameters for data insert or update or where filtering you won't have any problems with that.

Mladen Prajdic
See above answer
Omar Kooheji
+1  A: 

You can only change the language on the whole server, not individual databases. However if you need to support the UK you can run the following command before all inputs and outputs:

set language 'british english'

Or if you are having issues entering datatimes from your application you might want to consider a universal input type such as

1-Dec-2008

Nick Berardi
How does one change the language ont he server then?
Omar Kooheji
A: 

If this really is a QA issue and you can't change the code. Setup a new server instance on the machine and setup the language as "British English"

Nick Berardi
+1  A: 

In order to avoid dealing with these very boring issues, I advise you to always parse your data with the standard and unique SQL/ISO date format which is YYYY-MM-DD. Your queries will then work internationally, no matter what the date parameters are on your main server or on the querying clients (where local date settings might be different than main server settings)!

Philippe Grondier
unfortunately I'm not generating the data just consumung it.
Omar Kooheji
Philippe - I was convinced about that as well until someone showed me an example in SQL Server 2008 when date specified as 2008-09-01 was translated to 9th of January 2008 when the region was set to US.
kristof
As a rule I am trying to avoid using date in string format on the database level. When dealing with data input I would cast the string to date on the application level using appropriate locale settings so then it reaches the db in datetime date type. This way it is independent on the local settings
kristof
+2  A: 

You could use SET DATEFORMAT, like in this example

declare @dates table (orig varchar(50) ,parsed datetime)

SET DATEFORMAT ydm;

insert into @dates
select '2008-09-01','2008-09-01'

SET DATEFORMAT ymd;
insert into @dates
select '2008-09-01','2008-09-01'

select * from @dates

You would need to specify the dateformat in the code when you parse your XML data

kristof
A: 

Use:

select * from mytest
EXEC sp_rename 'mytest.eid', 'id', 'COLUMN'
alter table mytest add id int not null identity(1,1)
update mytset set eid=id
ALTER TABLE mytest DROP COLUMN eid

ALTER TABLE [dbo].[yourtablename] ADD DEFAULT (getdate()) FOR [yourfieldname]

It's working 100%.

A: 

SQL Server 2008 run... EXEC sp_defaultlanguage 'username', 'british'

http://twitter.com/paulhale