Default milliseconds to 0 in Oracle - oracle

I have a timestamp column in Oracle that has format 'MM/DD/YYYY HH24:MI.SxFF6'.
The data looks like below:
11/09/1917 10:45:28.230000
10/19/2014 18:09:28.410000
12/19/2011 11:06:28.340000
I need the timestamp to retain the value except for getting the milliseconds which need to be defaulted to 000000.
I tried query -
cast(to_char(Local_time, 'MM/DD/YYYY HH24:MI:SS') as timestamp(6))
But it is throwing error - "Not valid month"
Does anyone have any ideas on what I can try to get milliseconds to 0. I use Toad to query the table.

Your TIMESTAMP value does not have any format. All you have is a default display format - defined by current user NLS_TIMESTAMP_FORMAT setting.
Try this one:
CAST(Local_time AS TIMESTAMP(0))
If you like to trunc the milliseconds but haven them still available use
CAST(CAST(Local_time AS TIMESTAMP(0)) AS TIMESTAMP(6))

Something like this, perhaps?
SQL> create table test (col timestamp, result timestamp);
Table created.
SQL> insert into test (col) values (to_timestamp('11/09/1917 15:45:28.230000', 'MM/DD/YYYY HH24:MI:SS.FF6'));
1 row created.
SQL> update test set result = cast(col as date);
1 row updated.
SQL> select * From test;
COL RESULT
------------------------- -------------------------
09.11.17 15:45:28,230000 09.11.17 15:45:28,000000
SQL>

Related

How to update year only in date format dd-MON-yyyy use Oracle sql plus

DOB data type : DATE
13-JAN-76
10-FEB-80
17-MAR-79
---------------
Expected output
13-JAN-04
10-FEB-04
17-MAR-04
I tried use this but failed.
update table set dob=to_date(dob,'dd-MON-yyyy')||','||'2004','dd-MON-yyyy') where id='1001';
date format not recognized.
anyone help is much appreciated.
Here's one option:
(just to know date format; you don't have to do that)
SQL> alter session set nls_date_format = 'dd.mm.yyyy';
Session altered.
Here it goes:
SQL> select * from test;
DOB
----------
13.01.1976
10.02.1980
17.03.1979
SQL> update test set
2 dob = add_months(dob, (2004 - extract(year from dob)) * 12);
3 rows updated.
SQL> select * from test;
DOB
----------
13.01.2004
10.02.2004
17.03.2004
SQL>
You update statement relies on the session default date format. Typically this is defined by session parameter NLS_DATE_FORMAT
Better specify it explicitly:
update table set dob = to_date(2004 || TO_CHAR('dob', '-MM-DD'), 'YYYY-MM-DD')

How to save the value of one variable to another variable in Oracle APEX?

I have two items in Oracle APEX and I want to make one item to have by default part of value of another item. I have tried by using SQL function TRIM:
SELECT TRIM('HH24:MI ' FROM TIME_AND_DATE) AS DATE FROM BILL;
TIME_AND_DATE - (format mask: DD-MM-YYYY HH24:MI)
DATE - (format mask: DD-MM-YYYY)
But it returns an error.
If TIME_AND_DATE columns datatype is DATE and you want to put only date part of it into a variable, then TRUNC it (not TRIM).
SQL> select sysdate date_and_time,
2 trunc(sysdate) truncated
3 from dual;
DATE_AND_TIME TRUNCATED
------------------- -------------------
15.05.2020 15:07:12 15.05.2020 00:00:00
SQL>
Next step is just the presentation; use TO_CHAR (or format mask in front end (Apex?)) to display it as you want, e.g.
SQL> select to_char(trunc(sysdate), 'dd-fmmonth-yyyy') result from dual;
RESULT
--------------------
15-svibanj-2020
SQL>

What is the best data type for the field of format "YYYY-MM-DD" in oracle 11g?

I'm creating tables in Oracle 11g table and came across one date field of format "YYYY-MM-DD".
I don't want to use varchar2 for this and when I use number(5), it's still accepting the input. Then what's the meaning of limit 5 here?
Please suggest me the best datatype I can use here.
This is, obviously, a date format mask. If you're about to store dates into that column, you should use the DATE datatype, such as
SQL> create table test
2 (datum date);
Table created.
Don't use VARCHAR2 (put strings into it, not dates) nor NUMBER (put numbers into it, not dates) datatypes for that. You'll regret it sooner than you think.
I'm going to enter some values into the table, showing different ways of how you could do that - it is important that you insert dates, not strings into it. Never rely on Oracle, implicitly converting strings you might provide to dates. Sooner or later, it'll produce an error.
SQL> insert into test values (date '2018-12-25');
1 row created.
SQL> insert into test values (to_date('09.05.2018', 'dd.mm.yyyy'));
1 row created.
SQL> insert into test values (sysdate);
1 row created.
Now, several ways of selecting that value:
This one returns date in a format currently set by my database's NLS settings:
SQL> select * from test;
DATUM
--------
25.12.18
09.05.18
09.05.18
I'm forcing it to return values in desired format, using ALTER SESSION:
SQL> alter session set nls_date_format = 'yyyy-mm-dd';
Session altered.
SQL> select * from test;
DATUM
----------
2018-12-25
2018-05-09
2018-05-09
Yet another format; note that value inserted via the SYSDATE function (which returns DATE) contains date and time component. It was "invisible" in previous examples:
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select * from test;
DATUM
-------------------
25.12.2018 00:00:00
09.05.2018 00:00:00
09.05.2018 08:03:50
Using TO_CHAR function with some format (such as dd-mon-yyyy). I'm also requesting Oracle to "translate" month name into English (as my database works in Croatian):
SQL> select to_char(datum, 'dd-mon-yyyy', 'nls_date_language = english') datum from test;
DATUM
-----------
25-dec-2018
09-may-2018
09-may-2018
SQL>
[EDIT]
Oracle doesn't store DATE values in any "human" readable format (there's more to read on the Internet, Google for it). It is a format mask that represents that value to you.
I strongly suggest you NOT to store dates into any datatype column but DATE. It's a time bomb, waiting to explode (and then it'll hurt). Nobody stops you from entering a value as '1234-99-66' or '12-345-678'; what will you do with it, then?
Consider creating a view on a top of the table which uses TO_CHAR function and returns the value in a format you want ('yyyy-mm-dd'). DATE datatype column in a table makes sure that values are valid, and the view will let the third-party application to accept values it finds appropriate.
For example:
SQL> create view v_test as
2 select to_char(datum, 'yyyy-mm-dd') datum
3 from test;
View created.
SQL> select * from v_test;
DATUM
----------
2018-12-25
2018-05-09
2018-05-09
SQL>
So: you wouldn't let the third-party application to access the table, but the view instead.

Get current timestamp in specific format with seconds to one decimal place

I'm trying to set a value in an Oracle database that resembles the following:
2016-06-21 03:07:25.0
It is a varchar of the current date with one digit after the decimal for seconds. I tried multiple ways to achieve this but to no avail:
SQL> select sysdate from dual;
SYSDATE
--------
21-06-16
From this I tried:
SQL> alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS.F';
ERROR:
ORA-01821: date format not recognized
SQL> alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS.s';
ERROR:
ORA-01821: date format not recognized
Then I tried:
SQL> SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD HH24:MI:SS') FROM dual;
TO_CHAR(SYSDATE,'YY
-------------------
2016-06-21 12:58:55
Since that worked up to the seconds I tried the decimal part:
SQL> SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD HH24:MI:SS.SSSS') from dual;
TO_CHAR(SYSDATE,'YYYY-MM
------------------------
2016-06-21 13:23:47.4747
So I can get 4 significant places. Now I try with just 1:
SQL> SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD HH24:MI:SS.S') from dual;
SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD HH24:MI:SS.S') from dual
*
ERROR at line 1:
ORA-01821: date format not recognized
So how can I get the current timestamp in the abovementioned format with seconds set to one decimal place?
SSSS isn't fractional seconds. It's just repeating the SS value twice; in your example you get 13:23:47.4747 - the 4747 is just 47 displayed twice. Maybe you were thinking of SSSSS, but that is the number of seconds since midnight (i.e. 0-86399) so isn't useful here either. A single S isn't a valid format model element.
Dates don't have fractional second precision. A single F isn't a valid format model element either, but even FF isn't valid for a date.
You need to use systimestamp to get the server time with fractional seconds, rather than sysdate:
select to_char(systimestamp, 'YYYY-MM-DD HH24:MI:SS.FF1') from dual;
TO_CHAR(SYSTIMESTAMP,'YYYY-MM
-----------------------------
2016-06-21 14:42:22.7
Storing a date/time as a string is generally a bad idea. I'd suggest you make the column timestamp(1) which will keep the one-decimal-place precision when you set it from systimestamp. You can see what it would get with cast:
select to_char(cast(systimestamp as timestamp(1)), 'YYYY-MM-DD HH24:MI:SS.FF9') from dual;
TO_CHAR(CAST(SYSTIMESTAMPASTI
-----------------------------
2016-06-21 14:49:09.900000000
Converting a date/time to a string should really only be done for display, not for storage.

select data based on a date column

I was trying to select some data from my table using the following query:
select * from table1 where column1 = to_date('14-05-14','yy-mm-dd');
Where the column data type is DATE. I observed that, the above query won't return anything unless we modified it as,
select * from table1 where trunc(column1) = to_date('14-05-14','yy-mm-dd');
even though there are records available.
I checked the documentation for TRUNC.Can anyone please explain why this happens?
UPDATE
As per the valuable comments I think some time values may also associated with the DATE. But I cannot view/edit that time. How can I ensure there are time values associated.
Both TO_DATE and TRUNC are different. See the below example.
SQL> ALTER SESSION SET nls_date_format = 'dd/mm/yyyy hh24:mi:ss';
Session altered.
SQL> SELECT TO_DATE(SYSDATE) FROM DUAL;
TO_DATE(SYSDATE)
-------------------
28/05/2014 16:03:25
SQL> SELECT TRUNC(SYSDATE) FROM DUAL;
TRUNC(SYSDATE)
-------------------
28/05/2014 00:00:00
In Your first query to_date('14-05-14','yy-mm-dd') is comparing with the date field column1 in your table which has time values also. Whereas in Your 2nd query You are truncating the time part from table data and from Your query, that's why it's matching.
The DATE datatype stores the year (including the century), the month, the day, the hours, the minutes, and the seconds (after midnight).
TRUNC function will truncate the date to the day value, so that any hours, minutes, or seconds will be truncated off.
For more info please look at these below links
http://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm#CNCPT413
http://www.techonthenet.com/oracle/functions/trunc_date.php

Resources