Time stamp date to mm/dd/yyyy in oracle sql - sql

I have a field st_ts that shows as 07/09/2021 5:20:52 PM. How do I get it to show as just 07/09/2021? I've tried TO_DATE('ST_TS','MM/DD/YYYY') but it isn't working. In there where clause I have to do TRUNC(ST_TS) = '09-JUL-2021' to select the date. Thanks.

It depends on what that column's datatype is. I presume it is DATE. If that's so, have a look at the following examples:
SQL> create table test (st_Ts date);
Table created.
SQL> insert into test values (to_date('07/09/2021 05:20:52', 'mm/dd/yyyy hh24:mi:ss'));
1 row created.
Altering the session and setting date format mask:
SQL> alter session set nls_date_format = 'mm/dd/yyyy';
Session altered.
SQL> select * from test;
ST_TS
----------
07/09/2021
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select * from test;
ST_TS
-------------------
09.07.2021 05:20:52
Applying TO_CHAR function with desired format mask:
SQL> select to_char(st_ts, 'mm/dd/yyyy') result from test;
RESULT
----------
07/09/2021
Truncating DATE value "resets" time portion to midnight:
SQL> select trunc(st_ts) result2 from test;
RESULT2
-------------------
09.07.2021 00:00:00
SQL>
Therefore, you have various options. It depends on what you're up to. If you're using some reporting tool, I'd suggest you to set field's format mask there. If you just want to display it differently, use TO_CHAR. For the whole session length, alter the session.

Related

Inserting date&time, but only date is inserted

I'm trying to insert in a table the date and the time,but at the end only the date is insrted.
here's the code:
create table myDate(date_value Date PRIMARY KEY );
INSERT INTO myDate(date_value ) VALUES (to_date('14/09/2010 18:00','dd/mm/yyyy hh24:mi'));
And I get only 14/09/2010 stocked in the table myDate.What's the problem(is there a way to do that without timestamp)?
I think it worth noting that an alternatives to setting the session NLS parameter is the explicit use of the to_char function. I prefer the to_char because it leaves no doubt to anyone reading the code. Reliance on NLS can be iffy because it can be set at different levels. Besides, if you don't explicitly use to_char, oracle will still call it in the background, using the controlling setting of NLS_DATE_FORMAT.
SQL> -- create table
SQL> Create table mytest (dob date);
Table created.
Elapsed: 00:00:00.08
SQL> insert into mytest values (sysdate);
1 row created.
Elapsed: 00:00:00.03
SQL> commit;
Commit complete.
Elapsed: 00:00:00.00
SQL> select dob nls_default,
2 to_char(dob,'dd-mm-yyyy') dob1,
3 to_char(dob,'yyyy-mm-dd') dob2,
4 to_char(dob,'dd-mm-yyyy hh24:mi:ss') dob3
5 from mytest;
NLS_DEFAU DOB1 DOB2 DOB3
--------- ---------- ---------- -------------------
11-DEC-20 11-12-2020 2020-12-11 11-12-2020 11:45:31
1 row selected.
Elapsed: 00:00:00.03
SQL> drop table mytest purge;
Table dropped.
For more on the subject, see this article.
Depending on the tool you are using is most likely a display issue.
you might want to try something like this
alter session set nls_date_format = 'DD-MON-YYYY HH12:MI:SS PM';
This will alter your sesssion to display the date with full timestamp.
I use oracle sql developer https://www.oracle.com/database/technologies/appdev/sqldeveloper-landing.html
If I don't set the date to my session while I'm writing a query I may see an unexpected date format.
Let us know if that is the case for you as well.

How do I fix ORA-01843: not a valid month?

So at the query level, I have it:
to_char(
(
to_date(
substr(TIMESTAMP, 1, 19),
'yyyy-mm-dd hh24:mi:ss'
)
),
'dd-mon-yyyy hh24:mi:ss'
) as DateTime,
And I've tried looking at a few articles with one most notable:
How to change the date format in Oracle BI Publisher?
I have also tried using:
and trunc(TIMESTAMP) between :FROM_DATE AND :TO_DATE
--and also
and trunc(TIMESTAMP) between to_date(:FROM_DATE, 'yyyy-MM-dd') AND to_date(:TO_DATE, 'yyyy-MM-dd')
While going through structure and XML I noticed my date is in string format:
element name="DATETIME" value="DATETIME" label="DATETIME" dataType="xsd:string" breakOrder="ascending" fieldOrder="3"
So I removed the to_char to get the date format
The error I've been getting is:
java.sql.SQLDataException: ORA-01843: not a valid month
How do I fix this issue?
EDIT:
Format for the column, TIMESTAMP, the format is CHAR(14)
Example of values is like 20200701103038
It runs perfectly in SQL Developer
Well, it is quite a bad and extended practice to store DATES as strings, either using varchar2 or char. Anyway, having say that, I think you have a problem with your settings or the way you are constructing your query:
SQL> alter session set nls_date_format='YYYYMMDDHH24MISS' ;
Session altered.
SQL> select to_date('20200726123722') from dual ;
TO_DATE('20200
--------------
20200726123722
SQL> select sysdate from dual ;
SYSDATE
--------------
20200726124622
Besides, as you said, if your data is stored as YYYYMMDDHHMISS, you are applying the wrong date mask YYYY-MM-DD HH24:MI:SS to that char. I would use CAST to define the field as DATE.
Example
SQL> create table my_test ( c1 char(20) ) ;
Table created.
SQL> insert into my_test values ('20200726123722') ;
1 row created.
SQL> insert into my_test values ('20200725123722') ;
1 row created.
SQL> commit ;
Commit complete.
SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
Session altered.
SQL> select cast(c1 as date) from my_test ;
CAST(C1ASDATE)
-------------------
2020-07-26 12:37:22
2020-07-25 12:37:22
SQL>
Update
If you can't change your NLS Session settings, then you must apply a TO_CHAR to the resulting output. But in your case, you want to operate with dates, so as long as it is a date value you want to operate with, you can forget about the mask.
SQL> col value for a20
SQL> select value from nls_database_parameters where parameter = 'NLS_DATE_FORMAT' ;
VALUE
--------------------
DD-MON-RR
SQL> select cast(to_date('20200725123722','YYYYMMDDHH24MISS') as date) from dual ;
CAST(TO_D
---------
25-JUL-20
SQL> select to_char( cast(to_date('20200725123722','YYYYMMDDHH24MISS') as date) , 'YYYYMMDDHHMISS' ) from dual ;
TO_CHAR(CAST(T
--------------
20200725123722
SQL> select case when cast(to_date('20200725123722','YYYYMMDDHH24MISS') as date) > sysdate
2 then 'FALSE'
3 else
4 'TRUE'
5 end as result from dual ;
RESUL
-----
TRUE
SQL>
So, if you want to compare the date to another date, don't use to_char. If you want to show the value in a specific format, when you have no option to change the settings, then use to_char.
Just to make sure what SYSDATE (I'm going to select) represents:
SQL> alter session set nls_Date_format = 'dd.mm.yyyy';
Session altered.
Today is:
SQL> select sysdate from dual;
SYSDATE
----------
26.07.2020
This is the way to get the error you got: apply wrong format mask to a string which represents a DATE value:
SQL> select to_Date('2020-27-07', 'yyyy-mm-dd') from dual;
select to_Date('2020-27-07', 'yyyy-mm-dd') from dual
*
ERROR at line 1:
ORA-01843: not a valid month
SQL>
How to fix it? Usually, it is hard to fix it if dates are represented as strings. They (strings that represent dates) are like a box of chocolates, you never know what you're gonna get. If there's at least one wrong value, query will fail.
How to find wrong values? You could create a function which returns TRUE (or 1 or whatever you want) if a string you pass to it represents a valid date format. But, if you pass 01/02/03, which is which? Different formats match (e.g. dd/mm/yy, yy/mm/dd, mm/yy/dd ...). Worse cases are 84/25/32 or AB/23/2f. They are all strings, they "match" two characters separated by slash but certainly aren't valid dates, so you can't rely on a simple regular expression.
Shortly, there's no easy nor fast way out of it.

how i can format a column

i want to format a column in oracl 10g
its type is date
I want to show me always only hours and minutes
any solution with alter ..
thanks
If you want to show only hours / minutes from a timestamp field, you can use:
select to_char( sysdate, 'HH-MI' ) as sysdate_format from dual;
Date/Time Formatting in Oracle
Suppose this is what you have now:
SQL> alter session set nls_date_format = 'ddmmyy';
Session altered.
SQL> select sysdate from dual;
SYSDAT
------
131019
One option is to use TO_CHAR:
SQL> select to_char(sysdate, 'dd.mm.yyyy hh24:mi:ss') from dual;
TO_CHAR(SYSDATE,'DD
-------------------
13.10.2019 08:46:16
SQL>
but it requires you to always use it; another is to alter session and set desired format mask (which is probably what you want):
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select sysdate from dual;
SYSDATE
-------------------
13.10.2019 08:45:50
SQL> select min(hiredate) from emp;
MIN(HIREDATE)
-------------------
17.12.1980 00:00:00
SQL>
To show just the time component of a date, you need to convert the value to a string.
For the hours in a 24-hour format, you can use:
select to_char(sysdate, 'HH24:MI')
from dual;
Of course, sysdate is a built-in date value. You would use whatever value or column you desire.

Issue with NLS_DATE_FORMAT

Why Am I seeing different date formats below : see the output of select statement and output of anonymous block
13:46:23 13:46:23 SQL> conn prashant-mishra/ *****#*****
Connected to Oracle Database 12c Enterprise Edition Release 12.1.0.2.0
13:47:56 13:47:56 SQL> CREATE TABLE test AS SELECT SYSDATE as test_date
FROM dual;
Table created
13:48:15 13:48:15 SQL> SELECT test_date FROM test;
TEST_DATE
-----------
**10/13/2016**
13:50:17 13:50:17 SQL> BEGIN
2 FOR rec IN (SELECT test_date FROM test) LOOP
3 dbms_output.put_line(rec.test_date);
4 END LOOP;
5 END;
6 /
**13-OCT-16**
PL/SQL procedure successfully completed
Other helpful info may be :
13:50:20 13:50:20 SQL> SELECT value FROM v$nls_parameters
WHERE parameter ='NLS_DATE_FORMAT';
VALUE
----------------------------------------------------------------
DD-MON-RR
13:50:45 13:50:45 SQL> SELECT SYSDATE FROM dual;
SYSDATE
-----------
**10/13/2016**
SQL*Plus uses NLS_DATE_FORMAT to format dates as strings so a complete test case needs to show what this parameter is beforehand. Would you please try the following and see if you get consistent formatting:
SQL> select value from nls_session_parameters where parameter = 'NLS_DATE_FORMAT';
VALUE
----------------------------------------------------------------
DD-MON-RR
SQL> select sysdate from dual;
SYSDATE
------------------
13-OCT-16
SQL> alter session set nls_date_format = 'YYYY-MM-DD';
Session altered.
SQL> select sysdate from dual;
SYSDATE
----------
2016-10-13
SQL> alter session set nls_date_format = 'DD-MON-RR';
Session altered.
SQL> select sysdate from dual;
SYSDATE
------------------
13-OCT-16
SQL>
You get the following output based on your comments:
SQL> select value from nls_session_parameters where parameter = 'NLS_DATE_FORMAT';
VALUE
DD-MON-RR
SQL> select sysdate from dual;
SYSDATE
10/14/2016
SQL> alter session set nls_date_format = 'YYYY-MM-DD';
Session altered
SQL> select sysdate from dual;
SYSDATE
10/14/2016
SQL> alter session set nls_date_format = 'DD-MON-RR';
Session altered
SQL> select sysdate from dual;
SYSDATE
10/14/2016
So - SQLPlus is ignoring the session NLS_DATE_FORMAT and using it's only local format of MM/DD/YYYY. I didn't think there was a date format override in SQL*Plus but could be wrong. Have you tried the same commands in a different client?

How to get time string to Time in oracle sql

I have insert time in form of string in oracle VARCHAR2 column. But when I try to retrieve it in form of time it's not giving me right time, it's only giving me a date which I have not saved.
INSERT INTO table1
(timestr) Select substr(numtodsinterval(MAX(date1)-MIN(date2),'day'),
12,8) from table2 where ....; // stored timestr column value: 00:00:00
Retrieve ...
select TO_DATE(timestr,'hh24:mi:ss') from table1;
... is only giving 10/01/2015
You should use to_char to see the time
select to_char(timestr,'hh24:mi:ss') from table1;
That might be because of your client setting. If you are using SQL Developer, then go to Tools->Preference->Database->NLS and change setting to view timestamp also.
If you are using SQLPlus, the change nls_date_format or see below solution.
https://community.oracle.com/thread/312115?start=0&tstart=0
In Oracle there is no concept of a TIME datatype. There are only DATE and TIMESTAMP, both of which comprise date and time elements.
Your retrieval casts your time column to a DATE: TO_DATE(timestr,'hh24:mi:ss'). Oracle does what we ask it to do, so it displays a date. As there is no date element in timestr it assigns one for us: today's date.
You are only seeing 10/01/2015 because your client's NLS settings are configured to display only the date element. Change the setting to show the time element, in the client or by altering the session as here:
SQL> select to_date('10:08:23', 'hh24:mi:ss') ts from dual
2 /
TS
---------
01-OCT-15
SQL> alter session set nls_date_format='DD-MON-YYYY HH24:MI:SS'
2 /
Session altered.
SQL> select to_date('10:08:23', 'hh24:mi:ss') ts from dual
2 /
TS
--------------------
01-OCT-2015 10:08:23
SQL>
If you only want to see the time you can change the NLS settings ...
SQL> alter session set nls_date_format='HH24:MI:SS'
2 /
Session altered.
SQL> select to_date('10:08:23', 'hh24:mi:ss') ts from dual
2 /
TS
--------
10:08:23
SQL>
...but that will get annoying ....
SQL> select sysdate from dual
2 /
SYSDATE
--------
23:59:52
SQL>
So why not just display the column as a string all without casting to a DATE?
select timestr from table1;