Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Dates Problem

Author  Topic 

Steve2106
Posting Yak Master

183 Posts

Posted - 2011-03-12 : 03:15:19
Hi There,

We are a company located in the UK and the hosting for our website & Sql Server is in the US.
I have always had issues with dates trying to save to Sql & read / display them in UK format.
An issue came up yesterday where a record was saved and the create date was shown as 03/11/2011 (created in the future) it should have been 11/03/2011.
I have managed to sort the code out ( I think) but I am left with records with the wrong dates. (US format not UK)
Does anyone know how I can loop through the table and if the create date is greater than today swap the day and month parts to correct the date for UK.

I appreciate any help you can give.

Best Regards,


Steve

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-12 : 07:49:59
Everything I am going to say is assuming that the data is currently stored as one of the datetime data types (datetime or smalldatetime). Assuming that that is the case and assuming that you don't care about only the dates (and not the time part), here is one way to do it:
declare @tbl table(date datetime);
insert into @tbl values ('20110101');
insert into @tbl values ('20110703');

-- do a test to see if there are dates that won't convert.
select * from @tbl where day(date) > 12 and date > current_timestamp;

-- compose the date string in the YYYYMMDD format and update
-- This format will be interpreted correctly in any DATEFORMAT
-- setting. But convert it using style 112 anyway.
update @tbl set date=
convert(
datetime
,cast(year(date) as varchar(4)) +
right('0'+cast(day(date) as varchar(2)),2) +
right('0'+cast(month(date) as varchar(2)),2)
,112)
where
date > current_timestamp
and day(date) <= 12;

select * from @tbl
The text in red is where I am flipping the day and month.

This MSDN page has information about the various datetime styles, including the 112 format that I used above: http://msdn.microsoft.com/en-us/library/ms187928.aspx

I also have couple of questions/comments:

1. How are you going to/Can you identify dates are in the past that were entered incorrectly? For example February 1, 2011?

2. Do you have a plan of action to prevent this in the future? People use Calendar drop downs, long date formats etc. so Feb 1, 2011 won't get confused with Jan 2, 2011.

3. Look what doing things the wrong way gets you into!?! Driving on the wrong side of the road, using the wrong date format.. sheesh!! (*)

Anyway, good luck.

(*) That was my lame attempt at humor. I grew up in India, so I am just as guilty of the said crimes as you are :--)
Go to Top of Page

Steve2106
Posting Yak Master

183 Posts

Posted - 2011-03-13 : 07:54:16
Hi Sunita,
Thanks for your reply.

you are right I made a massive mistake hosting our stuff on US servers when we are based in the UK. I can only put it down to not really knowing enough about Sql Server and the way it works.

I take it that your DECLARE @tbl is creating a test table and I should obviously use my real table? This is the structure of my table:
[AutoId] [bigint] IDENTITY(1,1) NOT NULL,
[ActionId] [bigint] NOT NULL,
[DateCreated] [datetime] NULL,
[EmailFrom] [varchar](100) NULL,
[EmailTo] [varchar](100) NULL,
[EmailType] [varchar](25) NULL,
[ChangeText] [nvarchar](500) NULL

Not sure how I go about your question 1. but if I can repair the ones that are greater than todays date would be a good start.

Is there a way I could extract all the offending records into a new table, repair the dates then update them in the original table?

Your question 2. is a good one. I thought I had taken care of the date conversion in the Asp.Net code using the Format(DateVar, "yyyy/MM/dd") but obviously not.

Sunita, I really appreciate you taking the time to help me.

Best Regards,



Steve
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-13 : 18:07:04
quote:
Originally posted by Steve2106


Is there a way I could extract all the offending records into a new table, repair the dates then update them in the original table?


I usually backup the unadulterated data into a backup table, and then do the update to the original table. The following will create the backup table and insert the data into that table in one shot:
select * into YourBackupTable from YourTable
If the table has a large number of rows and you want to save only the rows that you are going to update, use the SAME where clause you will be using to update, as in
select * into YourBackupTable from YourTable where
date > current_timestamp
and day(date) <= 12;
Then you can update the table with
update YourTable set DateCreated = 
-- select
convert(
datetime
,cast(year(DateCreated) as varchar(4)) +
right('0'+cast(day(DateCreated) as varchar(2)),2) +
right('0'+cast(month(DateCreated) as varchar(2)),2)
,112)
-- ,* from YourTable
where
DateCreated > current_timestamp
and day(DateCreated) <= 12;
If you would like to see what the new values and the old values would be before doing the update, uncomment the two lines that are commented out, comment the first line, and run the resulting select statement.

Be very careful about including the where clause in the update statement; if you don't EVERY record in the table will get their dates flipped.
Go to Top of Page

Steve2106
Posting Yak Master

183 Posts

Posted - 2011-03-14 : 05:59:55
Hi Sunita,

Once again thanks for the reply.

That looks great I'll give it a try when I get back to my PC.

You have given me a complete solution and have even taken into account the maybe's. Appreciated.

It's such a benifit having someone that is helpful and willing to share, it will be repaid I'm sure.

Best Regards,



Steve
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-03-14 : 08:09:54
And as a future reference, I'd strongly recommend that when inserting data in to a database (any database!) you should use the format 'YYYYMMDD'. It is the *only* common format that is interpreted correctly in all databases.

- Lumbago
My blog-> http://thefirstsql.com/2011/02/07/regular-expressions-advanced-string-matching-and-new-split-function-sql-server-2008-r2/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-03-14 : 11:30:13
Also read
http://beyondrelational.com/blogs/madhivanan/archive/2010/06/03/understanding-datetime-column-part-ii.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -