Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Getting the year from a date variable

I have an input box containing a variable vStartDate. In the script I want to set another variable vStartYear which is just the year of vStartDate. However I'm having all kinds of trouble achieving what should be a really simple thing! In my script I have the following:

SET vStartYear = Year($(vStartDate));

But if I trace it, it seems to convert it to the string "Year(01/01/2007)". I have tried using the Date function but this also appears as a string. I have also tried various other things, the "best" I've managed is for it to be set to 1899. What am I doing wrong? It's driving me crazy!

Many thanks.

Paul

9 Replies
Not applicable
Author

it would seem that the format in which your vStartDate variable is being typed into the input box is not being recognized by QV as a timestamp. For example this works correctly:

Set CurrentDate=Today(2);
Set CurrentYear=Year($(CurrentDate));

Not applicable
Author

Try this:

SET vStartYear = Year('$(=vStartDate)');


Dates can sometimes do funny things on the dollar sign expansion. You may not need an equals sign and the single quotes.

GabrielAraya
Employee
Employee

Try the following:

Let vStarYear = Year($(vStarDate))

gabriel

disqr_rm
Partner - Specialist III
Partner - Specialist III

Do you have to use SET? If not, try following. It eliminates all the confusions of dollar sige, etc.

LET vStartYear = Year(vStartDate);

Not applicable
Author

Thanks Rakesh, I didn't realise you could use LET and specify variables like that. I'm now getting 1899 as the answer, with my input box containing 01/01/2007. If I add the following line:

LET vStartDate1 = Date(vStartDate,'DD/MM/YYYY')

and reload in debug mode, I can see that vStartDate1 is 30/12/1899. So the problem does appear to be with the input box. How do I get that to recognise that it's a date?

prieper
Master II
Master II

Hi,

you need to change the variables, i.e. LET vStartDate = Date(Year(AnotherDateField), 'DD/MM/YYYY');
Currently you have determined the date-value of an empty variable, vStartDate.

Peter

disqr_rm
Partner - Specialist III
Partner - Specialist III

I don't understand. See enclosed. It's working fine for me. Can you attach a sample here?

Not applicable
Author

I think I've fixed it by going to Document Properties, Variables, setting the constraints of vStartDate to Custom and making the custom expression date#. In the debugger, vStartDate now displays as 01/01/07 and vStartYear as 2007. Thanks for putting me on the right track.

disqr_rm
Partner - Specialist III
Partner - Specialist III

Paul, thanks for your feedback. Glad that it's fixed now.