I am trying to execute a query which has a where clause on a DATE type column against an Oracle DB.
Condition reads like
collectTime BETWEEN TO_DATE('2014-12-14 01:00:00','YYYY-MM-DD HH24:MI:SS') AND
TO_DATE('2014-12-14 03:40:00','YYYY-MM-DD HH24:MI:SS')
Now, if I use the DriverManager getConnection method to get the connection object and execute the prepared statement, the output looks fine. That is, when I look at the returned collectTime column (it comes back as java.sql.Timestamp) values, I see values between the two dates I passed in - example 2014-12-14 01:09:00.
However, if I get the connection object through a JDBC datasource (configured in Weblogic), the column values corresponding to the DATE column come back differently. Example 2014-12-13 17:09:00.0.
I am failing to understand the cause for this difference. All the values seem to get adjusted by a specific timezone probably. Any insights as to why this could happen?
I tried to set the session timezone to UTC but that didn't have any affect.
In your query, format the date to your liking. If you don't, the date will be returned using the NLS_DATE format defined in Weblogic or wherever it's being executed.
The only way to have full control of how the date is returned is to specify it in the query, e.g.
select to_char(sysdate, 'dd-mon-yyyy') from dual;
returns
21-dec-2014
Here's a discussion thread that talks about setting the timezone for Weblogic: link.
It suggests putting the following into the setDomainEnv.cmd file, with your correct timezone offset of course.
set JAVA_PROPERTIES=-Duser.timezone="+05:30" %JAVA_PROPERTIES% %EXTRA_JAVA_PROPERTIES%
Related
The date format in Oracle SQL Developer looks like DD-MON-YY.
For example 17-OCT-03.
The date format in APEX looks like MM/DD/YYYY.
For example 12/17/1980.
Why are they different?
This might cause the same SQL query not to work on both applications.
I know that I can avoid such problem by using TO_DATE and TO_CHAR functions but I want to understand the logic behind this problem.
Does every application use its own default date format?
Yes, every application has it's own date format.
And even every application can have more than one session, each with a different session format specified for DATEs and TIMESTAMPs.
SQL Developer has it's application level settings defined in the Preferences, Database, NLS page. This is how DATEs will appear unless you issue an ALTER SESSION SET... in your SQL Worksheet.
Or, if you always want a specific format regardless of this setting, build it into your query.
select to_char(sysdate, 'DAY') today from dual;
It's been my experience as well... YES, every Oracle application/tool can have its own default date format, but most just use the default display format set at the database level:
SQL> select name, value from v$parameter where name = 'nls_date_format';
NAME VALUE
--------------------- ---------------------
nls_date_format DD-Mon-YY
1 row selected.
But I would not characterize this as "a problem"... it is a valuable feature that provides lots of flexibility in displaying and entering dates in Oracle-based applications.
For sure though, if developers don't understand how Oracle dates work and write code such as this:
-- BAD Coding
DECLARE
ld_holiday_date DATE;
BEGIN
-- Set a date type variable to a string value and
-- hope that Oracle can figure out what I mean.
ld_holiday_date := '01-JAN-2022';
END;
they are writing environment specific code which will definitely not work in another database (or session) that has a different NLS_DATE_FORMAT from the one they wrote their code in. Furthermore, the code above is having to do an implicit data type conversion (from string to date) which is leaving it up to Oracle to try and figure out what format is in the string.
To write more deterministic code with regard to Oracle dates, developers should definitely use the TO_DATE function. Here's the bullet proof version of the code snippet above:
-- Good Coding
DECLARE
ld_holiday_date DATE;
BEGIN
-- I do not care what the NLS date format is in this
-- database... this code will work everywhere.
ld_holiday_date := TO_DATE ('01-JAN-2022', 'DD-MON-YYYY');
END;
I have been searching for a setting in DB BRowser for SQLite on how to change the timezone to IST (Indian Standard Time +5:30) Is there a way to set it directly without running any queries? I also found some SQL queries that can convert the db time to IST but almost all are SELECT statements. I am looking for a setting to change the timezone permanently and if that is not possible then may be an update query which can read all records in the database and change/convert/replace all times to IST. Can someone shed some light on it?
My field name is "expire_time" set as DATETIME NOT NULL in CREATE TABLE
What I searched for was
INSERT INTO MyTable(MyColumn) VALUES(datetime(CURRENT_TIMESTAMP, 'localtime'))
but I am not looking for insert statement
SELECT datetime(1092941466, 'unixepoch', 'localtime');
but I am not looking for select statement
Please help me either with a setting (if available in DB Browser for SQLite) or an update query that can change all times from GMT TO IST.
Thanks.
EDIT
SQLite has no DATETIME type. And it treats datatypes very different from other DBMS. For example
CREATE TABLE T (
Field MYTYPE
);
will run OK. Sqlite is applying so called datatype affinity https://www.sqlite.org/datatype3.html#affinity to figure out one of the implemented datatypes it will use instead of stuff specified it CREATE TABLE. DATETIME (as well as MYTYPE) affinity is NUMERIC - a special affinity which means column can store any type you want, TEXT for example.
This boils down the only way to work with DATETIME in Sqlite is datetime functions. And those functions use default timezone UTC. Any other timezone must be provided explicitly as a part of the datetime string. No PRAGMA or something to change this default.
EDIT
If expire_time is currently a string expression of UTC time you can get specific timezone text value, for example
select datetime(expire_time, '+05 hours','+30 minutes') || ' IST' as t
Note datetime(d,'utc') will most probably return NULL if string d contains explicit timezone. So i advice you standardize on storing datetime as UTC in DB and convert it to different timezone needed only when generating an output. This way you have all Sqlite toolbelt at your disposal.
I want to insert a datetime into a date field in oracle. I have up until now used sysdate, which works perfectly, however I need to use the same timestamp for something else.
Does anyone know how I can create a datetime variable that is compatible with oracle's date field? Everything I try causes errors.
I have tried variations along the lines of this:
Dim timestamp As DateTime = CDate(Now.ToString("yyyy/MM/dd HH:mm:ss"))
.......more code....
insertBuilder.Append("date = to_date(" & timestamp & ")")
First, you seem to be confused about the difference between a DateTime object and the formatted String representation of that DateTime object. Now, or even better, DateTime.Now is already a DateTime object, so it makes no sense to format it as a string and then parse the string to get it back into a DateTime value again. So, you can simply do this to accomplish the same thing:
Dim timestamp As Date = Date.Now
Note that in VB.NET, Date is a keyword that is short for DateTime, just as Integer is "short" for Int32.
Second, you should not be appending DateTime values directly into the SQL command string. You should be using a parameterized query. When you append the DateTime value to the SQL string, you must make sure it is formatted properly (by calling timestamp.ToString(...)). Unfortunately, however, which format is proper will depend entirely on the culture settings of the server. So, it is far better to use a DB parameter, set the parameter value equal to the actual DateTime object, and then let the DB provider do the conversion for you.
A parameterised query is a good start because you can pass a native .Net DateTime object into Enterprise Library (for example) and specify that it is of DbType.DateTime and let EL worry about it. However if you cannot do that because you aren't using EL or something similar and are stuck with using SQL strings as posted in your question then you need to be aware of how Oracle treats dates internally, particularly with regards to format.
Using SQL Developer or SQLPlus, execute the following:
Select To_Char(sysdate) from Dual;
The format which is displayed is the format which your Oracle instance expects dates to be in if you use To_Date and which Oracle will use in To_Char. If you deviate from that format in either call then you will get problems. However, regardless of the default format being used you can override it by specifying it in the call:
Select To_Char(sysdate, 'YYYY/MM/DD HH:MI:SS') from Dual;
Select To_Date('2013/02/26 05:03:27', 'YYYY/MM/DD HH:MI:SS') from Dual;
You can set the default you want Oracle to use implicitly by setting the NLS_DATE_FORMAT parameter:
ALTER SESSION SET NLS_DATE_FORMAT='YYYY/MM/DD HH:MI:SS';
Note that the previous statement only alters the default for the session in which it is executed. We use a custom format in our Oracle systems and to ensure that we get it on all sessions we use an after logon trigger:
create or replace TRIGGER NLS_Parameters_OnLogon AFTER LOGON ON SCHEMA BEGIN
-- This makes the database case insensitive for sorting and searching and sets the default date format for TO_CHAR and TO_DATE
EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_COMP=LINGUISTIC';
EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_SORT=BINARY_CI';
EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_DATE_FORMAT=''YYYY/MM/DD HH:MI:SS''';
End;
It is possible to change the default date format for the whole Oracle server by editing some of the Oracle config files but since we have to deploy to client servers over which we have no control we use this method instead.
This article covers the same sort of ground I just have with a little more in the way of examples.
I am saving date with time into DB and using SQL Developer which by default shows just date. I know its matter of client config but I have seen query in Oracle that returned time component of date when one was present. Something like convert (HH:MM:SS). I am just interested in this one.
To show the time element of all dates as a one-off exercise use this DDL statement.
ALTER SESSION set nls_date_format = 'DD-MON-YYYY HH24:MI:SS'
/
or whatever date mask takes your fancy.
To set this permanently in SQL Developer, set parameters in
Tools -> Preferences -> Database -> NLS Parameters
I you understand that it's a matter of client config and want to see time portion of date for some special purpose you can query it converting to char like this:
select to_char(sysdate, 'yyyy.mm.dd hh24:mi:ss') from dual;
More info on date format can be found here or in official docs
I'm using an Oracle DB and I'm trying to fetch data from its tables using PHP. One of the tables contains a date column which behaves strange to me.
When I open the table in my DB client (I'm using Navicat Lite) I see dates like "2007-11-29 10:15:42" but when I retrieve them with PHP and display the date it says "29-NOV-07". I use a simple SQL query and standard PHP functions (oci_parse, oci_execute, oci_fetch_array).
Why is the value from the DB converted to this (useless) format? How can I get the date just like it is stored in the DB? Thanks for your tips!
Oracle DATE datatype is a point in time, it has no format attached. When you transform a date to a string (to display if for example), the format applied to the date will be dependent upon your session parameters (implicit conversion). From what I remember of PHP, the retrieval functions will convert the date to a string automatically, using the NLS_DATE_FORMAT session parameter.
Either:
change the NLS_DATE_FORMAT beforehand with:
ALTER SESSION SET NLS_DATE_FORMAT='yyyy-mm-dd hh24:mi:ss'
or, if you want to specify another format in your query, you should explicitely ask for it:
SELECT to_char(my_date, 'yyyy-mm-dd hh24:mi:ss') ...
Update
Thanks to ThinkJet for the link to the PHP documentation:
DATE columns are returned as strings formatted to the current date format. The default format can be changed with Oracle environment variables such as NLS_LANG or by a previously executed ALTER SESSION SET NLS_DATE_FORMAT command.