Reputation: 17055
In a SQL Server 2008 database I have a table with a Date field of datatype date
and a Time field of datatype time
. The table contains some data.
Then I added a DateTime field of datatype datetime
and wanted to populate this field with values from corresponding Data and Time fields (of the same row).
I can update either date or time part of DateTime field by executing:
SET [DateTime] = Cast([Date] as datetime)
or
SET [DateTime] = Cast([Time] as datetime)
But how to correctly combine these operations and update the whole DateTime field?
Upvotes: 6
Views: 4984
Reputation: 425371
UPDATE mytable
SET [DateTime] = CAST([Date] AS DATETIME) + CAST([Time] AS DATETIME)
Upvotes: 7