SQL query for updating the date automatically - sql

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.

Related

Best way to handle between in two dates in postgresql on timestamp

i am having date field in my table in the format of yyyy-mm-dd HH:MM:SS wanted to get records between 11-july-2020 inclusive 14-july-2020 but records are missing on 14th. Tries >= && < SYMMETRIC but non of them is working.
currently using select .... from employee where ... and created_on in between ?1 and ?2 . It is excluding record created on 14th.
I think you want:
where col >= '2020-07-11' and col < '2020-7-15'
Or, to express this using timestamps:
where col::timestamp >= '2020-07-11'::timestamp and
col::timestamp < '2020-07-14'::timestamp + interval '1 day'
Note that the higher end is one day later than 2020-07-14, so you can get all times on that day.
You can also use between as:
where col::date between '2020-07-11'::date and '2020-07-14'::date
But I discourage the use of between with date/time data types -- precisely because of the problem that you are having with the time component.
If your field has format yyyy-mm-dd HH:MM:SS it is a timestamp field not a date field. That makes a big difference in the comparison that needs to be done as #Gordon shows. If you compare a timestamp to a date(14-july-2020) the date will be turned into a timestamp with a time of 00:00:00:
select '07/14/2020'::timestamp;
timestamp
---------------------
2020-07-14 00:00:00
That means you will miss the timestamps on the 14th that fall after Midnight of 13/14th.

Convert 17 digit with decimal point timestamp in SQL to date

Trying to convert 43439.961377314816 into date. Currently I am using this code:
SELECT
(timestamp '1970-01-01 00:00:00 GMT' +
numtodsinterval(WRITETIMESTAMP, 'SECOND')) at time zone 'CST',
WRITETIMESTAMP
FROM
t.table
but I am getting this result:
01-JAN-70 06.03.59.961377315 AM CST
Date should be:
12/05/2018
This produces the date that you want:
select date '1899-12-30' + 43439.961377314816
from dual;
It looks like you are using Excel dates or something similar.
You have two problems in your query. First, you used the wrong base time. As pointed out by #GordonLinoff, the base time for an Excel date is actually 1900-01-01, and Excel treats 1900 as a leap year. This is not an error in Excel, per se, but a conscious design decision which was made to copy the (buggy) behavior of Lotus 1-2-3, which did have this bug. So - in Lotus 1-2-3 it's a bug, but in Excel it's a feature. :-) Secondly, in Excel dates the integer portion represents the number of days since the base date, and the fractional portion represent fraction of a day. In your NUMTODSINTERVAL call, however, you specified the interval_unit argument as 'SECOND'; it should have been 'DAY'.
Putting these things together we get
WITH cte AS (SELECT 43439.961377314816 AS WRITETIMESTAMP FROM DUAL)
SELECT
(timestamp '1899-12-30 00:00:00 GMT' + numtodsinterval(WRITETIMESTAMP, 'DAY')) at time zone 'CST',
WRITETIMESTAMP
FROM
cte
dbfiddle here
Best of luck.
This looks like expected behavior to me. 43439 seconds/60/60 = 12 hours and you're getting about 12 hours from the starting timestamp.
SELECT numtodsinterval('43439.961377314816', 'SECOND') as i FROM dual;
I
----------------------
+00 12:03:59.961377315
Why would you think that would give you a date in 2018?
Here is a working formula to put in Excel that works for Chromium browsers.
Chrome/Edge: =((Cell/1000000-11644473600)*1000000)/86400000000+DATE(1970,1,1)

SQL to query greater than today's date and time, with time defaulting to 12:00:00 AM

Format of the time/date in the table from which I am querying is the following: 11/12/2015 12:00:00 AM.
I would like to be able to query if the date is greater than or equal today, but look for results as the above format. Using getdate() returns the current specific time. Also, the specific format includes two spaces between the date and time.
Thanks for you help.
You can remove the time from consideration as follows:
WHERE CAST(YourDateTime AS DATE) >= CAST(getdate() AS DATE)

Oracle to_date function usage

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.

Yesterday's date in where clase with HH:MM:SS

In my query I currently have user enter datetime.
Current query
where TableT.STARTDATETIME >= To_Date('?DATE1?','MM-DD-YYYY HH24:MI:SS')
and TableT.STARTDATETIME <= To_Date('?DATE2?','MM-DD-YYYY HH24:MI:SS')
User would enter
for Date1: 10-02-2013 00:00:00
for Date2: 10-02-2013 23:59:59
Parameter :- ?DATE? & DATE2 is just a parameter for user to enter dates.
Need
How can I made sql automatically enter yesterday's date starting from 00:00:00 to 23:59:59?
I know I can use use something like sysdate-1 but not sure.
You could use
TRUNC(TableT.STARTDATETIME) = TRUNC(sysdate-1)
for this purpose to truncate both dates to the day on both side of the check. However, for this to be efficient, you'd need a function index on TRUNC(TableT.STARTDATETIME).
Maybe better in general from a performance aspect:
TableT.STARTDATETIME >= trunc(sysdate-1) AND TableT.STARTDATETIME < trunc(sysdate);
This includes yesterday 00:00:00 (the >= ), but excludes today 00:00:00 (the <).
Warning! Keep in mind, that for TIMESTAMP columns - while tempting because of its simplicity - don't use 23:59:59 as end time, as the 1 second time slot between 23:59:59 and 00:00:00 might contain data too - and this gap will leave them out of processing...
It would be:
where TableT.STARTDATETIME >= trunc(sysdate-1) and
TableT.STARTDATETIME < trunc(sysdate)
Avoid truncating the column value itself -- although you can place an index on Trunc(TableT.STARTDATETIME) you'd need another one to support time-based queries, and it's a fine way to obscure the distribution of values from the optimiser.
Have a look here for more info on date and timestamp arithmetic, and at the Trunc(datetime, format) function for other useful ways of manipulating dates.