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?
To combine date and time column into a timestamp, you can use cast() function with concat().
We can update multiple columns by specifying multiple columns after the SET command in the UPDATE statement. The UPDATE statement is always followed by the SET command, it specifies the column where the update is required.
UPDATE mytable
SET [DateTime] = CAST([Date] AS DATETIME) + CAST([Time] AS DATETIME)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With