PostgreSQL find blank rows - sql

I want to search for non null values from the 'currentsheet' which works fine but some fields are actually blank rather than null. How can I find blank fields using postgreSQL as the below has not worked and still displays blank values under the 'currentsheet' field.
SELECT *
FROM PUBLIC._http_requests
WHERE (_http_requests.currentsheet IS NOT NULL OR _http_requests.currentsheet <> '')
AND _http_requests.session_id IS NOT NULL
AND _http_requests.http_referer IS NOT NULL

You need to use AND to check _http_requests.currentsheet. If it was NULL, then it would always be true for the <> '' check and vice versa.
As a way simpler example, you can use select statements without a table to help debug this sort of thing (from psql or whatever SQL query tool you like):
select ('' is not null or '' <> '') as empty_result,
(null is not null or null <> '') as null_result;
empty_result | null_result
--------------+-------------
t |
If the string is '', you get true. If the string is null, you get null (this is because comparisons with null are SQL oddities -- select null = null; results in null). Let's see what happens when we replace or with and:
select ('' is not null and '' <> '') as empty_result,
(null is not null and null <> '') as null_result;
empty_result | null_result
--------------+-------------
f | f
Neat! With X is not null and X <> '', we get false when X is either '' or null.
So the way to phrase the select statement to do what you actually want is:
SELECT *
FROM PUBLIC._http_requests
WHERE _http_requests.currentsheet IS NOT NULL
AND _http_requests.currentsheet <> ''
AND _http_requests.session_id IS NOT NULL
AND _http_requests.http_referer IS NOT NULL;

I think you just need AND _http_requests.currentsheet <> ''. The AND is important there so that we exclude both.

Related

SQL CASE that matches value or uses LIKE '%'

I'm trying to do a wildcard retrieve if a defined variable is blank. But I can't figure out the correct syntax.
SELECT ...
FROM ...
WHERE customers = CASE
WHEN ISNULL(#customerID, '') = '' THEN LIKE '%'
ELSE #customerID
END
It is as simple as this:
WHERE customers = NULLIF(#customerID, '') OR NULLIF(#customerID, '') IS NULL
The expression NULLIF(x, '') will convert '' to null. Then we take advantage of how null comparison works:
If #customerID is not null then first condition will be true only for exact match
If #customerID is null then first condition will be false (nothing equals null) but second condition will be true for all rows
DB<>Fiddle
You can just simply put it like this
SELECT...
FROM...
WHERE customers = ISNULL(#customerID,'') OR (ISNULL(#customerID,'') = '')
This should work because if the first condition was met, it will select the specific customer id. Otherwise, if the customer id is null,it will select all data because of this condition below:
(ISNULL(#customerID,'') = ''
If NULL, '' = '' will be true.

Create a custom column based on the values of three other columns

In this project I'm currently working on (I'm building a bridge between a desktop app and a new e-shop) there is a products table that has some spare columns defined that can be used for whatever reason the end user might need some custom data to be stored into.
So, the user needed to set a true/false flag to determine whether the products would appear in three different sliders... Unfortunately, the person who implemented this, didn't even use the same type of spare columns... So,
Slider1's flag is stored in a varchar(50) column
Slider2's flag is stored in a float column
Slider3's flag is stored in a float column
Additionally I ran a SELECT DISTINCT <column> for each one of them to get an idea of the actual data stored in each column and got the following results:
The varchar column has the following data stored in it:
FLDSTRING1
NULL
''
0
1
194276400456
The float column has the following data stored:
FLDFLOAT5
NULL
0
1
And the other float column has this:
FLDFLOAT6
NULL
1
Also, I ran the following query to find the different combinations of the data stored for each column:
SELECT FLDSTRING1, FLDFLOAT5, FLDFLOAT6
FROM MATERIAL
GROUP BY FLDSTRING1, FLDFLOAT5, FLDFLOAT6
and got the following combinations...
FLDSTRING1
FLDFLOAT5
FLDFLOAT6
NULL
NULL
NULL
NULL
NULL
1
NULL
0
NULL
NULL
1
NULL
NULL
1
1
''
NULL
NULL
''
NULL
1
0
NULL
NULL
0
0
NULL
1
NULL
NULL
1
NULL
1
1
0
NULL
1
1
NULL
1
1
1
194276400456
0
NULL
What I need after all this introduction...
I want a concatenated string of three comma-separated values like this
NEWPROD for when FLDSTRING1 would evaluate to true - anything not NULL, 0, or ''
CUSTOM1 for when FLDFLOAT5 would evaluate to true - basically the value 1
CUSTOM2 for when FLDFLOAT6 would evaluate to true - again value 1
After some trial and error I managed to bring this to a point that it kind of works, in the sense that it brings the correct values, not comma-separated though...
SELECT
FLDSTRING1, FLDFLOAT5, FLDFLOAT6,
CONCAT(CASE WHEN ISNULL(FLDSTRING1, '') = '' THEN '' ELSE 'NEWPROD' END,
CASE WHEN ISNULL(FLDFLOAT5, '') = '' THEN '' ELSE 'CUSTOM1' END,
CASE WHEN ISNULL(FLDFLOAT6, '') = '' THEN '' ELSE 'CUSTOM2' END) AS TAGS
FROM
MATERIAL
GROUP BY
FLDSTRING1, FLDFLOAT5, FLDFLOAT6;
FLDSTRING1
FLDFLOAT5
FLDFLOAT6
TAGS
NULL
NULL
NULL
NULL
NULL
1
CUSTOM2
NULL
0
NULL
NULL
1
NULL
CUSTOM1
NULL
1
1
CUSTOM1CUSTOM2
''
NULL
NULL
''
NULL
1
CUSTOM2
0
NULL
NULL
NEWPROD
0
0
NULL
NEWPROD
1
NULL
NULL
NEWPROD
1
NULL
1
NEWPRODCUSTOM2
1
0
NULL
NEWPROD
1
1
NULL
NEWPRODCUSTOM1
1
1
1
NEWPRODCUSTOM1CUSTOM2
194276400456
0
NULL
NEWPROD
Problem #1 is I don't quite understand how this works... I mean, value 0 isn't '', but still for the combination of NULL 0 NULL I get an empty value, which is what I wanted... But how does it do that?
And also, can someone update my final query to comma-separate the calculated TAGS column? Problem #2 is that I don't want it to contain just two commas, like ,,, when the combination wouldn't justify any of the three values to appear... It should work like PHP's implode() works...
To help you help me with this, I'm including a fiddle with the setup of the scenario I describe here... Thanks in advance!
Since you are using SQL Server 2014, instead of CONCAT_WS you may try STUFF as shown below. By prepending the delimiter , before all strings ,the STUFF will remove the first comma found.
SELECT
FLDSTRING1,
FLDFLOAT5,
FLDFLOAT6,
STUFF(
CONCAT(
CASE WHEN FLDSTRING1 IS NULL OR FLDSTRING1 IN ('0','') THEN '' THEN '' ELSE ',NEWPROD' END,
CASE WHEN FLDFLOAT5 IS NULL THEN '' ELSE ',CUSTOM1' END,
CASE WHEN FLDFLOAT6 IS NULL THEN '' ELSE ',CUSTOM2' END
),
1,1,''
) AS TAGS
FROM #MATERIAL
GROUP BY FLDSTRING1, FLDFLOAT5, FLDFLOAT6;
View working demo db fiddle
Let me know if this works for you.
Use the CONCAT_WS() function to concat values into a comma (or other separator) separated list, which ignores nulls.
To use CONCAT_WS(), you want to pass it a true NULL if the value is "blank" (by your definition), otherwise your custom label:
SELECT DISTINCT
FLDSTRING1,
FLDFLOAT5,
FLDFLOAT6,
CONCAT_WS(',',
CASE WHEN FLDSTRING1 IS NULL OR FLDSTRING1 = '' OR FLDSTRING1 = '0' THEN NULL ELSE 'NEWPROD' END,
CASE WHEN FLDFLOAT5 IS NULL OR FLDFLOAT5 = 0 THEN NULL ELSE 'CUSTOM1' END,
CASE WHEN FLDFLOAT6 IS NULL OR FLDFLOAT6 = 0 THEN NULL ELSE 'CUSTOM2' END) AS TAGS
FROM MATERIAL
Replaced GROUP BY with DISTINCT because it's simpler and (here) achieves the same thing.
If CONCAT_WS is not available:
SELECT DISTINCT
FLDSTRING1,
FLDFLOAT5,
FLDFLOAT6,
REPLACE(REPLACE(REPLACE(CONCAT(
CASE WHEN FLDSTRING1 IS NULL OR FLDSTRING1 = '' OR FLDSTRING1 = '0' THEN 'X' ELSE 'NEWPROD' END,
',',
CASE WHEN FLDFLOAT5 IS NULL OR FLDFLOAT5 = 0 THEN 'X' ELSE 'CUSTOM1' END,
',',
CASE WHEN FLDFLOAT6 IS NULL OR FLDFLOAT6 = 0 THEN 'X' ELSE 'CUSTOM2' END
), ',X', ''), 'X,', ''), 'X', '') AS TAGS
FROM MATERIAL
See dbfiddle.

How to select all items where LEN > 0

I'm new to Google BigQuery, and I'm finding that the SQL really quite different from normal SQL. I'm trying to select all items from a table that has data in a field named 'ticket_fields'. I son't want nulls. I tried the following:
WHERE COLUMN <> ''
WHERE LEN(COLUMN) > 0
WHERE NULLIF(LTRIM(RTRIM(COLUMN)), '') IS NOT NULL
None of that worked. How can I select all records where there are non-nulls from one specific field?
In some systems, the empty string and NULL are treated equivalently (Oracle, for example). In BigQuery, these are distinct values, so:
-- Returns TRUE
SELECT CAST(NULL AS STRING) IS NULL
-- Returns NULL
SELECT LENGTH(CAST(NULL AS STRING)) > 0
-- Returns TRUE
SELECT CAST(NULL AS STRING) IS NULL
-- Returns FALSE
SELECT '' IS NULL
-- Returns FALSE
SELECT LENGTH('') > 0
-- Returns TRUE
SELECT '' IS NOT NULL
If you want to filter rows where the column is NULL, then use IS NOT NULL:
SELECT * FROM dataset.table WHERE column IS NOT NULL
If you want to filter rows where the column is empty or NULL, you can just check that the length is positive:
SELECT * FROM dataset.table WHERE LENGTH(column) > 0
This is because LENGTH(column) returns NULL if column is null, so the WHERE clause excludes the row.

Does SQL Server take NULL value as an empty string?

I recently read a SQL code snippet which confuses me.
declare #test nvarchar(100) = NULL
select
case
when #test <> '' then 1
else 0
end
I was quite confident that the result will be 1, since I think NULL is not equivalent to an empty string. However, the actual output is 0.
(I'm using MS SQL Server 2012 on Windows 7 64-bit)
As far as I understand, '' is an empty string which indicates the value contains 0 character, and Null means the data is in absence. But now I'm not sure about this. Can anyone help me to sort it out? Is this some exemption case?
When you use NULL for your comparison, it always will return NULL/unknown so, in fact is not true, so is false.
To analyze a NULL field you must use IS NULL
select
case
when #test IS NULL then ....
when #test <> '' then ....
else ....
end
or you can re-write your query as follow:
select
case
when #test IS NULL or #test = '' then ...
when #test <> '' then ....
end
Null doesn't equal ''.
Null is the absent of a value.
Null also doesn't equal Null so SELECT 1 where NULL = NULL will also return nothing.
Use this instead.
declare #test nvarchar(100) = NULL
select case when #test IS NULL then 1
else 0
end
Use something like this:
declare #test nvarchar(100) = NULL
select case when #test <> '' OR #test IS NULL then 1
else 0
end
NULL is not the same as ''. Just like NULL is not the same as 0. NULL is a special value used to indicate that no value of the datatype is being stored.
If you want to COALESCE the NULL to a concrete value, you can use the ISNULL or the COALESCE functions in SQL Server.
DECLARE #test NVARCHAR(100) = NULL
SELECT
CASE
WHEN ISNULL(#test, N'') <> N'' THEN
1
ELSE
0
END

Using Case Statement in SQL with parameter/variable to check for Null values

I am trying to write a SQL Select statement to return records based on a user input through a front end.
I want to write the Select statement like this:
SELECT somefields
FROM sometable
WHERE CASE variable
WHEN 'blank' THEN field IS NULL
ELSE field = field
END
Basically I either want to filter a column to find NULL values or ignore the filter and return all values depending on the value of the variable. I know that the results of the CASE statement is not executable but how can I do this?
When variable is 'blank', the following query will give you rows where field is NULL. When variable is anything else, it will give you all rows:
SELECT somefields
FROM sometable
WHERE
(variable = 'blank' AND field IS NULL)
OR (variable <> 'blank')
You can use NULLIF() (link is for SQL Server, but NULLIF() should be standard):
SELECT somefields
FROM sometable
WHERE field = NULLIF(variable, 'blank')
The following snippet should behave as follows:
when #variable is null, return all rows
when #variable = 'blank', return all rows where field is null or field = 'blank'
otherwise, return rows where #variable equals field
Code snippet:
WHERE 1 = CASE
WHEN #variable is null then 1
WHEN #variable = 'blank' and field is null then 1
WHEN #variable = field then 1
END
SELECT somefields
FROM sometable
WHERE ((variable IS NULL OR variable = 0) OR (variable = field))
WHERE Criteria is apply when variable have value
For Example:
DECLARE #CityName VARCHAR(50)
SET #CityName = NULL
SELECT CityName
FROM City
WHERE ((#CityName IS NULL ) OR (#CityName = CityName ))
When City is null then tables return all rows
I think I get what you're after. Something like this maybe?
SELECT field1,
field2,
CASE variable
WHEN 'blank' THEN NULL
ELSE field3
END as field3
FROM sometable
Think I understand what you mean....for example....
SELECT
House, Postcode
from
SomeTable
where
(House=isnull(#House,House) or (House is null and #House is null))
and
(Postcode=isnull(#Postcode,Postcode) or (Postcode is null and #Postcode is null))
First bit of the conditional where is to use the variable, when present (the isnull bit is to ignore the variable if it's null)
Second bit of the conditional where is in case your evaluative field is null also as effectively fields don't = null they are 'is null'.
Confused? Good. Works on what I'm doing though!
Here is my solution based on #Andomar answer above aimed at anyone testing an input varchar value, you need to test the parameter in the right order as per the example below:
FIELD1 = CASE
WHEN #inputParameter = '' THEN FIELD1
WHEN #inputParameter <> FIELD1 THEN NULL -- if input is some text but does not match
WHEN #inputParameter IS NULL THEN FIELD1
WHEN #inputParameter != '' AND FIELD1 = #inputParameter THEN FIELD1
END
Hope this helps someone.