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 2000 Forums
 Transact-SQL (2000)
 Update time part of a datetime field.

Author  Topic 

mr.modus
Starting Member

27 Posts

Posted - 2007-09-21 : 11:43:40
Hello, I am working with a table that was originally a MSAccess table. I have a datetime field called Flight_Date and a varchar field called Flight_Time. The Flight_Date field only contains the date, the time part of the field is "00:00:00.0" for all records. The Flight_Time field is either 'AM' or 'PM.' I want to change this so that Flight_Date has either "06:00:00.0" for 'AM' flights and "18:00:00.0" for 'PM' flights. Is this possible?

Kristen
Test

22859 Posts

Posted - 2007-09-21 : 11:52:06
[code]
UPDATE U
SET Flight_Date = DATEADD(Hour,
CASE Flight_Time WHEN 'AM' THEN 6
WHEN 'PM' THEN 18
ELSE 0
END,
DATEADD(Day, DATEDIFF(Day, 0, Flight_Date), 0)) -- Remove any existing Time part
FROM MyTable AS U
[/code]
Backup first!!

Kristen
Go to Top of Page

mr.modus
Starting Member

27 Posts

Posted - 2007-09-21 : 12:07:22
Kristen you are a life saver. Thank you, thank you and thank you!
You are the best!
Go to Top of Page
   

- Advertisement -