Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I have this SQL Code that I use for my data charts in Visual Studio (vb.net) and i need to set values to this parameter with the condition but I don't know how:
ALTER Procedure Chart_daily1
(
 @DateD VARCHAR(50)
   ,@Department VARCHAR(50)
)
AS

 Begin

    SELECT    
          CONVERT(char(7), date, 120)  AS 'Month',
          DATEPART(DAY, Date) AS 'Day',
          SUM(Price) AS 'Total'
		  ,Department
FROM      [dbo].[Expenses]
where CONVERT(char(7), date, 120) = @DateD and Department =@Department
GROUP BY  DATEPART(DAY, Date),
         CONVERT(char(7), date, 120) ,
         -- DATEPART(YEAR, Date),
		  department
ORDER BY  
         Department,
          'Month',
          'Day'
   End


This is the one that I use for separate Department values to my data chart, I have a Combobox in my vb.net form which is set to a specific department(@Department) and it shows me data, and what I want now is to get all the departments when I set the value in my Combobox to 'ALL'

What I have tried:

I googled if I can set a value to the parameter in SQL and I thought if I could do it like this :

IF @Department ='All'
   set @Department IN ( 'Grocery','Electronics','Clothes','Vacation','Other','Fix Cost')

...and then the rest of the SQL code.


Any help is appreciated.
Thanks
Posted
Updated 22-Jul-20 5:42am

SQL
ALTER Procedure Chart_daily1
(
 @DateD VARCHAR(50)
   ,@Department VARCHAR(50)
)
AS

Begin
    -- HERE IS NEW CODE
    IF @Department = 'ALL' -- check if @Deparment is sending in the ALL value
    BEGIN
    SELECT    
          CONVERT(char(7), date, 120)  AS 'Month',
          DATEPART(DAY, Date) AS 'Day',
          SUM(Price) AS 'Total' ,Department
          FROM      [dbo].[Expenses]
          where CONVERT(char(7), date, 120) = @DateD --removed department returns all
          GROUP BY  DATEPART(DAY, Date),
          CONVERT(char(7), date, 120) ,
         -- DATEPART(YEAR, Date),
		  department
          ORDER BY  
          Department,
          'Month',
          'Day'
    END
    ELSE -- HERE IS OLD CODE (DO ORIGINAL QUERY WITH @DEPARTMENT) 
          SELECT    
          CONVERT(char(7), date, 120)  AS 'Month',
          DATEPART(DAY, Date) AS 'Day',
          SUM(Price) AS 'Total'
		  ,Department
          FROM      [dbo].[Expenses]
          where CONVERT(char(7), date, 120) = @DateD and Department =@Department
          GROUP BY  DATEPART(DAY, Date),
         CONVERT(char(7), date, 120) ,
         -- DATEPART(YEAR, Date),
		  department
         ORDER BY  
         Department,
          'Month',
          'Day'
End
 
Share this answer
 
Comments
Member 13410460 22-Jul-20 10:58am    
thank you mate, that worked beautifully
Best Regards
An alternative to Solution 1 is to use CASE in the WHERE clause then you don't need the IF e.g.
SQL
SELECT
    CONVERT(char(7), [date], 120)  AS 'Month',
    DATEPART(DAY, [Date]) AS 'Day',
    SUM(Price) AS 'Total' ,Department
FROM Expenses
WHERE CONVERT(char(7), date, 120) = @DateD 
AND Department LIKE CASE WHEN @department = 'ALL' THEN '%' ELSE @department END
GROUP BY  DATEPART(DAY, [Date]), CONVERT(char(7), [Date], 120) ,department
ORDER BY  Department, 'Month','Day'
 
Share this answer
 
Comments
Member 13410460 22-Jul-20 11:43am    
This is also useful, THanks mate for the help.
Best REgards
CHill60 22-Jul-20 11:45am    
My pleasure

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900