SQL Developer showing a weird data format in output - oracle

I tried to run a simple query which shows a weird date output format. When i try to run the below query, I get no value as output.
select value from SYS.nls_database_parameters where parameter = 'NLS_date_format';
please help.

See the difference? Your query first, mine second.
SQL> select value from SYS.nls_database_parameters where parameter = 'NLS_date_format';
no rows selected
SQL> select value from SYS.nls_database_parameters where parameter = 'NLS_DATE_FORMAT';
VALUE
--------------------------------------------------------------------------------
DD-MON-RR
SQL>
Parameters' names are UPPERCASE, e.g.
SQL> select parameter from nls_database_parameters where rownum < 5;
PARAMETER
------------------------------
NLS_LANGUAGE
NLS_TERRITORY
NLS_CURRENCY
NLS_ISO_CURRENCY
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')

Default milliseconds to 0 in 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>

TO_DATE function in ORACLE

I was trying the TO_DATE function. Specifically, I noted that the following queries
1. SELECT TO_CHAR(TO_DATE('01-01-2015','DD-MM-YYYY'),'DD-MON-YY') FROM DUAL
2. SELECT TO_DATE('01-01-2015','DD-MM-YYYY') FROM DUAL
have the same output: 01-JAN-2015.
Why does the TO_DATE function return the month in its abbreviated form?
My expected output for the second query is something like 01-01-2015 (simply, a TYPE conversion, NOT a format conversion).
Am I wrong?
Thanks
Dates do not have a format - they are represented by 7- or 8-bytes.
SELECT DUMP( SYSDATE ) FROM DUAL;
Might output:
Typ=13 Len=8: 220,7,11,26,16,41,9,0
This format is very useful for computers to compare dates but not so useful to people; so, when the SQL client (SQL/plus, SQL Developers, TOAD, etc) displays a date it does not display the the bytes but displays it as a string.
It does this by making an implicit call to TO_CHAR() (or some other internal method of stringifying dates) and uses a default format mask to perform this conversion.
SQL/Plus and SQL Developer will use the user's session parameter NLS_DATE_FORMAT to perform this conversion - see this answer regarding this.
So your second query is implicitly being converted to do something approaching this (but, almost certainly, more efficiently):
SELECT TO_CHAR(
TO_DATE('01-01-2015','DD-MM-YYYY'),
( SELECT VALUE FROM NLS_SESSION_PARAMETERS WHERE PARAMETER = 'NLS_DATE_FORMAT' )
)
FROM DUAL
The default output format of DATE value, resp TO_DATE() function is set by NLS_DATE_FORMAT value. You can verify it with this query:
SELECT *
FROM V$NLS_PARAMETERS
WHERE PARAMETER = NLS_DATE_FORMAT';
You can change it on session level for example with
alter session set NLS_DATE_FORMAT = 'DD-MM-YYYY';
The output format of TO_CHAR is not correct, try:
SELECT TO_CHAR(TO_DATE('01-01-2015','DD-MM-YYYY'),'DD-MM-YYYY') FROM DUAL;
Oracle TO_DATE: is used to convert a character string to a date format.
and related to your concern; you need to alter your session like below:
alter session set nls_date_format='DD-MM-YYYY'
in your apps right after the connect.
So now if you run again your query :
SELECT TO_DATE ('01-01-2015', 'DD-MM-YYYY')
FROM DUAL;
the result would be as expected:
01-01-2015
Hope that will help.

Inserting date as dd-mon-yy in oracle

I am trying to insert date value as dd-mon-yy but the database is storing it as dd/mm/yy.
For example: INSERT INTO tablename VALUES (to_date('01-mar-09', 'dd-mon-yy');
The DB stores it as 01/03/09. Why is that? Whay can't I just store it as 01-mar-09?
Please help.
Date is a raw datatype. It doesn't have a format to be saved. It's upto you or the environment/session to decide how to display it.. use NLS_DATE_FORMAT parameter as required... using alter session or use to_char() function
alter session set nls_date_format = 'DD-MON-YY';
select sysdate from dual;
28-OCT-15
alter session set nls_date_format = 'MM/DD/YY';
select sysdate from dual;
10/28/15
The NLS parameters precedence is decided as below, if not set on session level then use instance level, if not set at instance level then use which is present at database level. Below are the views which provide set values at each level
NLS_SESSION_PARAMETERS => session level parameters
NLS_INSTANCE_PARAMETERS => instance level parameters
NLS_DATABASE_PARAMETERS => database level parameters
Oracle doesn't store a date as any format .. it just stores "a date". you are viewing it/displaying it as a different format. Check your nls_date settings.
SQL> select sysdate from dual;
SYSDATE
---------
28-OCT-15
SQL> alter session set nls_date_format='dd-mon-yyyy hh24:mi:ss'
Session altered.
SQL> select sysdate from dual;
SYSDATE
--------------------
28-oct-2015 11:00:56
SQL>

Oracle: how to "unalter" a session parameter?

I'm doing a case-insensitive query with
alter session set NLS_COMP=LINGUISTIC;
alter session set NLS_SORT=BINARY_CI;
Is there a way to easily capture the session state prior to altering it so that I can restore the session to its original state?
You can obtain the current values using:
select *
from nls_session_parameters;
before you change your session. To restore it, you just use the saved values.
I am not aware of any statement that resets the session parameters to the default value.
The NLS parameters are exposed through a series of views, starting with NLS_. In your case you need NLS_SESSION_PARAMETERS. There are equivalent views for Instance and Database.
This is neater than using v$parameter, although that view does allow us to tell whether a paarmeter is changed from the default value.
You can get the value of a given session parameter by:
SELECT value
FROM nls_session_parameters
WHERE parameter = 'NLS_SORT'; -- replace NLS_SORT with parameter of your choice
This answer demonstrates other means of doing a case insensitive search.
Using UPPER()/LOWER() functions with a function based index.
Regular expressions: REGEXP_LIKE()
You can see the parameter values initially :
SQL> SHOW PARAMETER NLS_SORT;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
nls_sort string BINARY
SQL> SHOW PARAMETER NLS_COMP;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
nls_comp string BINARY
And then set the session parameter accordingly :
SQL> alter session set nls_comp='LINGUISTIC';
Session altered
SQL> alter session set nls_sort='BINARY_CI';
Session altered
In PL/SQL, you can do the following to fetch the parameter value and store it in session variable :
SQL> DECLARE
2 VAR_NLS_SORT VARCHAR2(10);
3 var_nls_comp VARCHAR2(10);
4 BEGIN
5 SELECT VALUE
6 INTO VAR_NLS_SORT
7 FROM NLS_SESSION_PARAMETERS
8 WHERE PARAMETER = 'NLS_SORT';
9 SELECT VALUE
10 INTO VAR_NLS_COMP
11 FROM NLS_SESSION_PARAMETERS
12 WHERE PARAMETER = 'NLS_COMP';
13 DBMS_OUTPUT.PUT_LINE('NLS_SORT = '||VAR_NLS_SORT);
14 DBMS_OUTPUT.PUT_LINE('NLS_COMP = '||VAR_NLS_COMP);
15 END;
16 /
NLS_SORT = BINARY
NLS_COMP = BINARY
PL/SQL procedure successfully completed.
For more information, you can have a look at Oracle – Case Insensitive Sorts & Compares

Resources