Inserting date&time, but only date is inserted - sql

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.

Related

Time stamp date to mm/dd/yyyy in oracle 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.

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.

Inserting a TIMESTAMP value in SQL Developer

Hello I'm having a trouble inserting this value in Oracle SQL Developer Version 19.4.0.354.
'2013-01-01 00:00:00.0000'
Here is the value that I want to insert in one of my tables.
I tried DATE and TIMESTAMP data types but they don't work. I also tried altering the sessions and other possible solutions all over the internet.
Column datatype should be TIMESTAMP. Use appropriate format mask in TO_TIMESTAMP function.
SQL> create table test (col timestamp);
Table created.
SQL> insert into test (col) values (to_timestamp('2013-01-01 00:00:00.0000', 'yyyy-mm-dd hh24:mi:ss:ff6'));
1 row created.
What's in there? (alter session is here just to display the result in desired format; it doesn't affect the value stored in that column):
SQL> alter session set nls_timestamp_format = 'dd.mm.yyyy hh24:mi:ss.ff6';
Session altered.
SQL> select * From test;
COL
---------------------------------------------------------------------------
01.01.2013 00:00:00.000000
SQL>
The simplest approach is to use a literal timestamp, so you don't rely on implicit conversion:
select timestamp '2013-01-01 00:00:00.0000' from dual;
You can also use to_timestamp():
select to_timestamp('2013-01-01 00:00:00.0000', 'yyyy-mm-dd hh24:mi:ss:ff4') from dual;
On the other hand, if you really want to work with a string, then you would need to change the nls setting to your own format before:
alter session set nls_timestamp_format = 'yyyy-mm-dd hh24:mi:ss:ff4';
You can then use pass your string value directly in the query, and Oracle will implicitly convert it to the target datatype while inserting.
You have no time component, so I would just use:
date '2013-01-01'
This is the safest syntax (and clearest and standard too!) for providing a date value.
If Oracle wants a timestamp, it will convert this correctly.

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;

check for valid date which is declared in varchar2

My table looks like below which is declared in VARCHAR2:
YMD
20101010
20101112
20100231
20150101
20160101
I have to check for valid dates and filter future dates from sysdate, which are in valid format.
I write the function as below to check for valid dates:
create or replace FUNCTION VALIDATE_DATE (p_string in string) return date is
begin
return to_date(p_string, 'YYYYMMDD');
exception when others then
begin
return to_date(p_string, 'YYYY-MM-DD');
exception when others then
begin
return to_date(p_string, 'RR-MON-DD');
exception when others then
return null;
end;
end;
end;
and written this query to check for valid dates and replace with null for invalid dates
select ymd, VALIDATE_DATE(ymd) as Valid
from temp
and to check future dates I wrote the following query, but it throws error
ORA-01839
select ymd
from temp
where validate_date(ymd)='YES'
and to_date(ymd,'yyyymmdd')>sysdate
How to check future dates in my table if exists?
I will rather fix the design issue as a permanent fix rather than wasting time on the workaround.
Firstly, NEVER store DATE as VARCHAR2. All this overhead is due to the fact that your design is flawed.
'20100231'
How on earth could that be a valid date? Which calendar has a 31 days in FEBRUARY?
Follow these steps:
Add a new column with DATE DATA TYPE.
Update the new column with date values from the old column using TO_DATE.
Do the required DATE arithmetic on the new DATE column, or handle this in the UPDATE statement in step 2 itself.
Drop the old column.
Rename the new column to the old column.
UPDATE Adding a demo
Setup
SQL> CREATE TABLE t
2 (ymd varchar2(8));
Table created.
SQL>
SQL> INSERT ALL
2 INTO t (ymd)
3 VALUES ('20101112')
4 --INTO t (ymd)
5 -- VALUES ('20100231')
6 INTO t (ymd)
7 VALUES ('20150101')
8 INTO t (ymd)
9 VALUES ('20160101')
10 SELECT * FROM dual;
3 rows created.
SQL>
SQL> COMMIT;
Commit complete.
SQL>
Add new column:
SQL> ALTER TABLE t ADD (dt DATE);
Table altered.
SQL>
DO the required update
SQL> UPDATE t
2 SET dt =
3 CASE
4 WHEN to_date(ymd, 'YYYYMMDD') > SYSDATE
5 THEN NULL
6 ELSE to_date(ymd, 'YYYYMMDD')
7 END;
3 rows updated.
SQL>
SQL> COMMIT;
Commit complete.
SQL>
Let's check:
SQL> SELECT * FROM t;
YMD DT
-------- ---------
20101112 12-NOV-10
20150101 01-JAN-15
20160101
SQL>
Drop the old column:
SQL> ALTER TABLE t DROP COLUMN ymd;
Table altered.
SQL>
Rename the new column to old column name
SQL> ALTER TABLE t RENAME COLUMN dt TO ymd;
Table altered.
SQL>
You have just fixed the issue
SQL> SELECT * FROM t;
YMD
---------
12-NOV-10
01-JAN-15
SQL>