Case when statement with where clause

Analyst_SQL 3,576 Reputation points
2022-09-06T11:11:58.353+00:00

I am using case when condition with where clause below

 CREATE TABLE #ItemMasterFile(CodeItem INT,Descriptionitem VARCHAR(50))    
  CREATE TABLE #Bigbalprd (BID INT,CodeItem INT,prdQTY INT,Entrydate date)    
  
 INSERT INTO #ItemMasterFile VALUES(1,'A')    
  INSERT INTO #ItemMasterFile VALUES(2,'B')    
  INSERT INTO #ItemMasterFile VALUES(3,'C')    
  INSERT INTO #ItemMasterFile VALUES(4,'D')    
  INSERT INTO #ItemMasterFile VALUES(5,'e')    
  INSERT INTO #ItemMasterFile VALUES(6,'f')    
  INSERT INTO #ItemMasterFile VALUES(7,'g')    
  INSERT INTO #ItemMasterFile VALUES(8,'h')    
  INSERT INTO #ItemMasterFile VALUES(9,'K')    
  INSERT INTO #ItemMasterFile VALUES(10,'L')    
  INSERT INTO #ItemMasterFile VALUES(11,'M')    
  
  
   INSERT INTO #Bigbalprd VALUES(10011,1,1,'2022-09-06')    
  INSERT INTO #Bigbalprd VALUES(10012,3,1,'2022-09-05')    
  INSERT INTO #Bigbalprd VALUES(10013,11,1,'2022-09-05')    
  INSERT INTO #Bigbalprd VALUES(10014,10,1,'2022-09-05')    
  INSERT INTO #Bigbalprd VALUES(10015,8,1,'2022-09-05')    
  INSERT INTO #Bigbalprd VALUES(10016,9,1,'2022-09-05')    
  INSERT INTO #Bigbalprd VALUES(10017,9,1,'2022-09-05')    
  INSERT INTO #Bigbalprd VALUES(10018,8,1,'2022-09-05')    
  
  
 Declare @BID int=10011  
 Declare @Empid int=2  
 Declare @Todaydate date=getdate();  
 Declare @Todaydatef date = DATEADD(day, -30, getdate())  
  
select B.BID,I.Descriptionitem ,B.prdQTY QTY  
 from #Bigbalprd B inner join #ItemMasterFile I on I.CodeItem=B.CodeItem  
 where B.BID=@BID and   b.EntryDate=  case @Empid  when 4  then @Todaydate   
                                                                                                 when 2 then @Todaydatef end  

I want, when i pass @Empid =4 then today date must display and if i pass @empid =2 then it must ignore date parameter,i tried above ,but not getting expected result

Developer technologies Transact-SQL
SQL Server Other
{count} votes

2 answers

Sort by: Most helpful
  1. Bjoern Peters 8,921 Reputation points
    2022-09-06T12:42:17.623+00:00

    Try this:

     select B.BID,  
    		I.Descriptionitem,  
    		B.prdQTY QTY,  
    		case @Empid   
    				when 4 then @Todaydate  
    				when 2 then @Todaydatef   
    			end as	EntryDate  
      from #Bigbalprd B inner join #ItemMasterFile I on I.CodeItem=B.CodeItem  
      where B.BID=@BID  
    

    Maybe this is what you are expecting.

    1 person found this answer helpful.

  2. Jingyang Li 5,896 Reputation points Volunteer Moderator
    2022-09-06T15:59:24.157+00:00
     select B.BID,I.Descriptionitem ,B.prdQTY QTY  
      from #Bigbalprd B inner join #ItemMasterFile I on I.CodeItem=B.CodeItem  
      where B.BID=@BID   and  ( (@Empid=4 and b.EntryDate=@Todaydate )    
    Or   @Empid=2 )  
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.