Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating a DateTime field in SQL Server db from Date and Time fields

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?

like image 891
rem Avatar asked Apr 19 '11 08:04

rem


People also ask

How do I combine a date and a time field in SQL?

To combine date and time column into a timestamp, you can use cast() function with concat().

Can we UPDATE two fields at a time in SQL?

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.


1 Answers

UPDATE  mytable
SET     [DateTime] = CAST([Date] AS DATETIME) + CAST([Time] AS DATETIME)
like image 190
Quassnoi Avatar answered Oct 12 '22 22:10

Quassnoi