How to concatenate date and time columns based on value from another column in sql?

user1930115

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?

Abhik Chakraborty

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

UPDATE sql column with value from another column based on a date column

From Dev

How to extract multiple rows from a table based on values from multiple columns from another table and then concatenate in SQL?

From Dev

How to Create Two Columns from One Column in SQL Based on a IF argument in Another Column

From Dev

How to add in or concatenate a a value in another value in a column?

From Dev

How to add in or concatenate a a value in another value in a column?

From Dev

SQL inner join on a column based on max value from another column

From Dev

SQL how create a Boolean column based on the value of another column

From Dev

How to find adjacent values in a column based on another column value in Sql

From Dev

in sql, how to make a rank column based on the value of another column?

From Dev

How can update a column based on the value of another column in SQL?

From Dev

How to convert the date value taken from the date time picker to the Date column in SQL Server table in Visual C#?

From Dev

Dax formula for power bi that gives the last value of a column based on the latest date and time on another column

From Dev

Python - Concatenate multiple columns based on the value of each column

From Dev

Sqlite add column based on another columns value

From Dev

SQL combine columns based on another column

From Dev

Populating a column based on value of another column (SQL)

From Dev

How we find Latest Date value and update in another column in Sql?

From Dev

How can I concatenate date from another column when I use groupby and aggregation in a pandas dataframe

From Dev

SQL Server: split a column value into two separate columns based on another column

From Dev

How to concatenate a column value with single quotes in sql?

From Dev

How to select a bit based on value in another tables column in SQL

From Dev

link a value from one table to another and slice one table based on columns from another table in sql

From Dev

How to get parameter value for date/time column from empty MaskedTextBox

From Dev

Concatenate time value to match row's Date SQL Server

From Dev

the next closest date time from another column

From Dev

how to separate date and time from same column sql

From Dev

SQL server concatenate date and time

From Dev

R - How to get value from a column based on value from another column of same row

From Dev

How do I make one column return a desired value based on the date in another column?

Related Related

  1. 1

    UPDATE sql column with value from another column based on a date column

  2. 2

    How to extract multiple rows from a table based on values from multiple columns from another table and then concatenate in SQL?

  3. 3

    How to Create Two Columns from One Column in SQL Based on a IF argument in Another Column

  4. 4

    How to add in or concatenate a a value in another value in a column?

  5. 5

    How to add in or concatenate a a value in another value in a column?

  6. 6

    SQL inner join on a column based on max value from another column

  7. 7

    SQL how create a Boolean column based on the value of another column

  8. 8

    How to find adjacent values in a column based on another column value in Sql

  9. 9

    in sql, how to make a rank column based on the value of another column?

  10. 10

    How can update a column based on the value of another column in SQL?

  11. 11

    How to convert the date value taken from the date time picker to the Date column in SQL Server table in Visual C#?

  12. 12

    Dax formula for power bi that gives the last value of a column based on the latest date and time on another column

  13. 13

    Python - Concatenate multiple columns based on the value of each column

  14. 14

    Sqlite add column based on another columns value

  15. 15

    SQL combine columns based on another column

  16. 16

    Populating a column based on value of another column (SQL)

  17. 17

    How we find Latest Date value and update in another column in Sql?

  18. 18

    How can I concatenate date from another column when I use groupby and aggregation in a pandas dataframe

  19. 19

    SQL Server: split a column value into two separate columns based on another column

  20. 20

    How to concatenate a column value with single quotes in sql?

  21. 21

    How to select a bit based on value in another tables column in SQL

  22. 22

    link a value from one table to another and slice one table based on columns from another table in sql

  23. 23

    How to get parameter value for date/time column from empty MaskedTextBox

  24. 24

    Concatenate time value to match row's Date SQL Server

  25. 25

    the next closest date time from another column

  26. 26

    how to separate date and time from same column sql

  27. 27

    SQL server concatenate date and time

  28. 28

    R - How to get value from a column based on value from another column of same row

  29. 29

    How do I make one column return a desired value based on the date in another column?

HotTag

Archive