What would be the output of these SQL functions? - sql

SELECT EXTRACT(d from DATE_TRUNC('month', '2022-10-30'::DATE))
and
SELECT EXTRACT(month from '2022-10-30'::DATE + interval '5 days')
I thought it was going to be 30 for the first one because it says "D" before date trunc, but that is wrong.
For the second one, I am confused and I wasn't able to find any helpful tips on the internet nor was my book helpful . I am not understanding how +interval 5 days can affect the month.
If you could help me with this, I would really appreciate it!

The first query will return 1 as you are using DATE_TRUNC with field 'month' and datepart is DATE so it will return
SELECT DATE_TRUNC('month', '2022-10-30'::DATE)
will return 2022-10-01T00:00:00Z and when you extract the d then it will return 1.
For the second case when you are adding 5 days to the date then the date is changing to 2022-11-04T00:00:00Z hence the month is changing to 11 instead of 10 hence the result will be 11.

Related

How to get the first day of the previous month in SQL (BigQuery)

Would any of you know and would like to share the konwledge how to subtract the number of days from the current date (the data is type = DATE) so that I get the first day of the previous month. Here is an example:
Current Date = '2022-10-27'
The date I want = '2022-09-01'
I know how to get the first day of the current month using this:
(CURRENT_DATE() - EXTRACT(DAY FROM CURRENT_DATE()) +1)
BuT I have no idea how to check how many days there were in the previous month and hence get the correct answer.
I though that maybe DATE_TRUNC(CURRENT_DATE() - EXTRACT(DAY FROM CURRENT_DATE())) would work but I'm getting this error:
"No matching signature for function DATE_TRUNC for argument types: DATE"
SO that's clearly not the way. Any suggestions please? :)
Try using a combination of DATE_TRUNC and DATE_SUB as follows:
select current_date() as curr_date,
date_sub(date_trunc(current_date(), MONTH), INTERVAL 1 MONTH) as lm_day_1
It produces the following:

Oracle SQL: Dynamic timeframe calculation

Greetings all knowing Stack.
I am in a bit of a pickle, and I am hoping for some friendly assistance form the hive mind.
I need to write a query that returns the difference in days between a registration date (stored in a table column) and the first day of the last September.
For example; assuming the query was being run today (24-10-2016) for a record with a registration date of 14-07-2010, I would want the script to return the difference in days between 14-07-2010 and 01-09-2016
However had I run the same query before the end of last August, for example on 12-08-2016, I would want the script to return the difference in days between 14-07-2010 and 01-09-2015.
I'm fine with the process of calculating differences between dates, it's just the process of getting the query to return the 'first day of the last September' into the calculation that is tripping me up!
Any input provided would be much appreciated.
Thankyou =)
Try this approach:
add four months to the current date
truncate this date to the first of year
subtract four months again
Add_Months(Trunc(Add_Months(SYSDATE, 4), 'year'), -4)
Hope this might help.
WITH T AS (SELECT TO_DATE('14-07-2010','DD-MM-YYYY') REG_DATE,
SYSDATE EXEC_DATE
FROM DUAL)
SELECT CASE WHEN TO_CHAR(EXEC_DATE,'MM') >= 9
THEN ADD_MONTHS(TRUNC(EXEC_DATE,'YEAR'),8)
ELSE ADD_MONTHS(TRUNC(ADD_MONTHS(EXEC_DATE,-12),'YEAR'),8)
END
- REG_DATE AS DIFF
FROM T;

How to SELECT number of hours worked in specifc weeks using postresql

I'm trying to find out how many hours employees worked on specific weeks in April and March.
When trying to find the total hours worked (numeric value), I'm given a "Date Out of Range" error. I'm fairly sure I made a simple mistake in my select queries. Could someone point out to me what's wrong?
How would you make a select query if you only wanted to get the number of hours worked from 2015-04-05 to 2015-04-11? If I can figure out how to get the numbers for that specified week, then that'll help me with the other weeks I need to look up as well. Pardon the newbish questions, I'm still new to SQL with very little experience.
SELECT
SUM(CASE WHEN hours_worked BETWEEN dateadd(ww,-1,'2015-04-05') AND dateadd(ww,-00,'2015-04-05') THEN r.hours_worked END) AS hours_worked
SUM(CASE WHEN hours_worked BETWEEN dateadd (wk,-1,'2015-3-14') AND dateadd(wk,-0,'2015-3-20') THEN r.hours_worked END) AS hours_worked
Here's one way to write a query similar to yours:
SELECT
name,
SUM(
CASE
WHEN hours_worked BETWEEN
(to_date('2015-04-05', 'YYYY-MM-DD') - interval '7 days')
AND to_date('2015-04-05', 'YYYY-MM-DD') THEN 1
ELSE 0
END
) AS number_of_records
from test
group by name
to_date function converts a string to a date.
- interval '7 days' tells PostgreSQL to go to last week.
Using hints from the query above, you should be able to suit it to your needs.

Case statement using date intervals

I am trying to create a variable that sums sales in 3 months for each customer after their first purchase in the time series. The code below errors and says I'm missing a parentheses.
sum(case
when merch.trans_dt between min(merch.trans_dt)
and add_date(min(merch.trans_dt), interval 3 month)
then merch.rdswrit_rps_netnet_pur_amt
end) as spend_next3
You can do this by simply using addition, rather than a function: min(merch.trans_dt) + interval 3 month.
However, this may not give you the answer you want. In many cases, such as to_date('1/31/2015','mm/dd/yyyy') + interval '3' month, this will result in ORA-01839: date not valid for month specified.
You're better off using add_months as indicated previously in the comments: add_months(min(merch.trans_dt),3).

SELECT using timestamp in Oracle SQL

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().