I have three Columns in my SQL Table: Date
, Time
, and Check
.
I want to concatenate Date and Time value together into a datetime column based on Check Column.
The Check column contains a varchar value either 'Today' or 'Tomorrow'. So, if the Check value is 'Today', the Time should concatenate with today's date, else if Check value is 'tomorrow', the time should concatenate with tomorrow's date.
Is there a way to concatenate two columns based on another column with SQL statements?
If you are doing a select then you can do as
select
date,
time,
case
when check = 'Today' then concat(curdate(),' ',time)
when check = 'Tomorrow' then concat( date_add(curdate(),interval 1 day) ,' ', time )
else concat(date,' ', time)
end as datetime
from table_name
Now if you want to update then you can do as
update table_name
set
datetime = case
when check = 'Today' then concat(curdate(),' ',time)
when check = 'Tomorrow' then concat( date_add(curdate(),interval 1 day) ,' ', time )
else concat(date,' ', time)
You can ignore the else
part or set it to something else as per your need.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments