Force oracle sql developer to show timestamp - oracle

I am new to SQL developer but each time i get queries it changes time stamp to readable date format, but i want my result to be in time stamps as they are in database.

Please go to: Tools/Preferences/Database/NLS and set there date format and timestamp format to what you want.
Oracle stores date and timestamp type but display it according to NLS settings. You either call alter session set NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH:MI:SS.FF'; or just set those NLS settings in your client tool.
Documentation

Related

How to format date fields using select query with Oracle

I am new to node red.
I am storing into the Oracle db in this date format dd-mm-yy hh:mi:ss.ff PM but I am getting a payload using a select query in this format yyyy-mm-ddThh:mi:ss.000Z but while retrieving data from Oracle db I want to print this format dd-mm-yyyy hh:mm:ss in my payload, how to write select query so that I can print the same date format,whatever stored in Oracle db
When you fetch a date value, it is stringified according to the NLS settings of your client. Looking at the same data with two different clients, you might see two different representations... of the same date.
If you want to choose the display format of a date, use Oracle function TO_CHAR in your query. It accepts a date and a format spec, and returns a string :
TO_CHAR(my_date_column, 'dd-mm-yyyy hh24:mi:ss')
It is also possible to change the default date format for the life time of your session, like :
ALTER SESSION SET NLS_DATE_FORMAT = 'dd-mm-yyyy hh24:mi:ss';
NB : if you are dealing with timestamps or timestamps with time zone, you need NLS_TIMESTAMP_FORMAT or NLS_TIMESTAMP_TZ_FORMAT.

Date formats are displaying differently in Oracle

I am exporting the reports data from DB to excel. I am having 2 DB(Local & QA) schemas and having same queries for reports. Here I am facing an issue with date formats. we are using Oracle 12c.
Date Format should be 'DD-MON-YY' like '01-AUG-18'
While exporting data to excel,
from local db, it exporting date like '12-09-18'.
from QA db, it exporting date like '12-AUG-18'.
Query is :
select trim(to_date('01-01-1970 00','DD-MM-YYYY hh24') + (createdtime)/1000/60/60/24)
from TBL_RESPONSESUMMARY;
Can any one guide me please.
Use TO_CHAR to explicitly specify the format model you want to use to output the date:
SELECT TO_CHAR(DATE '1970-01-01' + createdtime/1000/60/60/24, 'DD-MON-YYYY')
FROM TBL_RESPONSESUMMARY;
What your query does is:
to_date('01-01-1970 00','DD-MM-YYYY hh24') generates your epoch value as a DATE data type;
+ (createdtime)/1000/60/60/24 adds your milliseconds offset value to it (and the data type will remain as a DATE);
trim( string_value ) takes a string value and removes leading and trailing white space; however, your input value is a DATE not a string so Oracle must perform an implicit cast using TO_CHAR and it uses the NLS_DATE_FORMAT session parameter. On your local database this is probably DD-MM-RR but on the QA database it is DD-MON-RR so you get different values.
You could change the NLS_DATE_FORMAT so that it is consistent on both databases - however, it is better to use an explicit call to TO_CHAR so you are not relying on session variables and implicit casts.
You have different date format between two databases. Run this command on local database to change date format
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-Mon-YY';

Oracle change dbtimezone

My database was configured with an dbtimezone=+2:00:
When my application sends a date which has a timezone, does Oracle automatically translate the date to its dbtimezone and store it in the column?
When my application asks for a field date, does Oracle automatically translate it to the application timezone?
In order to be consistency with business rules, I wanted to change this dbtimezone to UTC. So I made the alter database set time_zone='UTC' command, I restarted the oracle server and now the select dbtimezone from dual; command returns "UTC".
But, all fields date in DB haven't changed (no change -2 hours from GMT+2 to UTC). When I ask the sysdate, it returns the GMT+2 date ... I try to change my SQL Developer configuration timezone to UTC but it didn't change anything. Do I have an issue of Oracle session parameters that convert my DB data to GMT+2 before displaying it ?
Finally, does anyone have a good practice to make this change ? (change the database timezone and existing date to a new timezone).
If all you're doing is changing the database time zone setting, then you are only going to notice any change in output if your data is stored with the TIMESTAMP WITH LOCAL TIME ZONE type.
I don't recommend that though. It would be much better if your data was just stored in a regular TIMESTAMP field and was already set to UTC.
You should read the documentation about all of the different date and time datatypes, so you understand how each of these types works and differs from the other.

sql developer time format

I have to add a column into my DB oracle SQL DEVELOPER.
This column needs the format HH:MI:SS, but when I set the column as timestamp,
it convert me the column as : DD-MM-YY HH:MI:SS, even if I change NLS Settings.
You can change the preferences as follows:
Go to Tools > Preferences.
In Preferences dialog, select Database > NLS from the left panel.
From the list of NLS parameters, enter HH:MI:SS into the Timestamp Format field.
Save and close the dialog.
The actual value saved in the db in not even that. What is saved in the database is a number that represents the date. What you actually see is how the SQL Developer represents that date, and not how it is actually stored.
You can print the date in the format you want using to_char() :
select to_char(sysdate, 'HH:MI:SS') as "Current Time" from dual;

Unable to insert date and time when using date datatype

I am hitting a bit of a problem when using the date datatype. When trying to save a row to the table where the field it throws an error ora 01830 and complains about converting the date format picture ends...etc. Now when I do the insert, I use the to_date function with the format of "dd-mon-yyyy hh24:mi:ss". Of course, when I remove the time element, everything is perfect.
Checking sysdate, I noticed that the time element wasn't be displayed, and I used alter session set nls_date_format to set the date and time I want to save to the table, which worked!
I used alter system set nls_date_format ="dd-mon-yyyy hh24:mi:ss" scope=spfile; This showed that it was altered, and I can see the setting in the enterprise management console. In sqlplus, I shutdown the database, and restarted with startup mount; alter database open; and then selecting sysdate, it still shows the date as dd-mon-yy, and still no time! Checking the enterprise management, and looking up the nls_date_format the setting is still shown as "dd-mon-yyyy hh24:mi:ss".
So, my question is this - what am I doing wrong? Why can't save date and time using date in Oracle 11g?????
Thanks
Dates are stored with "second" granularity in Oracle.
Display formats are dependent on the system and session. In your case, since you are connecting with sqlplus, you are using a default session format from the client that does not include time. You need to execute an:
ALTER SESSION SET nls_date_format ="dd-mon-yyyy hh24:mi:ss";
when you start up your sqlplus client in order to change the default display. There is a client side file (glogin.sql?) that sqlplus will run on startup. You can place this kind of command in there if you want it to be executed each you start that client. I'm pretty sure the sqlplus client sends an "alter session set nls_date..." on start up.
In general, when outputting dates, I think it is better to just be explicit on the format by doing a TO_CHAR(myDateColumn, "dd-mon-yyyy hh24:mi:ss"). If you are reading dates programatically, you don't need to worry about it since you are dealing with internal formats, not display formats.
I've seen this error when the input data did not match the date format used. check your, data would be my suggestion.

Resources