I'm doing a SQL query in Oracle 10g where I'm comparing against a cutoff date. So my query has this in it:
THING < TO_DATE('02/14/13','MM/DD/YY')
Now the THING can have a time component in it. I want to know how the cutoff date will interact with it. Does the TO_DATE function have some default implied time component in it? Does the date it creates have a default time of midnight on the specified date, or noon or some other time? Essentially my concern is if I have a column in the table like this:
THING
-------
2/4/13 11:13AM
2/13/13 3:36PM
2/14/13 2:00PM
2/15/13 1:52AM
Will I get 2 rows or 3 rows back?
The implied time is 00:00:00, so in your example you will get two rows back.
You can verify this with:
select to_char(TO_DATE('02/14/13','MM/DD/YY'),'YYYY-MM-DD HH24:MI:SS')
from dual;
You'll get two rows back. The implied time is 0:00:00 (midnight). Your dates with a 24-hour clock look like this:
2/13/13 3:36PM --> 2013-02-13 15:36:00
TO_DATE('02/14/13','MM/DD/YY') --> 2013-02-13 00:00:00
Related
I am working on creating a dashboard on Tableau. To get the best result, I am writing a code on SQL first. But, my requirement is to update the date between specific dates. For example, in SQL, I want to filter the date range and it will update automatically for tomorrow. Below is my filter code.
WHERE date BETWEEN '2020-02-02 00:00:00' and '2021-02-03 00:00:00'
------------------------------------Expectation-------------------------------------------------------
Tomorrow date range: 2020-02-03 00:00:00 and 2021-02-04 00:00:00
after a month date range : 2020-03-03 00:00:00: and 2021-03-04 00:00:00
In short, I won't need to re-range the date filter manually and date would be update automatically on Tableau as well.
Could anyone help me with this issue ?
In SQL just use the now() function - to which you can add/subtract days as necessary
In Tableau use Relative Date Filters
For doing this on Tableau, simply add the `[date] field on filters card, click relative dates and use the following options..
A screenshot for simplest of data
I would recommend:
WHERE date >= CURRENT_DATE AND
date < DATE_ADD(CURRENT_DATE, INTERVAL 1 DAY)
Note that this code does not use BETWEEN. I see no reason to double count midnight on different dates. This captures only the times that are with in the day, from midnight to just before midnight when the next day begins.
I am running below query:
select a.event_date,
date_format(date_trunc('month', a.event_date), '%m/%d/%Y') as date
from monthly_test_table a
order by 1;
Output:
2017-09-15 | 09/01/2017
2017-10-01 | 09/30/2017
2017-11-01 | 11/01/2017
Can anyone tell me why for date "2017-10-01" it is showing me date as "09/30/2017" after using date_trunc.
Thanks in Advance...!
You are reverse formatting so it is incorrect.
Use the below Code
select a.event_date,
date_format(date_trunc('month', a.event_date), '%Y/%m/%d') as date
from monthly_test_table a
order by 1;
You can use date_add with a logic to subtract 1-day(yourdate) to replicate trunc.
For eg:
2017-10-01 - day('2017-10-01') is 1 and you add 1-1=0 days
2017-08-30 - day('2017-08-30') is 30 and you add 1-30=-29 days
I faced the same issue recently and resorted to using this logic.
date_add(from_unixtime(unix_timestamp(event_date,'yyyy-MM-dd'),'yyyy-MM-dd'),
1-day(from_unixtime(unix_timestamp(event_date,'yyyy-MM-dd'),'yyyy-MM-dd'))
)
PS: As far as i know, there is no date_trunc function in Hive documentation.
As per the source code below: UTC_CHRONOLOGY time is translated w.r.t. locale, also in Description it is mentioned that session timezone will be the precision, also refer to below URL.
#Description("truncate to the specified precision in the session timezone")
#ScalarFunction("date_trunc")
#LiteralParameters("x")
#SqlType(StandardTypes.DATE)
public static long truncateDate(ConnectorSession session, #SqlType("varchar(x)") Slice unit, #SqlType(StandardTypes.DATE) long date)
{
long millis = getDateField(UTC_CHRONOLOGY, unit).roundFloor(DAYS.toMillis(date));
return MILLISECONDS.toDays(millis);
}
See https://prestodb.io/docs/current/release/release-0.66.html:::
Time Zones:
This release has full support for time zone rules, which are needed to perform date/time calculations correctly. Typically, the session time zone is used for temporal calculations. This is the time zone of the client computer that submits the query, if available. Otherwise, it is the time zone of the server running the Presto coordinator.
Queries that operate with time zones that follow daylight saving can
produce unexpected results. For example, if we run the following query
to add 24 hours using in the America/Los Angeles time zone:
SELECT date_add('hour', 24, TIMESTAMP '2014-03-08 09:00:00');
Output: 2014-03-09 10:00:00.000
When use of to_date function like
select to_date(17,'HH24') from dual
it return the 2015/3/1 17:00:00 which starts from the first day of the month. I wonder why the to_date function behave like this?
I expect it starts from the current day,
so it will show like '2015/3/31 17:00:00'.
How to deal with it?
If you are trying to get 17:00 on today's date you can do this:
trunc(sysdate) + 17/24
From a similar question: Oracle Get only time from To_Date() in a Query?
One of the anwsers may explain the behavior:
The reason this doesn't work is because this not a complete date. Even
when you use a to_date('07/12/2011','MM/DD/YYYY'), Oracle stores the
date and time, but makes all the components of the time ZERO. So the
actual date stored is 07/12/2011 HH:MI:SS
So maybe it stores correctly the year and the month but not the day.
I have an application, using an Oracle 11g (11.2.0.2.0 64 bit) db.
I have a lot of entries in a Person table. To access data I'm using different application (same data).
In this example I'm using birth_time field of my person table.
Some application queries data with birth_time directly, some other with to_char to reformat it, and some other with UTC function.
The problem is this: with same data, same query, result are different.
In this screenshot you can see the result with Oracle Sql developer (3.2.20.09)
All the timestamp are inserted with midnight timestamp, and in fact the to_char(..) and birth_time result are at midnight. UTC hours are returned with one hour less (Correct according to my timezone!) but some entry (here one for example, the last one) is TWO HOURS less (only few on thousand are Three)!!
The same query executed with sql*plus return the correct result with one hour of difference for all the entries!
Does anyone have a suggestion to approach this problem?
The issue is born because one of our application made with adobe flex seems to execute queries with UTC time, and the problems appears when you look at data with this component.
ps.:
"BIRTH_TIME" is TIMESTAMP (6)
Would it be possible for you to change the query used? If so, you could use the AT TIME ZONE expression to tell Oracle that this date is in UTC time zone:
SELECT SYS_EXTRACT_UTC(CAST(TRUNC(SYSDATE) AS TIMESTAMP)) AS val FROM dual;
Output:
VAL
----------------------------
13/11/20 23:00:00,000000000
Now, using AT TIME ZONE 'UTC' gets you the date you need:
SELECT SYS_EXTRACT_UTC(
CAST(
TRUNC(SYSDATE) AS TIMESTAMP)
AT TIME ZONE 'UTC') AS val FROM dual;
Output:
VAL
----------------------------
13/11/21 00:00:00,000000000
I have 2 questions:
I want to compare a field whose data type is "Date" against a given date. The DB is oracle and being a mysql guy I'm finding it difficult to come up with simple queries.
The field("date_closed") stores date in UTC format (24-Aug-2011 18:55:11 for example) and I want to convert it to PST for comparison.
I tried this query but it returns some extra rows in the data set(obviously):
select * from table1 where trunc(date_closed)=to_date('2011-08-24','yyyy-mm-dd')
How do I covert to PST format before comparison?
In the same query how do I compare "date_closed" against the current date?
You need the NEW_TIME function
Dates don't include timezone in Oracle, and are assumed to be in the database timezone (which may by UTC but probably isn't). You should look at the TIMESTAMP WITH TIMEZONE data types.
Also, bear in mind that if you are comparing to the current date - I assume that you want to strip off the timestamp and compare only the day.
So, if new_time(date_closed,'GMT','PST') translates the date , your where clause will be comparing something like
trunc(new_Time(date_closed,'GMT','PST')) = trunc(sysdate)
to get all records with date_closed on the current day in PST.