MYSQL qlikview assign variable - mysql

Im having problems with this.
I have this qlikview script which is run on the 15th of every month. This script intends to automatically take the last date of the previous month in the format of 'YYYY-MM-DD'.
For example today is 15th January 2017. I will run my script and it will give me a date of 31th December 2016 in the format 2016-12-31.
If it is 15th December 2016, I will get 30th November 2016 in the format 2016-11-30.
I want to set the date retrieved as a variable in the format yyyy-mm-dd to be used in another query.
Basically I tried
SET #vLastDate = DATE_FORMAT(select last_day(curdate() - INTERVAL 1 MONTH), '%Y-%m-%d');
but I receive an error that it does not execute. I am doing this in qlikview. Please help me identify my problem thank you.

You are trying to execute mysql functions in qlikview? That won't work.
Try in QV script:
let vLastDate = MonthEnd(AddMonths(Today(),-1));
If you need to change the date format, play around with the Date#() function, probably something like Date#(Field, 'YYYY-MM-DD'). Today() returns YYYY-MM-DD format so it will probably be fine.

Related

Convert date format while using date_add function in SQL

I have a variable (order_date_key) that contains the number of days since 1/1/1900. For example, 42711 represents December 9, 2016 or 42711 days since 1/1/1900. I want to convert that variable to the week of the year (e.g., 2016-50)
I was able to convert the variable to yyyy-mm-dd format using the date_add function, but when I try to use the DATE_FORMAT function with it, it returns an incorrect week number. For example, 2016-12-09 converted to 2016-5, but that date isn't the 5th week of the year.
Here's the code I'm using. The format also doesn't seem to work when I use the '%' symbol.
SELECT order_date_key,
DATE_ADD('1900-01-01', order_date_key) AS Year_month_day,
DATE_FORMAT(DATE_ADD('1900-01-01', order_date_key), 'Y-u') as year_week
Sample data: 42711, 42714, 42715, 42720
Desired output: 2016-50, 2016-51, 2016-51, 2016-52

Impossible date issue with formatDate formula

I have written a simple function to track payments, and as part of the function want to keep track of the last amount paid and the date the last payment was made within a single cell.
In order to do this, I used the formatDate formula so that the date entered via a date picker would appear in the DD/MM/YY format.
For some reason, the dates entered via the date picker are not working properly with the formatDate formula. For example the date 10/2/20 (10th Feb 2020) is being returned as 41/2/20 and 20/2/20 (20th Feb 2020) is being returned as 51/2/20.
Obviously, it seems like the formula is somehow adding 31 to the dates entered, but I would like some help understanding where I am going wrong.
Here is a picture of the code for my addPayment function:
addPayment function code
It was suggested to that Utilities.formatDate() was not working with the format string of “DD/MM/YY” and I simply suggested to try “dd/MM/yy”

SSRS expression to get same month through today but for last year?

For example I would like the expected output to be 8/21/2018.
I have an expression to get the month of August of 2018 below, but not sure how to modify to get through the same date instead of returning the full month.
=DateAdd(DateInterval.Month, -12, DateAdd(DateInterval.Day,-Day(Today())+1,Today()))
If I get this correctly you want one year ago from a given date with todays day?
'Today: 8/23/2019
'Input: 8/21/2019
'Output: 8/23/2018
That would be this expression:
=DateSerial(Year(DateAdd("yyyy", -1, CDate("8/21/2019"))),
Month(CDate("8/21/2019")),
Day(Today()))

ssrs expression to calculate number of worked days in particular month

i need to calculate the number of worked days in a particular month. i have start date and end date depending on this i have to calculate the worked days in particular month. in ssrs report please help me with a ssrs expression to solve the problem.
i have a logic to do this but how to do it in a SSRS expression is a problem.
the logic is:
if(start date is in January than
start date-31/1/2014)
//this will give me the worked days in January
if(start date is not in January
start date-1/1/2014 //this gives me working days till January
(total working days)-( start date-1/1/2014)//this gives me working days after January 1
if((total working days)-( start date-1/1/2014)>31
then my answer is 31 days worked in January.
i have to convert this logic in to ssrs expression, is it possible?
is there any alternate way to do this ?
please help me with this.

How to get current month name in SSRS?

I need current month name as default perameter name in my ssrs report. How can I get current month name using an ssrs expression?
For example "27-09-2012" would become "September"
and one more i need....
27-09-2012 to previous month name as well (August)
First question:
=MonthName(Month(Fields!datefield.Value))
Second question:
=MonthName(Month(DateAdd("m", -1, Today())))
I think the second question answer might be something like that, first converting the date to month then subtracting 1 from the month value and then converting it to month name.
Further reading:
SSRS Reports get Month name from Month Index or from date
Converting month number to month name in reporting services
OFF: I would change the date format you are using to 2012-09-27 as it works in every setting and should give you peace of mind when converting date formats.
Don't subtract 1 b/c it won't work for January.
Use this:
MonthName(Month(DateAdd("m", -1, CDate(Today))))
As a note, I tried the suggestion of =MonthName(Month(today())). What I would get is #error for whatever field the expression was in. However, =MonthName(str(Month(today()))) worked for me. I am unsure of whether or not the MonthName method changed to require a string or if it is some issue with my program. Just figured I would post this in case anyone else was having the same issue.
For Previous Month i found universal way : =MonthName(Month(CDate(Today()))-1,False) for SEPTEMBER (Full Month Name) 'OR'
=MonthName(Month(CDate(Today()))-1,True) for SEP (Short Month Name)