I am trying to do something that should be fairly simple but ISNULL isn't doing what I thought it would.
Basically I have a stored procedure and I am expecting either PARAM1 OR PARAM2 to have a matching value in my table.
SELECT * FROM MyTable WITH (NOLOCK)
WHERE
field1 = ISNULL(#PARAM1 ,field1 )
AND
field2 = #PARAM2
This works fine until I have NULL fields in my row then it excludes those results. Is there a different method that can cater for this?
ISNULL replaces the first value with the second value, so only if your parameter #PARAM1 is NULL does it replace it with PARAM1. I assume you're not passing in NULL values, so that's probably not what you want. More likely you just want to say
WHERE
(Field1 = #PARAM1 OR Field1 IS NULL)
AND
Field2 = #Param2
I Suppose you could use ISNULL in this fashion too:
ISNULL(Field1, #PARAM1) = #PARAM1
null is special in sql. If you do any comparisons on a column any rows that have a null for that column will be excluded.
SELECT * FROM MyTable WITH (NOLOCK)
WHERE
(PARAM1 = #PARAM1 or PARAM1 is null)
AND
(PARAM2 = #PARAM2 or PARAM2 is null)
Use
field1 = ISNULL(#PARAM1, A_REPLACEMENT_VALUE_IF_PARAM1_IS_NULL)
This will evaluate to-
field1 = #PARAM1 if #PARAM1 IS NOT NULL.
field1 = A_REPLACEMENT_VALUE_IF_PARAM1_IS_NULL if #PARAM1 IS NULL
EDIT:
Try these:
--If you want to ignore the WHERE clause if PARAM1/2 is null
ISNULL(field1, DEFAULT_VALUE) = ISNULL(#PARAM1, ISNULL(field1, DEFAULT_VALUE))
OR
ISNULL(field2, DEFAULT_VALUE) = ISNULL(#PARAM2, ISNULL(field2, DEFAULT_VALUE))
OR
--To get all rows with field1/2 as PARAM1/2 and ignore everything else
field1 = #PARAM1 OR field2 = #PARAM2
Related
I have a query where I select data from a table using TSQL. When one of the fields (field1) has a certain value, I want to change the value of two other fields (field2 and field3) to NULL depending on the value of field1. I've browsed stack overflow and see many answers saying to UPDATE the table like this:
UPDATE MyTable
SET MyField = NULL
WHERE MyField = ''
But Update changes a table, right? I don't want to change my table data. I want to change my query results. Is there a way to change results of a query like I want?
If you have table field1 | field2 | field3 and you want to query field2 and field3, but in the case of field1 is NULL, these two other field should be also NULL. So in your case:
SELECT field1
, CASE WHEN field1 IS NULL THEN NULL ELSE field2 END AS field2
, CASE WHEN field1 IS NULL THEN NULL ELSE field3 END AS field3
FROM dbo.MyTable
Update (1) after clarification in comment:
SELECT field1
, CASE WHEN field1 = 'IR' THEN NULL ELSE field2 END AS field2
, CASE WHEN field1 <> 'IR' THEN NULL ELSE field3 END AS field3
FROM dbo.MyTable
As described in CASE (Transact-SQL) documentation:
WITH Data (value) AS
(
SELECT 0
UNION ALL
SELECT 1
)
SELECT
CASE
WHEN MIN(value) <= 0 THEN 0
WHEN MAX(1/value) >= 100 THEN 1
END
FROM Data ;
Or in your case:
SELECT
MyField = CASE
WHEN MyField = '' THEN NULL
ELSE MyField
END
FROM MyTable
I did test runs just adding the two CASE statements to the Select part of my main query. Worked well. Thanks for the help!
Here are the exact statements I used. I named the resulting fields Case# and Payee instead of CaseNo and VendorName, because SQL was calling the results CaseNo1 and VendorName1:
CASE WHEN Accounting_Categories.Name LIKE 'I%' then NULL Else CaseNo END as Case#,
CASE WHEN Accounting_Categories.Name LIKE 'I%' then VendorName Else NULL END as Payee
I need to query a table for null values but on different fields. I have been setting up my queries independently and running them one at a time to view my results.
EX:
Query 1:
select * from TABLE1 where FIELD1 is null and FIELD2 is NOT null
Query 2:
Select * from TABLE1 where FIELD6 = 'YES' and FIELD2 is null
Query 3:
select * from TABLE1 where FIELD4 = 'OUTSIDE' and FIELD7 is not null
Is there a way to set up a single query which will allow me retrieve data from a single table but run queries where the conditions are different?
It looks like you could use the or operator:
select * from TABLE1
where (FIELD1 is null and FIELD2 is NOT null)
or (FIELD6 = 'YES' and FIELD2 is null)
or (FIELD4 = 'OUTSIDE' and FIELD7 is not null)
I have a stored procedure that takes a single optional parameter. If the parameter exists it should update the 1 record, otherwise everything else. Are there a way to do in with a single SQL statement without using dynamic SQL?
Something like this:
CREATE PROCEDURE UpdateEmployees (#PersonID varchar(10) = null)
AS
BEGIN
UPDATE Employees
SET Field1 = 'Changed'
WHERE (PersonID Is Not Null OR PersonID = ISNULL(#PersonID, '')) --this not 100% yet.
END
You're almost there - you need to perform the update on a row with the supplied #PersonId or if the parameter is null (not if it's not, as you currently have).
Additionally, The isnull is redundant, as null will return "unknown" (which is not true) on any = operation.
CREATE PROCEDURE UpdateEmployees (#PersonID varchar(10) = null)
AS
BEGIN
UPDATE Employees
SET Field1 = 'Changed'
WHERE (#PersonID IS NULL OR PersonID = #PersonID)
END
I have a database table with many columns. Is there sql that will update records in that table where all or only specific columns are handled in such a way that if NULL is passed for any column value that the existing value not be changed?
Currently I can use solutions like these
UPDATE table
SET column1 = COALESCE(#param1, column1),
column2 = COALESCE(#param2, column2),
...
WHERE id = #id
or
UPDATE table
set column1 = isnull(#param1,column1),
column2 = isnull(#param2,column2)
They both works well, though sometimes I want to explicitly save null in any column and I can't do it with the above solutions. How?
One approach is to declare two parameters for each column, the first contains the value, the second is a bit instructs the query to insert null explicitly.
Example
create table example (column1 nvarchar(255), column2 nvarchar(255))
create procedure pUpdate(
#column1 nvarchar(255) = null,
#nullColumn1 tinyint = 0,
#column2 nvarchar(255) = null,
#nullColumn2 tinyint = 0
) as
BEGIN
update example
set column1 = Case When #nullcolumn1 = 1
Then NULL ELSE IsNull(#column1, column1) End
set column2 = Case When #nullcolumn2 = 1
Then NULL ELSE IsNull(#column2, column2) End
END
Then when calling from code, you only have to pass the parameters that you know need updating, or explicitely set the #nullcolumn to force a null.
Is there a way to create UPDATE stored_procedure with parameters like:
#param1 int = null,
#param2 int = null,
#param3 nvarchar(255) = null,
#param4 bit = null,
#id int
and with UPDATE statement which will update only fields which are not NULL
so if I execute
spUpdateProcedure #param1=255, #id=1
if will update record #id=1 but it will change only field #param1 and will ignore changes to other #param2,3,4.
In other words, it wont change value for null in #param2,3,4
Thanks.
UPDATE YourTable
SET Column1 = COALESCE(#param1, Column1),
Column2 = COALESCE(#param2, Column2),
...
WHERE id = #id
on your edit statement, you can do this
update table
set
column1 = isnull(#param1,column1),
column2 isnull(#param2,column2)