Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have the following SQL statement in which I want to remove a certain column when a condition is met. Below are my SQL statement:

SQL
CASE @p_ReportType WHEN 1 THEN NULL WHEN ISNULL(@p_ReportType, 0) THEN row_number() OVER (ORDER BY  EventId desc)+1 END AS MeasurementGroupId 


For the above scenario, when the @p_ReportType is 1 then the MeasurementGroupId is removed else the MeasurementGroupId is remained. Any helps are appreciated.
Posted
Comments
_Maxxx_ 8-Mar-15 22:34pm    
When you say you want the column removed, do you mean not included in the results at all, or returned as Null?
If you want to not include the column, then you will have to resort to dynamic SQL - i.e. build up the sql string and execute it.
If you are happy to return null, then
Select Case When @p_reportType = 1 Then Null Else row_number() END as MeasurementGroupId
Jamie888 8-Mar-15 22:39pm    
I have tried execute it using different ways. First would be only one condition whereas it returns the result I want. After that I add in the second condition whereas it returns the result I do not want. So I would prefer the MeasurementGroupId is returned as 0 when first condition is met.
ZurdoDev 9-Mar-15 8:39am    
Where exactly are you stuck? This question does not make sense.

1 solution

Above code does not remove any column! It "evaluates a list of conditions and returns one of multiple possible result expressions".

See:
CASE (sql)[^]
 
Share this answer
 

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