Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

how to compare two date values stored in variables ?

Hello Everybody,

I am trying to compare two date value stored in two variables.

This is what I am trying :

let curr_date= date(today(),'MM/DD/YYYY');

LET PREV_DATE= DATE ('06/07/2011','MM/DD/YYYY');

if date($(PREV_DATE)) < date($(curr_date))

let hilo= 1;

else

LET hilo=0;

end if

I have to assign the value of variable hilo based up on this condition.

But I am getting error

--    

Script line error:

if date(06/07/2011) < date(07/15/2011)

--

Can anybody help me out and tell me how to do the comparison ?

1 Solution

Accepted Solutions
Miguel_Angel_Baeyens

Hello,

If you are doing that in the script, the syntax needs the THEN keyword

IF Date($(PREV_DATE)) < Date($(curr_date)) THEN

That should work

Miguel Angel Baeyens

BI Consultant

Comex Grupo Ibérica

View solution in original post

8 Replies
kaushiknsolanki
Partner Ambassador/MVP
Partner Ambassador/MVP

HI,

   Try without date function. like this.

  

let curr_date= date(today(),'MM/DD/YYYY');

LET PREV_DATE= DATE ('06/07/2011','MM/DD/YYYY');

if $(PREV_DATE) < $(curr_date)

let hilo= 1;

else

LET hilo=0;

end if

Regards,

Kaushik Solanki

Please remember to hit the 'Like' button and for helpful answers and resolutions, click on the 'Accept As Solution' button. Cheers!
Miguel_Angel_Baeyens

Hello,

If you are doing that in the script, the syntax needs the THEN keyword

IF Date($(PREV_DATE)) < Date($(curr_date)) THEN

That should work

Miguel Angel Baeyens

BI Consultant

Comex Grupo Ibérica

Not applicable
Author

Use then after If expression

i.e If condition then

        expression

     else

     end if

Not applicable
Author

Thank you all for you reply.

Sorry I forget to put then .

Now I have tried  :

let curr_date= date(today(),'MM/DD/YYYY');

LET PREV_DATE= DATE ('06/07/2011','MM/DD/YYYY');

if date($(PREV_DATE),'MM/DD/YYYY') < date($(curr_date),'MM/DD/YYYY') then

let hilo= 1;

else

LET hilo=0;

end if

But according to the logic the value of hilo must be 1.

But here I am getting value of hilo as 0.

can you explain what is the problem , or where am I going wrong?

Thanks in advance !!!!

Miguel_Angel_Baeyens

Hi,

You are comparing dates, and dates as returned by the Date() function are literals, so you need to single quote them

IF '$(PREV_DATE)' < '$(curr_date)' THEN

Hope that helps.

Miguel Angel Baeyens

BI Consultant

Comex Grupo Ibérica

Not applicable
Author

Thanks for the help.

Can you through some more light on the Date() function, so that I would not do the same mistake again.

I didn't got your point.

Thanks in advance !!!!!

Not applicable
Author

Hi Dear,

Use Correect syntax of if-else condition

Syntax is:- IF(<CONDITION>,<THEN EXPRESSION>,<ELSE EXPRESSION>)

example:- IF(DATE($(PREV_DATE)) < DATE($(CURR_DATE)) , LET hillo=1 , LET hillo=0)

Miguel_Angel_Baeyens

Hi,

It all has to do with the way QlikView uses variables and the difference bewteen LET and SET, and incidentally, dollar expansion. I posted about this some months ago, in this and this post (they came from the old Community, so I just have edited them to keep a readable format).

Besides, a Date() is a literal value, so when you refer to it in a conditional, you will not need to use quotes when it's not expanded, as in

IF Date(PREV_DATE) < Date(curr_date) THEN

Because you are comparing equal formats (numeric formats, a date is evaluable as a numeric value in QlikView). When you expand (you use the $() so the values within are evaluated) and the result is a literal for example "15/07/2011", you do need to quote them

IF '$(PREV_DATE)' < '$(curr_date)' THEN

Check both for further information, and check the Reference Manual in this regard.

Hope that makes sense and helps.

Miguel Angel Baeyens

BI Consultant

Comex Grupo Ibérica