PL/SQL oracle, test window using date and time - oracle

I have a function that returns a date (actual a timestamp, date+time).
In the table it looks like this, "2010-09-05 17:33:00".
when i call my function from an sql script like,
select my_package.my_function('2010-09-05')
from dual.
I'll get time in the result.
But if I run the same in a test window I'll only receive the date and not the time.
How could this be?

you are seeing the effect of the NLS_DATE_FORMAT setting. set that as required. eg:
alter session set NLS_DATE_FORMAT="yyyy-mm-dd hh24:mi:ss";
select my_package.my_function('2010-09-05') from dual;
to see the output properly.
or TO_CHAR the output.
select to_char(my_package.my_function('2010-09-05'), 'yyyy-mm-dd hh24:mi:ss') from dual;

What do you mean by test window?
If you mean SQL+ then there will be a format applied, you can change this using
alter session set nls_date_format='DD/MM/YYYY HH24:MI:SS';
Do this at the start of your SQL+ session.

when i simply write ALTER .... then i get error working in procedure. However, i added below script, it worked for me.
execute immediate 'ALTER SESSION SET NLS_DATE_FORMAT = ''DD/MM/YYYY HH24:MI:SS''';

Related

Setting NLS_DATE_FORMAT on Adminer

I am using Adminer 4.7.3 connecting to an oracle DB and I want to display dates in a different format from what the default is ('DD-MON-YY'). I tried using the command
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH:MI:SS' but it only lasts for that query, and it goes back to the old date format once I open the table again. Is there a better way to force the date to show up in a different format?
You have a few options. First, you can perform an
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH:MI:SS'
every time you log on. If you don't want to do this you can:
If you're using *nix:
setenv NLS_DATE_FORMAT "dd-mon-yyyy hh:mi:ss"
If you're using Windows:
Control Panel-System-Advanced System Settings-Environment Variables, and create the NLS_DATE_FORMAT environment variable with the appropriate setting.
If you want an entirely database-dependent solution, you can use an ON LOGON trigger similar to the following:
CREATE OR REPLACE TRIGGER DATABASE_AFTER_LOGON
AFTER LOGON
ON DATABASE
BEGIN
EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_DATE_FORMAT = ''DD-MON-YYYY HH:MI:SS''';
END DATABASE_AFTER_LOGON;

Can't permanently change SYSDATE Format

I want to change the date format of Oracle
Now it's DD/MM/YYYY, i want to change it to MM/DD/YYYY
I used a simple code (thank you google)
ALTER SESSION SET NLS_DATE_FORMAT = 'MM/DD/YYYY HH24:MI';
select sysdate from dual;
The problem is when I disconnect and reconnect, the changes are cancelled,
Any help ?
The main of the change is to let Oracle accept insert date with the format MM/DD/YYYY
Thanks in advance
As you see in your command you're altering session. So after reconnect you have new session that gets server settings. For better understanding please read:
https://docs.oracle.com/cd/A87860_01/doc/server.817/a76966/ch2.htm
I would recommend to insert dates always with to_date(data_string,'MM/DD/YYYY HH24:MI') and not trust that NLS won't be changed.
If you're using SQL*Plus, you can add the command to set the nls_date_format in the glogin.sql or login.sql files - see this helpful guide.
However, if your requirement is "to let Oracle accept insert date with the format MM/DD/YYYY", why not simply convert the string into a date when you insert it, e.g.:
insert into some_table (id, date_col)
values (1, to_date('10/25/2016', 'mm/dd/yyyy'));
That way, you don't need to worry about formatting issues; it'll automatically be in the correct format.
You can set this format also in your Registry at
HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\KEY_{Oracle Home Name}\NLS_DATE_FORMAT,
resp.
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ORACLE\KEY_{Oracle Home Name}\NLS_DATE_FORMAT
Example to do this from command line:
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\KEY_OraClient11g_home1" /v NLS_DATE_FORMAT /t REG_SZ /d "MM/DD/YYYY HH24:MI" /f
Another possibility is to set the value as Environment Variable, e.g.:
set NLS_DATE_FORMAT="MM/DD/YYYY HH24:MI"
Note, the Environment Variable takes precedence over Registry settings.
But be aware, these just define the default value of NLS_DATE_FORMAT. It can be changed by ALTER SESSION SET NLS_DATE_FORMAT = ... command at any time.
Do ALTER SYSTEM instead of SESSION:
ALTER SYSTEM SET NLS_DATE_FORMAT='MM/DD/YYYY' scope=both;
or:
ALTER SYSTEM SET NLS_DATE_FORMAT='MM/DD/YYYY';
More info here.

How to change Number format in oracle?

I have a requirement to globalize the application based on culture specified in config file.
I have Amounts and date values .I decided to convert them in query itself.Like below
For Spanish Chile Es-CL:
for Date format is "dd-mm-yyyy" I will use Select TO_CHAR(busdate,'dd-mm-yyyy') from itemTable.
Similarly i want to use same for the amounts Select to_char(Amount,'99.999.99,00') from table1.
But its in vain.
Please suggest me the right way to achive my requirement.
These amounts and date formats can be available.but need to know how to convert them.
I thought to use in On_Data_bound event for datagrids ,but still this will be a performance issue.
You would use the NLS_NUMERIC_CHARACTERS session variable, along with globalized format model:
SQL> alter session set NLS_NUMERIC_CHARACTERS='.,';
Session altered
SQL> select to_char(123456789.01, 'fm999G999G990D00') from dual;
TO_CHAR(123456789.01,
---------------------
123,456,789.01
SQL> alter session set NLS_NUMERIC_CHARACTERS=', ';
Session altered
SQL> select to_char(123456789.01, 'fm999G999G990D00') from dual;
TO_CHAR(123456789.01,
---------------------
123 456 789,01

Getting all data between certain date time

I am reading data from remote oracle database with read only access. and I want to query all data between some time frame. I actually wanted to query between mid-night and current time. so the query I was using :
TO_DATE(to_char(sysdate, 'MM-dd-yyyy')||'00:00:00','MM-dd-yyyy HH24:MI:SS' )
AND
TO_DATE(to_char(sysdate, 'MM-dd-yyyy HH24:MI:SS'),'MM-dd-yyyy HH24:MI:SS' )
But the query
select TO_DATE(to_char(sysdate, 'MM-dd-yyyy HH24:MI:SS'),'MM-dd-yyyy HH24:MI:SS' ) from dual
is returning only 18-APR-12 not the time. How do I get time too?
I am running :
ALTER SESSION SET NLS_DATE_FORMAT='DD-MON-YYYY HH24:MI:SS';
But I don't want to run this everytime any other way to overcome this problem?
What you see is not what is really there.
Oracle holds the date as a number represanting the date and the way you see it when you query it depends on your NLS parameters.
The fact that you don't see the time doesn't mean there is no time.
as for your query I'd do something like this:
... BETWEEN trunc(sysdate) AND sysdate
I see no use in casting a Date to a string and then back to a Date with the same format ...

How to populate a timestamp field with current timestamp using Oracle Sql Loader

I'm reading a pipe delimited file with SQL Loader and want to populate a LAST_UPDATED field in the table I am populating. My Control File looks like this:
LOAD DATA
INFILE SampleFile.dat
REPLACE
INTO TABLE contact
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
(
ID,
FIRST_NAME,
LAST_NAME,
EMAIL,
DEPARTMENT_ID,
LAST_UPDATED SYSTIMESTAMP
)
For the LAST_UPDATED field I've tried SYSTIMESTAMP and CURRENT_TIMESTAMP and neither work. SYSDATE however works fine but doesn't give me the time of day.
I am brand new to SQL Loader so I really know very little about what it is or isn't capable of. Thanks.
Have you tried the following:
CURRENT_TIMESTAMP [ (precision) ]
select current_timestamp(3) from dual;
CURRENT_TIMESTAMP(3)
-----------------------------
10-JUL-04 19.11.12.686 +01:00
To do this in SQLLDR, you will need to use EXPRESSION in the CTL file so that SQLLDR knows to treat the call as SQL.
Replace:
LAST_UPDATED SYSTIMESTAMP
with:
LAST_UPDATED EXPRESSION "current_timestamp(3)"
I accepted RC's answer because ultimately he answered what I was asking but my unfamiliarity with some of Oracle's tools led me to make this more difficult than it needed to be.
I was trying to get SQL*Loader to record a timestamp instead of just a date. When I used SYSDATE, and then did a select on the table it was only listing the the date (05-AUG-09).
Then, I tried RC's method (in the comments) and it worked. However, still, when I did a select on the table I got the same date format. Then it occurred to me it could just be truncating the remainder for display purposes. So then I did a:
select TO_CHAR(LAST_UPDATED,'MMDDYYYY:HH24:MI:SS') from contact;
And it then displayed everything. Then I went back to the control file and changed it back to SYSDATE and ran the same query and sure enough, the HH:MI:SS was there and accurate.
This is all being done in SqlDeveloper. I don't know why it defaults to this behavior. Also what threw me off are the following two statements in sqldeveloper.
SELECT CURRENT_TIMESTAMP FROM DUAL; //returns a full date and time
SELECT SYSDATE FROM DUAL; // returns only a date
If you want to use the table defined default you can use:
ROWDATE EXPRESSION "DEFAULT"
In Sql Developer run:
ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'
and then check it with
SELECT SYSDATE FROM DUAL

Resources