Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle SQL "SELECT DATE from DATETIME field "

I have field REPORTDATE (DATETIME). In SQL Developer i can see its value in this format

29.10.2013 17:08:08

I found that in order to do the select of just a DATE I need to execute this:

SELECT TO_DATE (REPORTDATE, 'DD.MON.YYYY') AS my_date
FROM TABLE1

but it returns 0RA-01843: not a valid month

I want result to return only 29.10.2013

like image 371
Veljko Avatar asked Mar 12 '15 08:03

Veljko


People also ask

How do I convert a datetime column to a DATE in SQL?

As you can see from the script above, to convert the DateTime type column to Date, you can use the CAST function. You need to pass the column name followed by the AS statement and the DATE type to the CAST function.

Can you convert DATE to datetime in SQL?

We can convert the Date into Datetime in two ways. Using CONVERT() function: Convert means to change the form or value of something. The CONVERT() function in the SQL server is used to convert a value of one type to another type. Convert() function is used to convert a value of any type to another datatype.


2 Answers

TO_DATE (REPORTDATE, 'DD.MON.YYYY')

This makes no sense. You are converting a date into a date again. You use TO_DATE to convert a string literal into DATE.

I want result to return only 29.10.2013

You could use TRUNC to truncate the time element. If you want to use this value for DATE calculations, you could use it directly.

For example,

SQL> select TRUNC(SYSDATE) dt FROM DUAL;

DT
---------
12-MAR-15

To display in a particular format, you could use TO_CHAR and proper FORMAT MASK.

SQL> SELECT to_char(SYSDATE, 'DD.MM.YYYY') dt from dual;

DT
----------
12.03.2015

SQL>
like image 156
Lalit Kumar B Avatar answered Sep 21 '22 00:09

Lalit Kumar B


Use this:

SELECT trunc(REPORTDATE, 'DD') AS my_date
FROM TABLE1

This will not change the type of the returning object, only truncates everything below "day" level.

If you are ok with returning a String, then you can just do:

SELECT TO_CHAR(REPORTDATE, 'DD.MM.YYYY') AS my_date
FROM TABLE1
like image 28
Gergely Bacso Avatar answered Sep 20 '22 00:09

Gergely Bacso