SQL: Update a field only if a condition is met - sql

I want to update a column only if a condition is met. So for the column "Type", I want to change its value to "MongoDB" only if its current value isn't "MongoDB" This is what I'm using:
UPDATE Report
SET Type =
CASE
WHEN Type <> 'MongoDB' THEN 'MongoDB'
ELSE Type
END
WHERE Id = x
The problem is:
Even when the Type is "MongoDB" I still see
(1 row(s) affected)
in my SQL result. The whole point of this exercise was to reduce db operations when no needed. Why is it still modifying the record when the condition is not met?
Thanks.

Why not simplify it like this?
UPDATE Report
SET Type = 'MongoDB'
WHERE Id = x AND Type <> 'MongoDB'
But to answer your question you are still setting a records value even though its to the existing value. The record also comes back in the where clause so you will always have 1 row affected regardless of your CASE statement.

Related

How to change data value in a column if it match with flag=1

So assume there are 2 cols Name and Flag. I want to change the name of the data which have flag=1.
This is the table (https://i.stack.imgur.com/LILzV.png)
And this is what i want.(https://i.stack.imgur.com/JPZWj.png)
I have first created a flag according to some condition. Now im not able to fig out the next move.
update TableName
set name = 'zumba'
where flag = 1
Edit: as pointed out by jarlh, single quotes are the correct syntax

SQL Working with Bit values to get either true or false

I have a SQL table that has a bit column named "Split".
I need to create a stored procedure that receives a parameter named WithSplit of type bit.
If WithSplit is false then I need to get all the records that have the "Split" column equal to false.
If WithSplit is true, then I need to get the records having the "Split" column value either true or false.
That is , if WithSplit is false then get only the records where split=withsplit, else get all records.
How should this be achieved?
I have found the answer:
(Split = #WithSplit or #WithSplit = case when #WithSplit =1 then 1 end)

PostgreSQL - Assign integer value to string in case statement

I need to select one and only 1 row of data based on an ID in the data I have. I thought I had solved this (For details, see my original question and my solution, here: PostgreSQL - Select only 1 row for each ID)
However, I now still get multiple values in some cases. If there is only "N/A" and 1 other value, then no problem.. but if I have multiple values like: "N/A", "value1" and "value2" for example, then my case statement is not sufficient and I get both "value1" and "value2" returned to me. This is the case statement in question:
CASE
WHEN "PQ"."Value" = 'N/A' THEN 1
ELSE 0
END
I need to give a unique integer value to each string value and then the problem will be solved. The question is: how do I do this? My first thought is to somehow convert the character values to ASCII and sum them up.. but I am not sure how to do that and also worried about performance. Is there a way to very simply assign a value to each string so that I can choose 1 value only? I don't care which one actually... just that it's only 1.
EDIT
I am now trying to create a function to add up the ASCII values of each character so I can essentially change my case statement to something like this:
CASE
WHEN "PQ"."Value" = 'N/A' THEN 9999999
ELSE SumASCII("PQ"."Value")
END
Having a small problem with it though.. I have added it as a separate question, here: PostgreSQL - ERROR: query has no destination for result data
EDIT 2
Thanks to #Bohemian, I now have a working solution, which is as follows:
CASE
WHEN "PQ"."Value" = 'N/A' THEN -1
ELSE ('x'||LPAD(MD5("PQ"."Value"),16,'0'))::bit(64)::bigint
END DESC
This will produce a "unique" number for each value:
('x'||substr(md5("PQ"."Value"),1,8))::bit(64)::bigint
Strictly speaking, there is a chance of a collision, but it's very remote.
If the result is "too big", you could try modulus:
<above-calculation> % 10000
Although collisions would then be a 0.01% chance, you should try this formula against all known values to ensure there are no collisions.
If you don't care which value gets picked, change RANK() to ROW_NUMBER(). If you do care, do it anyway, but also add another term after the CASE statement in ORDER BY, separated by a comma, with the logic you want - for example if you want the first value alphabetically, do this:
...
ORDER BY CASE...END, "PQ"."Value")
...

IS NULL not working in WHERE clause of SQL

I have written a Stored procedure in which in given table column named xx can have 0,1 or null. When I give the below condition. SP is ignoring null and returning data only for 0 value.
WHERE (CAR_INSPECTION_NEW_TEST.NODAMAGEFLAG is null OR
CAR_INSPECTION_NEW_TEST.NODAMAGEFLAG = 0) AND
CAR_INSPECTION_NEW_TEST.ISSUBJECT_TODELIVER = 0
Can any one tell what is the problem?
By simple logic it will not return rows where NODAMAGEFLAG is null as long as ISSUBJECT_TODELIVER = 0 is not also valid (because of the AND).
So, check your data please.

SELECT a cell and UPDATE at the same time?

I've got a SQL Server table in which I have a column where I would like to select the current value and increment by one, is there a way to do this in a single query? This in order to mitigate the chance, however small it might be, that someone else gets the same number.
Something along the lines of this pseudo code:
SELECT NumSeriesCurrent
FROM NumSeries
(UPDATE NumSeries SET NumSeriesCurrent = NumSeriesCurrent+1)
WHERE NumSeriesKey='X'
To update the value and get the value in NumSeriesCurrent previous to the update you can use
UPDATE NumSeries
SET NumSeriesCurrent += 1
OUTPUT DELETED.NumSeriesCurrent
WHERE NumSeriesKey='X'