I'm having trouble setting up a SQL to bring up all information 2 weeks before and after the current date. Here is what I am currently doing:
Select WRK.Wrk, WRK.Client, WRK.Status, WRK.TAT, WRK.Due
From WRK
WHERE WRK.Due >= now()
Order By WRK.Due Desc, WRK.Status Desc
This gets me everything due on or after the current date but when I try to add lines to indicate 2 weeks before and after the current date I get errors.
Thanks
Date/time function vary significantly among databases. The use of now() makes me think of MySQL. The syntax in MySQL is:
where wrk.Due between date_sub(curdate(), interval 2 weeks) and date_add(curdate, interval 2 weeks)
Note that between includes the end dates, so this might be off by a day in either direction.
You can implement similar logic in other databases, but the specific functions would look different.
Related
I have a below query that I run to extract material movements from the last 7 days.
Purpose is to get the data for the last calender week for certain reports.
select
*
From
redshift
where
posting_date between CURRENT_DATE - 7 and CURRENT_DATE - 1
That means I need to run the query on every Monday to get the data for the former week.
Sometimes I am too busy on Monday or its vacation/bank holiday. In that case I would need to change the query or pull the data via SAP.
Question:
Is there a function for redshift that pulls out the data for the last calender week regardless when I run the query?
I already found following solution
SELECT id FROM table1
WHERE YEARWEEK(date) = YEARWEEK(NOW() - INTERVAL 1 WEEK)
But this doesnt seem to be working for redshift sql
Thanks a lot for your help.
Redshift offers a DATE_TRUNC('week', datestamp) function. Given any datestamp value, either a date or datetime, it gives back the date of the preceding Sunday.
So this might work for you. It filters rows from the Sunday before last, up until but not including, the last Sunday, and so gets a full week.
SELECT id
FROM table1
WHERE date >= DATE_TRUNC('week', NOW()) - INTERVAL 1 WEEK
AND date < DATE_TRUNC('week', NOW())
Pro tip: Every minute you spend learning your DBMS's date/time functions will save you an hour in programming.
I'm trying to condition my WHERE clause to accommodate relatively defined dates into my date filter. I'm pretty confused what type I need to use, if it's CONVERT or TO_DATE function, or if I need to put a CASE WHEN statement into my code.
This is the code that I have written so far:
WHERE event_create_verified_user_success.created_at_utc_date
BETWEEN DATE '2021-11-29' AND DATE '2021-12-05'
And this is the condition of the activity I need to finish:
If the desired date-period is not set manually using fixed dates like from “2021-11-29”
to “2021-12-05”, how would you change the where-clause to consider all data from relative
defined dates: “consider messages created between 10 days and 5 days ago (inclusive)”
I've only started PostgreSQL yesterday and the last time I've handle SQL was probably 4 years ago so I'm pretty confused at how much SQL has changed since then.
Thank you so much for helping!
The basic syntax hasn't really changed in the last 4 years (or even 15 years).
You can use current_date to obtain "today's date". You can subtract days from that
where ... between current_date - 10 and current_date - 5
If created_at_utc_date is a timestamp (= date and time) rather than a date (=date without time) it's better to use a range query though:
where created_at_utc_date >= current_date - 10
and created_at_utc_date < current_date - 4
Note the < combined with the next day you want to compare with.
I have this where SQL Query. I want to know if I can limit that user can only pick 2 days of of date range. meaning they cannot pick 1 month or 1 week or more than 2 days difference of querty data range.
WHERE
To_Date(to_char(B.time_stamp, 'DD-MON-YYYY')) >= To_Date('?DATE1::?','MM/DD/YYYY')
and To_Date(to_char(B.rest_date, 'DD-MON-YYYY')) <= To_Date('?DATE2::?','MM/DD/YYYY')
If I understand your question, you are just asking how to limit the two dates so that they are within 2 days of each other. Date math is pretty simple in Oracle (which is my guess as to the DB you are using):
WHERE ABS(Date1 - Date2) <= 2
You don't need to convert it at all with to_char or anything else, since it is stored internally as an actual date. You can use this same type of logic to make sure it is less than 16 hours:
WHERE ABS(Date1 - Date2) <= 16/24
As long as you remember to adjust your units appropriately.
Note that in this case, 2 days means 48 hours. If you mean it has to be 2 actual days, then it is slightly different.
my problem is trying to use a SELECT statement and order the top 10 by a certain column. I managed to compile something after searching through lots of forums, however I need to confirm that the timestamp in one field is within the last week. I have gotten this to execute however i'm not sure whether this is correct as I can't print the value for the where clause:
SELECT itemid, count(itemid)
FROM Rateddate
WHERE TO_CHAR(CURRENT_TIMESTAMP - DATE_RATED) < TO_CHAR(7)
GROUP BY itemid;
TLDR:
TO_CHAR(CURRENT_TIMESTAMP - DATE_RATED) < TO_CHAR(7)
does this make sure the date_rated timestamp is less than a week old?
It would make more sense to say
WHERE date_rated > sysdate - interval '7' day
if you want data from the last 168 hours. You may want
WHERE date_rated > trunc(sysdate) - interval '7' day
if you want data from any point in the day 7 days ago rather than caring about what time of day it is currently.
Wouldn't this work for you:
trunc(CURRENT_TIMESTAMP - DATE_RATED) < 7
This is assuming that DATE_RATED is a date field. If not, you need to convert it first with TO_DATE().
I am trying to use the actual numerical value for the month on a sql query to pull results. Is there any way to do this without having a function to change the numbers to actual month names, then back to month numbers? The following code works for Names, what works for numbers?
datename(month,(convert(datetime,DTSTAMP)))=
'October'
month,(convert(datetime,DTSTAMP)) should do it, but why on earth are you not storing the data correctly as a datetime to begin with? All that additional conversion stuff to use the dates adds unnecessary load to your server and slows down your application.
Datepart is an alternative to the month command and it is more flexable as you can extract other parts of the date.
DATEPART(mm, convert(datetime,DTSTAMP))
Gets Month Number of Date
CONVERT(VARCHAR(2) ,DTSTAMP ,110)
here, you can search with diff options like:
DATE_SUB(CURDATE(), INTERVAL 0 DAY)
DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
DATE_SUB(CURDATE(), INTERVAL 1 YEAR)
as per your requirement.