Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine date from one column and time from another

Tags:

date

sql

oracle

Currently I'm trying to select a datetime column from two date columns. I'm having the following situation:

  1. FROMDATE column which contains dates without time, e.g. 08-01-2017 00:00:00
  2. FROMTIME column which contains times without a date, e.g. 01-01-1899 13:50:00

Now I want to select both within one column, e.g. SELECT (<what ever>) as FROMDATETIME ...

Expected result: 08-01-2017 13:50:00

But I'm not able to extract the date and time parts and add them together as a datetime.

Spoken in a kind of meta sql: select (date(FROMDATE) + time(FROMTIME)) as FROMDATETIME.

I've tried a lot with TO_CHAR and TO_DATE but was not able to get the expecting result.

like image 928
BendEg Avatar asked Dec 29 '16 10:12

BendEg


People also ask

How do I combine date and time in two columns in Excel?

Combine date and time with formula in Excel There is a very simple formula that can quickly help you combine date column and time column into one. Tip: You also can use this formula =A2+B2 and then format the result cells as date and time formatting.

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(). select cast(concat(yourDateColumnName, ' ', yourTimeColumnName) as datetime) as anyVariableName from yourTableName; In the above concept, you will use cast() when your date and time is in string format.


1 Answers

Here you go:

SELECT TO_DATE(TO_CHAR(t.fromdate,'dd-mm-yyyy') ||
               ' ' ||
               TO_CHAR(t.fromtime,'hh24:mi:ss'),'dd-mm-yyyy hh24:mi:ss') as full_date_col 
FROM YourTable t
like image 52
sagi Avatar answered Oct 20 '22 09:10

sagi