SQL Update if parameter is not null or empty
not sure what you are trying to achieve if it is blank, but I would try using IsNull()
I don't think there is an IsBlank()
, but it shouldn't be too hard to write yourself
Using just IsNull
your query would look something like...
Update [Users]
set FirstName = IsNull(@FirstName, FirstName),
City = IsNull(@City, City)
....
Where ...
this will Update the row with the param value if they are NOT null, otherwise update it to itself aka change nothing.
SQL Server UPDATE: set column if parameter is not null
ISNULL
will work fine here:
UPDATE [dbo].[State]
SET Bar = @Bar,
Something = ISNULL(@Something,Something)
WHERE...
SQL if not null update
If email OR password is not null update them otherwise let them be as they are.
You can use case
expressions for this. I think that the logic you want is:
UPDATE users
SET
username = Param1
email = case when email is not null then Param2 end,
password = case when password is not null then Param3 end
WHERE id = Param4;
Or if you want to update email and password if both are not null
then:
UPDATE users
SET
username = Param1
email = case when email is not null and password is not null then Param2 end,
password = case when email is not null and password is not null then Param3 end
WHERE id = Param4;
Now the question was updated and I understand that you want to perform the update if and only if both email and password parameters are not empty strings. So you actually want filtering. I would phrase this as:
UPDATE users
SET username = Param1, email = Param2, password = Param3
WHERE id = Param4 and Param2 <> '' and Param3 <> ''
Or if you want to separate the logic for both parameters:
UPDATE users
SET
username = Param1,
email = case when Param2 <> '' then Param2 else email end,
password = case when Param3 <> '' then Param3 else password end
WHERE id = Param4;
SQL Query for If not null,then update or else keep the same data
May be something like this
Update T
SET A = CASE WHEN A IS NOT NULL THEN 'Value' ELSE A END,
B = CASE WHEN B IS NOT NULL THEN 'Value' ELSE B END,
C = CASE WHEN C IS NOT NULL THEN 'Value' ELSE C END
D = CASE WHEN D IS NOT NULL THEN 'Value' ELSE D END
FROM Table1 T
How to update Columns if value is not NULL
This will update only the values that are not null. If the value is null, the column is updated back to its own value.
UPDATE [dbo].[TBL_Log]
SET User_Names = isnull(@User_Names, User_Names)
, [Start] = isnull(@start, [Start])
, [End] = isnull(@End, [End])
, [Count] = isnull(@Count, [Count])
where User_id = @User_id
Sql query for updating database if value is not null?
Without knowing your database it's tough to be specific. In SQL Server the syntax would be something like ...
UPDATE MyTable
SET
Field1 = IsNull(@Field1, Field1),
Field2 = IsNull(@Field2, Field2),
Field3 = IsNull(@Field3, Field3)
WHERE
<your criteria here>
EDIT
Since you specified SQLLite ...replace my IsNull
function with COALESCE()
or alternately look at the IfNull
function.
How do check if a parameter is empty or null in Sql Server stored procedure in IF statement?
that is the right behavior.
if you set @item1
to a value the below expression will be true
IF (@item1 IS NOT NULL) OR (LEN(@item1) > 0)
Anyway in SQL Server there is not a such function but you can create your own:
CREATE FUNCTION dbo.IsNullOrEmpty(@x varchar(max)) returns bit as
BEGIN
IF @SomeVarcharParm IS NOT NULL AND LEN(@SomeVarcharParm) > 0
RETURN 0
ELSE
RETURN 1
END
SQL Stored Procedure: If variable is not null, update statement
Use a T-SQL IF
:
IF @ABC IS NOT NULL AND @ABC != -1
UPDATE [TABLE_NAME] SET XYZ=@ABC
Take a look at the MSDN docs.
ISNULL() OR Is NULL in UPDATE statement
Those queries won't do the same thing.
Update Table set REC_ID = isnull(REC_ID,'')
This one will update each record and if REC_ID
is NULL
it will set it to ''
.
Update Table set REC_ID = '' where REC_ID is NULL
This one will only update records containing a null value in REC_ID
, and set it to ''
.
While both of them will end up giving the same result, the second one will be executed on less records (except if every REC_ID
is NULL
), it should be faster.
Related Topics
MySQL Count(*) on Multiple Tables
Copy from One Database to Another Using Oracle SQL Developer - Connection Failed
How to Perform a Left Join in SQL Server Between Two Select Statements
Getting List of Table Comments in Postgresql
How to Find Top Three Highest Salary in Emp Table in Oracle
Select Top Distinct Results Ordered by Frequency
Remove Duplicates from SQL Union
How to Constraint No Empty Strings on an Nvarchar Column
How to Use a SQL for Loop to Insert Rows into Database
How to Check If a Table Is Locked in SQL Server
Is There Startswith or Contains in T SQL with Variables
Using Union and Count(*) Together in SQL Query
How to Return a Table from a Stored Procedure
What's the Best Way to Store Sort Order in SQL
How to Get the Min() of Two Fields in Postgres
Should I Set Max Pool Size in Database Connection String? What Happens If I Don'T