Postgres incrementing null value in a column - sql

In Postgres 9.3 when a field in a table has a null value the following expression doesn't work:
update table_statatistic set members = members + 1 WHERE user_id = $1;
However when the field has an integer value then this query increments it by 1 without a problem.
The questions are:
Why is this happening.
How to fix it.

you need to use coalesce for checking null values
update table_statatistic set members = coalesce(members, 0) + 1 WHERE user_id = $1

Use COALESCE() instead:
The COALESCE function returns the first of its arguments that is not null. Null is returned only if all arguments are null. It is often used to substitute a default value for null values when data is retrieved for display
UPDATE table_statatistic SET members = COALESCE(members,0) + 1 WHERE user_id = $1;

Related

SQL Coalesce not returning any rows

I am using Postgres and have the following SQL statement:
SELECT *
FROM "osmlocal-dsd-de".t_osm_vehicle_image t
WHERE t.vehicle_config_id = 3
and image_type_id = 2
Which returns one row:
id vehicle_config_id cosy_url image_type_id
113 3 SomeValue 2
When I run the following:
SELECT * from "osmlocal-dsd-de".t_osm_vehicle_image t
WHERE t.vehicle_config_id = 3
and image_type_id = 2
and coalesce(t.cosy_url, '') = ''
Zero rows are returned.
I think my understanding of coalesce is wrong, because I would have expected one row still to be returned, because the cosy_url is not null.
Any advise on what I am doing wrong would be appreciated.
Your understanding of coalesce is wrong
It returns the first argument that is not null. If all arguments are null, the COALESCE function will return null
In your case t.cosy_url is not null it is equally SomeValue and your condition doesn't work because SomeValue is not equal ''
You seem to be misunderstanding coalesce(). It returns the first value that is not null.
In your case, you have:
coalesce(t.cosy_url, '')
Because t.cosy_url has a value ('SomeValue'), this evaluates to that value. The value is not '' so the expression returns false and the entire where clause returns false.
If you want non-NULL values, then use:
t.cosy_url is not null

"!="/NOT perhaps not working properly in SQLite

I have a table with about a hundred rows. It has a column is_gallery that contains either 1, 0, or NULL. If I do...
SELECT * WHERE is_gallery != 1
or
SELECT * WHERE NOT (is_gallery = 1)
it excludes the rows where is_gallery is null. I can manage to get a proper response if I do
SELECT * WHERE (is_gallery = 0 OR is_gallery is null)
But shouldn't the "!=" or NOT work? Isn't there a way to just return the rows where is_gallery doesn't equal 1 without testing for every other possibility?
You can use the IS and IS NOT operators instead of = and !=. These treat NULL like a normal value.
SELECT * FROM yourTable WHERE is_gallery IS NOT 1
The best thing to use is coalesce as in:
SELECT *
WHERE coalesce(is_gallery,0) != 1;
what coalesce does, is replaces any null value in that column with the second parameter. In the example above, any nulls in the "is_gallery" column will be replaced with 0 before it is compared with 1. So will of course return true.
On NULL realize that a NULL value isn't equal to ANYTHING - not even NULL itself. It cannot be compared - so when "comparing", it always will return FALSE. On NULL, it has a special operator which is "IS NULL" or "IS NOT NULL"

Using COALESCE to handle NULL values in PostgreSQL

I have the following query
SELECT DISTINCT
pt.incentive_marketing,
pt.incentive_channel,
pt.incentive_advertising
FROM test.pricing pt
WHERE pt.contract_id = 90000
group by 1,2,3
order by pt.incentive_marketing;
The above query returns the o/p as shown in the attached image
However I want to replace all null values by 0 using COALESCE
Please let me know how this can be achieved in above SELECT query
Now I further modified the query using coalesce as below
SELECT
COALESCE( pt.incentive_marketing, '0' ),
COALESCE(pt.incentive_channel,'0'),
COALESCE( pt.incentive_advertising,'0')
FROM test.pricing pt
WHERE pt.contract_id = 90000
group by 1,2,3
the result of which is as attached in image 2.
I still receive one row with blank values
You can use COALESCE in conjunction with NULLIF for a short, efficient solution:
COALESCE( NULLIF(yourField,'') , '0' )
The NULLIF function will return null if yourField is equal to the second value ('' in the example), making the COALESCE function fully working on all cases:
QUERY | RESULT
---------------------------------------------------------------------------------
SELECT COALESCE(NULLIF(null ,''),'0') | '0'
SELECT COALESCE(NULLIF('' ,''),'0') | '0'
SELECT COALESCE(NULLIF('foo' ,''),'0') | 'foo'
If you're using 0 and an empty string '' and null to designate undefined you've got a data problem. Just update the columns and fix your schema.
UPDATE pt.incentive_channel
SET pt.incentive_marketing = NULL
WHERE pt.incentive_marketing = '';
UPDATE pt.incentive_channel
SET pt.incentive_advertising = NULL
WHERE pt.incentive_marketing = '';
UPDATE pt.incentive_channel
SET pt.incentive_channel = NULL
WHERE pt.incentive_marketing = '';
This will make joining and selecting substantially easier moving forward.

Oracle no results, small query

I am having trouble with some sql. When I run the following query:
Select * from teleapp;
I get TONS of results. Resulst which include a column (called cashwithappyn) that has TONS of empty or null data cells. (They look empty and don't say null)
The column info is:
ColumnName ID Null? Data Type Histogram Num Distinct Num Nulls Density
CASHWITHAPPYN 54 Y VARCHAR2(1 Byte) Frequency 2 56895 0
When I try to run the following query:
Select * from teleapp where cashwithappyn = null;
or
Select * from teleapp where cashwithappyn = '';
or
Select * from teleapp where cashwithappyn not like '';
or
Select * from teleapp where cashwithappyn not in ('Y','N');
or ANY type of combination, I cannot seem to get all of the rows with nothing in cashwithappyn.
Any ideas? Please help, this is the last part of a project that I was assigned to do and I just need to figure this out.
Thanks.
Maybe the column contains blanks. In that case you can do
WHERE TRIM(CASHWITHAPYYN) IS NULL
TRIM removes all blanks before and after and if nothing is left anymore the value becomes NULL
e.g.
TRIM(' ') IS NULL -- one blank removed = true
TRIM(NULL) IS NULL -- true
Also NULL cannot be compared with = NULL but must be phrased IS NULL. NULL is not a value as such and that is why the comparison never works.
You need to use IS NULL
Select * from teleapp where cashwithappyn is null;
Logical test expressions (=, Not In, Like etc) with null result in a false so all of the following result in false
1 = NULL
1 <> NULL
NULL = NULL
NULL <> NULL
NULL NOT IN ('a','b')
NULL Not Like NULL
Additionally in oracle the zero length string is null so NOT LIKE '' will never return any rows
You'll need to use either is null, is not null or Coalesce
A co-worker and I did a bunch of research, here's what we came up with that will work. Any ideas why this works but not the others?
Select * from teleapp where nvl(cashwithappyn,'U') = 'U';

Increment int by 1 in sql server?

How can I return 1 if the SQL below returns NULL?
Something like (pseudo code):
if sql return NULL value
then set value to one
otherwise returning sql result value.
Is there any SQL for defining default value to 1 if SQL result is NULL?
SELECT Max(iCategoryOrder)+1
FROM [IVRFlowManager].[dbo].[tblCategory]
WHERE iCategoryLevel = 1
Option 1
Use ISNULL()
Description
Replaces NULL with the specified replacement value.
SELECT MAX(ISNULL(iCategoryOrder, 0))+1
FROM [IVRFlowManager].[dbo].[tblCategory]
WHERE iCategoryLevel = 1
Option 2
Use COALESCE()
SELECT MAX(COALESCE(iCategoryOrder, 0))+1
FROM [IVRFlowManager].[dbo].[tblCategory]
WHERE iCategoryLevel = 1
Description
Returns the first nonnull expression among its arguments.
Use ISNULL operator like:
ISNULL(your_field, 1)
Try following:
Select ISNULL(Max(iCategoryOrder), 0) + 1
from [IVRFlowManager].[dbo].[tblCategory]
where iCategoryLevel = 1