Flexible month setting in date
Hi I currently have the following WHERE statement which is "static" WHERE Practice.ibvSalesByJob.[Date] >= N'05/08/2011 00:00:01' AND Practice.ibvSalesByJob.[Date] <= N'05/16/2011 00:00:00' I would like to make it flexible like I have with other things in Reqport Builder like making the user choose which year (Practice.ibvSalesByJob.[Year] = @Year), but where it differs to the above I only want the month to be changeable so in this example it would always be /08/2011 00:00:01 - /16/2011 00:00:00 and the user only has to change which month they are reporting on as the other dates dont change. Is there a way this can be done? Something like the following if it exist and is possible? WHERE Practice.ibvSalesByJob.[Date] >= N'@Month/08/2011 00:00:01' AND Practice.ibvSalesByJob.[Date] <= N'@Month/16/2011 00:00:00' Can I do something like the above if I manually add fields to a Parameter like 01,02,03 etc? Just to add the data from the SQL database is under Date and is shown in the following format for example "4/30/2011 12:00:00 AM", so its the 4 I want to make changeable. Cheers
July 8th, 2011 10:17am

Hi Chris, Try casting your variable to a string then concatinating to the rest of the date (time). WHERE Practice.ibvSalesByJob.[Date] >= cast(@Month AS char(2)) + '/08/2011' but then you might as well @month and @year as well Hope this helps , Robert Edgson
Free Windows Admin Tool Kit Click here and download it now
July 8th, 2011 3:55pm

Hi Robert Sorry for the delay in getting back to you, this is EXACTLY what I need!! Thankyou very much for this Chris
July 11th, 2011 5:12am

Hi Chris Johnson-Barringtons, Based on your description, there is a need to add a parameter as the month date part in where clause, while the other date parts will be static. So that we can filter data with same period of different month. To fulfill month part to be dynamical, we can use cast function as Robert Edgson mentioned. But, we should be aware of type matching. So the where clause can be modified as: where Practice.ibvSalesByJob.[Date] >=Cast(@Month+'/08/2011' AS Date) and Practice.ibvSalesByJob.[Date] <=Cast(@Month+'/16/2011' AS Date) More information about Cast function, please refer: http://msdn.microsoft.com/en-us/library/ms187928(v=SQL.100).aspx , Thanks, Lola Wang Please remember to mark the replies as answers if they help.
Free Windows Admin Tool Kit Click here and download it now
July 11th, 2011 5:38am

Hi Just done that and both work so is the one provided by Lola best to use? I have edited @Month and specified what values are available and listed 01 - January = 1 for instance which should minimise user input error there shouldnt be a problem with this, should there? Again thanks to you both, you have both been very helpful. Cheers
July 11th, 2011 8:43am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics