16

I used this part of a query to create a table column for the date and time a row is added:

order_date datetime NOT NULL DEFAULT GETDATE()

and whenever a new row is created, the data for order_date is set to something like this:

Apr 8 2014 9:52AM

For some reason, when a row is created and the order_date column data is set, the hour is set 1 hour back. For example, the above column data for Apr 8 2014 9:52AM was set at 10:52AM.

Is there a way to set it 1 hour ahead so that it is correct with my current time?

Thank you for any help. All help is greatly appreciated.

Testing360
  • 101
  • 10
Kelsey
  • 913
  • 3
  • 19
  • 41

2 Answers2

38

Use DATEADD()

DATEADD(hh, 1, order_date)

EDIT:

If the time is being set an hour back, you may have a wrong system time. So, it would be better if you just ask server admin to correct it.

Nathan
  • 1,220
  • 3
  • 15
  • 26
5

You should consider using DATETIMEOFFSET as your daatype instead of DATETIME.

Defines a date that is combined with a time of a day that has time zone awareness and is based on a 24-hour clock.

You can use it with SYSDATETIMEOFFSET().

Returns a datetimeoffset(7) value that contains the date and time of the computer on which the instance of SQL Server is running. The time zone offset is included.

Example:

 CREATE TABLE DateTest (id INT, order_date DATETIMEOFFSET NOT NULL DEFAULT SYSDATETIMEOFFSET())
 INSERT INTO DateTest (id) VALUES (1)
 SELECT * FROM DateTest
Karl Kieninger
  • 8,841
  • 2
  • 33
  • 49
  • I am using `date_default_timezone_set('America/Edmonton');` on my PHP page so I hope it will set the date/time to that. How would I go about creating a query to create a table column for this type of data? – Kelsey Apr 08 '14 at 18:48
  • SQL Server DATETIME doesn't have Time Zone info in it so PHP cannot do anything with it without more info. I'll might not have to time to add the query, but it's basically the same just using the different type and different function. – Karl Kieninger Apr 08 '14 at 19:02
  • Very good answer, here is an up vote. I would appreciate an up vote on my question, if possible. – Kelsey Apr 08 '14 at 19:06