First day of the month - sql

WHERE to_date(smz_loanservicing.as_of_date) = last_day(add_months(current_date, -1))
The above will provide data only if the loanservicing.as_of_date occurs on the very last day of the month.
Last month (May 31 2020) the last day of the month fell on Sunday.
Is there a way to get the the first day of the month and say if this particular date occurs between the first and last day of the month, show the date? Essentially there were no activities on Sunday so the data was missed.
I tried
to_date(smz_loanservicing.as_of_date)
between first_day(add_month(current_date,-1))
and last_day(add_months(current_date, -1))`
However I get syntax error.

You seem to want to check if your date column belongs to the current month.
The syntax you use would work in Oracle, so let me assume this is the database that you are running. I also assume that column as_of_date is of datatype date (or timestamp).
What you ask for should be as simple as:
where
as_of_date >= trunc(current_date, 'mm')
and as_of_date < add_months(trunc(current_date, 'mm'), 1)
Actually, your syntax would also work in Snowflake - and so would the above code.
Note: if as_of_date actually is a string, then you need to_date() to convert it.

You could just truncate the date to the month, then you don’t need the know the first or last day.
where trunc(as_of_date, ‘MM’) = trunc(current_date, ‘MM’)

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:

SQL - filter values based on month & date

I'm currently strugeling to get something done.
I needed only the rows where the date is between a certain date and month.
Example show only the rows where the date is between 01/05 (DD/MM) and 08/07 (DD/MM) the date can be found in the table tasks within the field information.
The year can't make any sense, but the results may only between those two dates in that year.
I've tried:
BETWEEN (TO_DATE ('01/05','DD/MM') AND (TO_DATE('08/07', 'DD/MM')
EXTRACT (DD/MM) from information.
none of those are working for me, I hope that someone of you can help me to figure this out!.
Thanks!
You seem to want to disregard the year. If so, then I would recommend:
TO_CHAR(datecol, 'MM/DD') BETWEEN '05/01' AND '07/08'
In order for BETWEEN to work in this case, you need the format in the order of MM-DD.
If you want this for a particular year, then use direct date comparisons:
datecol >= DATE '2018-05-01' AND
datecol < DATE '2018-07-09' -- note this is one day later
Oracle dates have a time component, so you need to be careful when making comparisons.

How to get records between from and to date, when dates are for the same month/year?

I am trying to create a query to that can get some records in a table that is between a from and to date, with the dates being in month/year only. The problem that I am having is trying to get the records when the from and to dates are for the same month/year.
Here is a example of the issue that I am having:
select start_date
from job
where trunc(start_date) between to_date('05-2016','mm-yyyy') and to_date('05-2016','mm-yyyy')
In the job table, there are records with start_date in the month of May, but in order to see them I need to set the to date to '06-2016'. Is there way to get all of the records with a start_date in the month of May by just specifying that the from and to dates is 05-2016?
In your example you are selecting all start_date's between 5/1/2016 and 5/1/2016.
It seems like you want to capture everything in a month, but you are not specifying a format to truncate. Without specifying 'Month' in the Trunc() you are truncating to the day. When you truncate to Month you can now, actually just set it equal to the to_date() rather than between:
select start_date
from job
where trunc(start_date,'Month') = to_date('05-2016','mm-yyyy')
Here is some information on Trunc(date, [fmt]). when the fmt argument is left blank Trunc() defaults to 'round' to the nearest day but there are many other options.
If you want to specify ranges greater than a Month you can use between (but note, this is from the first day of the first to_date() to the first day of the second 'to_date()':
select start_date
from job
where trunc(start_date)
between to_date('06-2016','mm-yyyy')
and to_date('09-2016','mm-yyyy')
In this example all Start_dates would populate between 6/1/2016 and 9/1/2016.
I think the most accurate way to do this would be:
select start_date
from job
where trunc(start_date)
between to_date('06-01-2016','mm-dd-yyyy')
and to_date('09-30-2016','mm-dd-yyyy')
How about?
select start_date
from job
where trunc(start_date, 'mm') between
to_date('05-2016', 'mm-yyyy')
and to_date('05-2016', 'mm-yyyy')
Oracle has no way of interpolating the date on the right end of your between as falling at the end of the month. When to_date() is missing a day value in the format it simply supplies the 1st as the default value. Not sure if that's the behavior your were anticipating.
If instead you truncate dates by month (rather than day) then the comparisons will only involve dates falling on the first of the month. With the dates collapsed that way you can then treat a start and end of the two identical values as an inclusive range. Presumably you'll replace the hard-coded dates with parameters of some form and this works for wider ranges as well. Of course, the between can be just an equality test if you're always querying on a single month. And this isn't going to use an index on your column so it's not necessarily the most efficient.
I think this is what you're looking for. Is there a reason you need to supply the date value(s) in that format? Perhaps there's a better way to approach this using a little date math.
Try like this:
select start_date
from job
where trunc(start_date) between to_date('05-2016','mm-yyyy')
and dateadd('d', -1, dateadd('m', 1, to_date('05-2016','mm-yyyy')))
In order to see an entire month worth of dates, your date range in the between clause must be a full month long.

Calculation with WEEK in oracle

I am comparing two dates in oracle where I stuck at one point in which I have to compare my week with the current week.
Suppose today is 05-Jan-2015 and it's a first week and input date is 29_Dec-2014 and I'm executing below statement to compare:
ld_week BETWEEN FOCUS_WEEK-4 AND FOCUS_WEEK-1
where:
ld_week is the week from input date (last week of current year)
focus_week is week from current date (first week of next year)
hence (52 BETWEEN (1-4) AND (1-1)) always fails.
I am using below function to calculate the week.
FOCUS_WEEK := to_number(to_char(to_date(focus_day),'WW'));
Please let me know how to deal with it.
Your question is a bit vague. I believe you want to try
trunk(date '2014-12-29', 'd')
between
trunk(sysdate - 4*7 , 'd') and
trunk(sysdate - 1*7 , 'd')
With trunc(date, 'd') you obtain the start date of a week.

SQL +/- number meaning?

I could not find the meaning of the following SQL command:
where date between to_date('2013-03-01', 'yyyy-mm-dd') and trunc(sysdate, 'mm') -1
What does the "-1" mean / does?
The other example is
trunc(months_between(date1, date2))+1
I have searched for this, but could not find a thing.
Thank you for advice!
As others have answered, "date - 1" subtracts one day from the date. Here's more detail on your specific SQL snippets:
where date between to_date('2013-03-01', 'yyyy-mm-dd') and trunc(sysdate, 'mm') -1`
This evaluates to "date between 3/1/2013 and the end of last month"
TRUNC(some date, 'MM') chops the date to the beginning of the month
TRUNC(SYSDATE, 'MM') returns the beginning of the current month
TRUNC(SYSDATE, 'MM')-1 returns the last day of the previous month
trunc(months_between(date1, date2))+1
This is giving the number of full months between date1 and date2, treating any fraction of a month as a whole month. For example, if you gave it the dates 7/28/2013 and 7/29/2013 it would report one month, and it would also report one month if you gave it 7/1/2013 and 7/31/2013.
The MONTHS_BETWEEN function returns, as it implies, the number of months between two dates. The return value will have decimal places - for example a return value of 1.5 means one and a half months.
The TRUNC function, when applied against a numeric, will chop off all its decimals, so TRUNC(1.9999999) will return 1.
+1 is way to add a day to the date
-1 is way to remove a day to the date
In your specific case:
the instruction trunc(sysdate, 'mm') -1 remove one month to the date, in this case is one month before the current date.
the instruction trunc(months_between(date1, date2))+1 compute the difference in month between the two dates and then adds one.
Give a look at this SQLFiddle
it is a lazy way of adding or subtracting day(s)