Add column to condition based on parameter value SQL

A Coder

I'm working in a dynamic query where i need to add a column to the query if the parameter value is false.

//SQL:

SET @FilterExp = 'SELECT * from tblName where ptblnFlag = '+Convert(varchar(2),@blnFlag)+') as tbl where 1=1'+ COALESCE(NULLIF( CONVERT(varchar(8000), @FilterExp),''), '')

In the above query i need to check if the @blnFlag is false then the column can be added in the where condition. If true it need not to be there.

Something like if the condition is false it should return all values and if true the result set is based on the condition.

Pரதீப்

You just need AND/OR operator's no need of Dynamic query here

SELECT *
FROM   tblName
WHERE  ( ptblnFlag = @blnFlag
         AND @blnFlag = 0 )
        OR @blnFlag = 1 

May be am wrong with variable names but logic will be same

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Dynamic Where Condition Based on Parameter Value in sql

Python: Add a column to a crosstab based on value and condition

pandas add value to new column based on condition

SQL Force a column value based on a condition and group

SQL Hive add column based on column value

Add column based on condition

How to add a column with value based on date condition in pandas dataframe?

How to add a value across rows for a column sample based on a condition

How to add column to an existing SQL Server table based on condition?

Oracle SQL statement to update column value based on a matching condition

Add column to dataframe based on condition

add a binary column based on condition

Check if condition is true and if so add value to another column in sql

SQL query add custom value to select based on distinct column value

Add value based on column value and group in SQL query.

Replace value in column based on a condition

Lookup for a value in column based on condition

Find the column value based on condition

Subtracting a value in a column based on condition

Split a column value based on condition

keep value in a column based on condition

Create Column in sql based on condition?

SQL query based on column condition

SQL Add columns based on value from another column

SQL condition based on variable value

Find out max and min value for a column based on other column condition in SQL Server

Change all values in a particular column based on one column value or condition in SQL using CTE

Add years to a date column based on condition in pandas

Add values in new column of df based on a condition