SQL Selecting A Range Of Data According To Sysdate - sql

So I have this problem where I am trying to select a range of data for this sql that will run monthly. Basically I have this field Date_OF_Entry that records dates as 01-Jan-15
Now I will be running this script twice a month at the 16th to capture all the data from the 15th back to the 1st. I will also then run the script at the 1st of the next month to capture all data from the end of the month to the 16th.
Any help is appreciated.
What I am doing now.
Where DATE_OF_ENTRY > sysdate-16

ROUND(date,'MM') Rounds down to the 15th, and up from the 16th.
So WHERE ROUND(DATE_OF_ENTRY,'MM') = ROUND(sysdate-1,'MM') will - on the 16th - round sydate and the 1-15th down to the start of the month, and, on the 1st will round everything from the 16th up.
Be carefull though as on the 1st if any new records have been added where date_of_entry = the 1st, they would get bumped down into last month's end of month group.
So, WHERE ROUND(DATE_OF_ENTRY,'MM') = ROUND(sysdate-1,'MM') AND date_of_entry < trunc(sysdate) should clear that up.

Related

SQL if last day of pervious month has value change the beginning date of next month to start from that day

Need help with this sql code, I would like to roll back start day of new month if last day of pervious month has values.
Thanks
I would suggest nesting EOMONTH() in an IF statement.
Not sure what table(s) you're working with, but general layout would be:
IF(MAX(date_column) = EOMONTH(MAX(date_column), MAX(date_column), new_date)
Reference for EOMONTH():
https://www.sqlservertutorial.net/sql-server-date-functions/sql-server-eomonth-function/

Snowflake retrieving data of past month on a particular date of current month

I am new to snowflake and my manager wants me to retrieve the data of the past month when it is 5th of the current month. For example if today is 5th April, then ask snowflake to retrieve the data of the past month i.e. from 1st March 2021 to 31st March 2021 and similar for all the other months.
The reason why he wants to update the last month data on 5th of every next month because that is the day when we get the data.
I tried to use the DATEADD function but it is more complicated than just using this function.
Thanks in advance!
PS: The data for every month has same date. for example: the date is like - April 20th will be stored in the database as "2021-4-01" - and same for April 25th date will be stored as "2021-4-01" .
The day doesn't change in the database, just the month and year.
as to the prior month window that can be done via DATE_TRUNC and DATEADD
select
current_date as cd
,date_trunc('month', cd) as end_range
,dateadd('month', -1, end_range) as start_range
;
gives:
CD END_RANGE START_RANGE
2021-04-21 2021-04-01 2021-03-01
the other half of the question only do it on the 5th, if you have a task run daily etc. can be solved via
,day(current_date) = 5 as is_the_fifth
or if in an inline way
iff(day(current_date) = 5, <do true stuff>, <do false stuff>)

Oracle Week Number from a Date

I am brand new to Oracle. I have figured out most of what I need but one field is driving me absolutely crazy. Seems like it should be simple but I think my brain is fried and I just can't get my head around it. I am trying to produce a Sales report. I am doing all kinds of crazy things based on the Invoice Date. The last thing I need to do is to be able to create a Week Number so I can report on weekly sales year vs year. For purposes of this report my fiscal year starts exactly on December 1 (regardless of day of week it falls on) every year. For example, Dec 1-7 will be week 1, etc. I can get the week number using various functions but all of them are based on either calendar year or ISO weeks. How can I easily generate a field that will give me the number of the week since December 1? Thanks so much for your help.
Forget about the default week number formats as that won't work for this specific requirement. I'd probably subtract the previous 1 December from invoice date and divide that by 7. Round down, add 1 and you should be fine.
select floor(
(
trunc(invoiceDate) -
case
-- if December is current month, than use 1st of this month
when to_char(invoiceDate, 'MM') = '12' then trunc(invoiceDate, 'MM')
-- else, use 1st December of previous year
else add_months(trunc(invoiceDate, 'YYYY'), -1)
end
) / 7
) + 1
from dual;

SQL Query that rund monthly on a fixed day range

I have an SQL query I need to run once a month.
The data set the query produces always has to be from the 11th of the month before to the 10th of the current month.
I now manualy run the query in the fews days after the 11th day of the month manually adjusting the date range in my where statement:
for example...
Where Column A is greater than 10/10/2015 and less than 12/11/15
I was hoping there would be a statement I could add to my query to automatically find the 11th day of the last month and the 10th of the current month. This way I could schedule the query and automatically email the results.
You should be able to use the following within your query: -
CONVERT(date,FORMAT(GETDATE(),'yyyy-MM')+'-10')
(for the 10th of this month)
and
CONVERT(date,FORMAT(DATEADD(m,-1,GETDATE()),'yyyy-MM')+'-11')
(for the 11th of last month).
Try to look out the MONTH() function in your working DBMS. In MySQL and MSSQL it returns a number (1 been january) corresponding to the current month that your system is (you may check if it's date is updated).
With this function you can subtract 1 to get the last month, having to do some logic when the current one is January, hence 1. Since now you should get 12 (december) intead of 0 (an error).
Cheers, mate!

Get the EOM and MOM of any month in SQL Server 2005

I've periods of time every 15 days, MOM and EOM.
What I need to check is if a date value on a date field is prior to the current period minus 1.
For example, if today is 12/29, the period is 12/31, and i need to check
if prior < 12/15
How can i get the EOM (End Of Month, i mean, the last day of the month) and the MOM (Middle of month, it's like every 15th of month) with GETDATE() function without doing a DATEADD with -15 days (because in feb will be fail, and i don't care the month)
Any help or work around will be preciated.
Thanks
If you need the value 15 then put it in your code.
If that is against your company's policies then challenge the person that made that policy. Writing 5 lines of code to replace two characters is not a good coding...
If writing the 5 lines made your application much more flexible then maybe I could understand, but you are still "hard coding" 15 into your comparisons.
Thinking in a work around, what I did it was this:
If actual day < 15 then get the month actual, convert to the first day of the month (01) and minus 1 day. I get the last day of the prior month. (EOM - 1 period)
If actual day > 15, then the prior period (MOM - 1 period) is: 15 of actual month.
It's a query with if structure.
If someone has a better answer, please answer it and I'll be accept it.
Thanks :)