Informix timestamps in Oracle (via ODBC) - oracle

I need to select some values from an Informix database via Oracle ODBC. One of the columns is a timestamp, and when I just select it all I see in SQL*Plus is the date value. How do I get the time as well?

By default SQL*Plus will display the date in the format specified by the NLS_DATE_FORMAT system parameter (client side). You can alter this behaviour by setting the NLS_DATE_FORMAT appropriately. You can also explicitly display the time data:
SQL> select sysdate from dual;
SYSDATE
-----------
05/10/2009
SQL> select to_char(sysdate, 'hh24:mi') from dual;
TO_CHAR(SYSDATE,'HH24:MI')
--------------------------
13:55

Related

Toad SQL Issue for Timestamp without Seconds

I am executing the following sql in Toad. Oracle is RDBMS
I only need Date in yyyymmdd HH24:mi, but I get Date only as shown below
alter session set nls_date_format = 'DD/MM/YYYY HH24:MI';
SELECT to_date('22/07/1980 00:00','dd/mm/yyyy hh24:mi') dt FROM dual
22/07/1980
Required Output
22/07/1980 00:00
You are looking for to_char() -- you want to return the date as a string, not a date. As far as I know, the date is returned without the time, and I don't think the NLS changes that.
So:
SELECT to_char(to_date('22/07/1980 00:00', 'dd/mm/yyyy hh24:mi'
), 'DD/MM/YYYY HH24:MI'
) as dt
FROM dual
I have used sql plus and spooled the file instead of Toad, used NLS Date format conversion here, but as this is command based, wanted to use GUI based TOAD.

How to create DATE datatype in a create query in oracle database?

Please explain how to write a create query where i can write a spcific date in a specific format (suppose dd/MM/yy) in oracle. Suppose i need my columns in my table ORDERS to be-
order_id, order_date, quantity
From the documentation:
"The database stores dates internally as numbers. Dates are stored in
fixed-length fields of 7 bytes each, corresponding to century, year,
month, day, hour, minute, and second."
And what that looks like:
SQL> select dump(sysdate) from dual
2 /
DUMP(SYSDATE)
--------------------------------------------------------------------------------
Typ=13 Len=8: 222,7,9,7,2,48,32,0
SQL>
Which actually looks like eight bytes but interestingly a date is nine bytes long:
SQL> select lengthb(sysdate) from dual
2 /
LENGTHB(SYSDATE)
----------------
9
SQL>
Anyway, storage is fixed and entirely independent of the displaying of dates.
The default date format is governed by the NLS_DATETIME_FORMAT, which is defaulted by the NLS_TERRITORY setting. This is how Oracle determines Currencym, number, formats, days of the week and so on. Find out more by reading the Globalization Support guide.
If you want a different default format mask for your dates this can be set at the database level. This is a big decision. Fortunately it can also be set at a more granular level:
SQL> select sysdate from dual
2 /
SYSDATE
---------
07-SEP-14
SQL> alter session set nls_date_format='Month DD YYYY HH12:MI AM'
2 /
Session altered.
SQL> select sysdate from dual
2 /
SYSDATE
--------------------------
September 07 2014 03:05 AM
SQL>
As far as input of dates goes, Oracle expects strings containing dates to have the same format as that specified by the NLS_DATE_FORMAT. If this is not the case then we have to apply a conversion using the TO_DATE() function and supplying the mask of the string:
SQL> select to_date('31/05/14','DD/MM/YY') from dual
2 /
TO_DATE('31/05/14','DD/MM/
--------------------------
May 31 2014 12:00 AM
SQL>

Oracle - Timestamp

When importing columns of type "Timestamp" to an Oracle DB:
Import tbl:
02.09.13 00:00:00
After importing:
Oracle tbl:
02.09.13 08:23:44,000000000
In the Oracle tbl the type is also "Timestamp".
How can I remove the ",000000000"?
The TIMESTAMP datatype includes fractional seconds; there is no way to remove them within the database whilst maintaining the datatype. If you don't want fractional seconds then put the date into a DATE column.
It shouldn't matter whether the fractional seconds are stored or not. When selecting from the database into anything other than another date datatype you should format the date as required by the client displaying it. The normal method of doing this would be by using the function TO_CHAR(); for instance:
select to_char(column_name, 'dd.mm.yy hh24:mi:ss') from table_name
You can also do this at a session level by changing your NLS settings.
Unrelated to your question, but to address the comment on a comma being part of your timestamp; the default date format is determined by NLS_TERRITORY. A comma is a perfectly valid character to have here. Altering the NLS_TERRITORY, for example to France, it will appear as part of the NLS_DATE_FORMAT:
SQL> alter session set nls_territory = 'FRANCE';
Session altered.
SQL> select systimestamp from dual
2 ;
SYSTIMESTAMP
------------------------------------------------------------
18/09/13 13:09:54,418387 +01:00

Change Date values using PL/SQL to mm/dd/yyyy

Have some dates in my local Oracle 11g database that are in this format:
01-JUL-85
How do I change it to this format (mm/dd/yyyy) using PL/SQL:
7/01/1985
With thanks...
If the column is a date datatype then it is only your NLS_DATE settings that are causing them to be displayed in the format DD-MON-YYYY.
To check your current NLS_DATE format run the following:
SELECT value
FROM V$NLS_Parameters
WHERE parameter ='NLS_DATE_FORMAT';
Oracle stores all dates in an internal binary format and uses the NLS_DATE format to display them (unless explicitly told to display them differently).
You can either alter your NLS_DATE settings to MM/DD/YYYY or TO_CHAR the date column using:
TO_CHAR(<date_column>, 'MM/DD/YYYY')
to see the format you require.
You can alter the NLS_DATE format for your current session or alter the database parameters to change the default NLS_DATE format for the database itself.
If the column is a VARCHAR2 type then you'll need to convert to a date first and then you can format the output using either of the methods described above.
See: http://ss64.com/ora/syntax-nls.html
and: http://www.dba-oracle.com/t_nls_date_format_sysdate.htm
e.g.
SELECT TO_CHAR(sysdate, 'MM/DD/YYYY') as current_date
FROM dual;
or
ALTER SESSION SET NLS_DATE_FORMAT = 'MM/DD/YYYY';
SELECT sysdate
FROM dual;
In pure PL/SQL
DECLARE
v_date DATE := sysdate;
BEGIN
DBMS_OUTPUT.put_line(TO_CHAR(v_date, 'MM/DD/YYYY'));
END;

timestamp locale in hibernate using oracle

I have an entity class which has a timestamp property.
If I save an entity instance in oracle, the corresponding timestamp column will be written into Chinese format(I am using oracle 10g with Chinese locale).
22-8月 -11 07.04.03.926000 δΈ‹εˆ is saved
But I want 2011-08-22 19:04:03.926
How can change column definition or database locale setting?
A timestamp in Oracle is a point in time. As such, there is no format stored with the data. When you retrieve data from a timestamp column, it is displayed by default in the format specified by your NLS_TIMESTAMP_FORMAT or NLS_TIMESTAMP_TZ_FORMAT session variable.
You can always use a specific format with to_char:
SQL> SELECT to_char(systimestamp, 'yyyy-mm-dd hh24:mi:ss.ff3') my_ts FROM dual;
MY_TS
-----------------------------
2011-08-22 14:38:48.351
You could also set a new default for your session with:
SQL> ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT='yyyy-mm-dd hh24:mi:ss.ff3';
Session altered
SQL> select systimestamp from dual;
SYSTIMESTAMP
-------------------------------------------------
2011-08-22 14:42:23.776

Resources