SQL: Get row using dynamic date - mysql

First let me start by saying I am a Junior so I do apologise if this is a really stupid question or not possible.
I have a WHERE clause in an SQL script that gets rows if the date is later than a certain date. Like so:
WHERE
u.created > UNIX_TIMESTAMP('2018-04-01')
The month and day will always be the same but the year needs to be changed every year with the current year. So, next April I will have to make a change to the code to read:
WHERE
u.created > UNIX_TIMESTAMP('2019-04-01')
I was wondering if it's possible to make the year update to the current year IF the day and month are past 1st April

One option is to use DATE_FORMAT with the NOW() function as the source for the current year:
WHERE
u.created > UNIX_TIMESTAMP(DATE_FORMAT(NOW() ,'%Y-04-01'))
Demo

Related

SSRS Expression for getting current week Sunday-Saturday

I am looking to make a report that gets the current week from sunday to saturday, i was thinking oh the headers of it having the date of the current week of running the report, does anyone know how to do that?
I assuming what you are asking is how to filter your dataset to show only records within the same week as the current week?
if so then you can do something like this
=WEEK(today()) = WEEK(Fields!myDateField.Value)
This will return True if the week is in the same week as the current week.
For more info on the WEEK function see here
https://learn.microsoft.com/en-us/previous-versions/sql/sql-server-2008/aa337345(v=sql.100)

Week To Date Based Off Current Date

I am looking for an expression that will allow to me find the date of Monday of the week of my Date Field, to be used in a filter for a tablix.
Example, my date field today is 22/01/2019. I would like an expression that will return 21/01/2019. If the date was 26/01/2019, it should still return 21/01/2019.
For next week 31/01/2019 would return 28/01/2019.
Week starting Monday going to Sunday.
If its also possible for a similiar expression but to find the beginning of the month as well?
Is that possible?
Many thanks
To get the date of last Monday that occurred you can use something like
=Today.AddDays(1-WeekDay(Today(),FirstDayOfWeek.Monday))
and for the first of the month its just
=DateSerial(Year(Today()), Month(Today()), 1)
Both the above are based on the today() function, if you need them based on a date parameter, then the expression is a little different but your question stated "today".
monday of the week use the following expression:
DateAdd("d",DatePart(DateInterval.WeekDay,Fields!myDatefield.Value,0,0)+1,Fields!myDatefield.Value)
first day of the month use the following expression:
=DateAdd("d",-(Day(Fields!myDatefield.Value)-1), Fields!myDatefield.Value)

SSRS date to and From parameters default to current financial year

I have a Dynamics CRM SSRS report which has two date parameters to filter records when report is executed.
When running the report I would like the parameters to default to the current financial year eg. if today is 01/10/2017 then From should default to 01/04/2016 and To should default to 31/03/2017.
Can this be done and if so what is the best way?
Not sure why people are over complicating this. I understand the common need for date tables, I use them a lot but it's pretty easy to do in an expression.
The From date expression needs to be
=DATESERIAL(Year(Now()) - (IIF(format(now(),"MMdd") > "0331",0,1)) ,4,1)
and the to date expression needs to be
=DATESERIAL(Year(Now()) + (IIF(format(now(),"MMdd") > "0331",1,0)) ,3,31)
All we are doing is creating a date value that is either 1st April current year or 31st March current year then depending on if the current date is on or before the 31st March we adjust the year by 1.
I'M ASSUMING YOUR DATES ARE WRONG IN YOUR EXAMPLE. You said you wanted current financial year but your sample showed last financial year. Anyway, if the code above is incorrect just adjust the Year(Now()) to be (Year(Now())-1) .

Netezza What is the best way to get the first day of the month for a date?

Select to_date(to_char(date_part('year',current_date),'0000') || trim(to_char(date_part('month',current_date),'00')) || '01','YYYYMMDD')
So far, this is the best I can come up with.
I am also unable to find a comprehensive language reference for Netezza SQL that has all functions in it, so please include a source in your answer.
Use date_trunc('month', current_ date), which is documented here.
Start with the date, for what I needed today, was using current date/month. Then find the last day of the current month. That's the outer shell for the first day as a systematic output:
start with the current date
use add_months (-1) to subtract a month;
find the last day of the previous month;
add 1 day.
I then decided to test out on February (leap year and normal), finding the first from the first, and then adding in finding the first of the next month. Also checked looking at a month ago from a month ending 31st and 30th. I think this should be flexible.
select current_date,
last_day(add_months(current_date,-1))+1 as firstdt_currmos,
last_day(current_date) as lastdt_currmos
;
select last_day(add_months('02-29-2016',-1))+1 as firstdt_febleap,
last_day(add_months('02-28-2019',-1))+1 as firstdt_febnorm,
last_day(add_months('07-01-2019',-1))+1 as firstdt_first,
last_day(add_months(current_date,-1))+1 as firstdt_currmos,
last_day(current_date)+1 as firstdt_nextmos,
last_day(add_months('07-31-2019',-1))+1 as firstdt_mos31st,
date(add_months('07-31-2019',-1)) as mosago_31st,
date(add_months('06-30-2019',-1)) as mosago_30th
;

Current and Previous Month Revenue in Report

In Access 2013, I have a report showing revenue for each month of the year. I was success in getting a formula to name the previous month:
=Format$(DateAdd("m",-1,([ApptDate])),"mmmm",0,0)
What I am trying to get, is the revenue for the previous month for comparison, like a distribution chart. This is as close as I have gotten to something working, but the sum of the revenue for the previous month is too low (maybe only for a day's revenue?) Could anyone please help me put the following formula in the right order with right parameters to get the date needed?
=Sum(DateAdd("m",-1,([revenue])))
If I understood your problem correctly, you could use the DSum function by setting as criteria the month of the date to the previous month.
=DSum("[revenue]","YourTableName","Month([ApptDate])=" & Month(DateAdd("m",-1,Date())))