I have a table test
with the following columns: id
, startDate
, stopDate
, startOvertime
, stopOvertime
.
startDate
and stopDate
are of type datetime and startOvertime
, stopOvertime
are of type time(7)
.
I want to create a new column with the date portion from stopDate
and the time portion from startOvertime
and copy that into a newly created column test-fill
.
If I use formula (stopDate, startOvertime) then the value of the new datetime column is not right. Any proposals?
In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is also used to add and drop various constraints on the existing table.
this: ALTER TABLE YourTable ADD YourNewColumn INT NOT NULL DEFAULT 10 WITH VALUES; Add the column with null values first. Then update all rows to enter the values you want.
INSERT INTO SELECT Syntax Copy only some columns from one table into another table: INSERT INTO table2 (column1, column2, column3, ...)
If you always want the new column with this information, then you can use a computed column:
alter table test add test-fill as
(cast(cast(startDate as date) as datetime) + cast(overtime as datetime));
This doesn't actually add a new column. It just computes the values when you need them.
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