How to check for null/empty/whitespace values with a single test in PostgreSQL - sql

I would like to check an input (parameter) of a stored procedure if it has many spaces and empty, not just 1 space like so:
' ' '.
I tried :
IF column IS NULL or TRIM(column IS NULL) THEN RAICE NOTICE 'input is empty spaces';
END IF;
But the spaces input still passes through.

You could use COALESCE() here:
IF
COALESCE(column, '') ~ '^\s*$'
THEN
RAICE NOTICE 'input is empty spaces';
END IF;
The pattern ^\s*$ will match zero or more whitespace characters. The outer call to COALESCE() replaces NULL column values with empty string, which will match the regex pattern.

TRIM(column IS NULL) should result in an error, because column is null yields a boolean and trim() doesn't work on boolean values.
Note that trim() will return an empty string if the input is only blank, not null (an empty string '' is something different than null)
You can shorten the whole test to a single expression:
if nullif(trim(column), '') is null then ...

Related

LTRIM(RTRIM(COLUMN)) in sql server

I need to create a query to collect all the rows with column A not null and not blank.
When I used this:
AND A.EXP_GRAD_TERM is not null AND A.EXP_GRAD_TERM <> ' '
I got 1169 records which all have some values for the field.
But when I used this:
AND LTRIM(RTRIM(A.EXP_GRAD_TERM)) is not null
I got 1932 records. They have the rows with values and rows with blanks. I even convert the column, the column only has blanks or the values, not other weird invisible character.
I do not know why. I thought they should have worked the same way.
Any thoughts?
This expression:
LTRIM(RTRIM(A.EXP_GRAD_TERM)) is not null
is exactly equivalent to:
A.EXP_GRAD_TERM is not null
An empty string is different from NULL, so removing spaces -- even from a string that only has spaces -- has no effect on the NULL comparison.
If you are confused, perhaps you have experience with Oracle where empty strings and NULL are the same thing. That is a peculiar property of Oracle.
LTRIM() function:
Returns a character expression after it removes leading blanks.
RTRIM() function:
Returns a character string after truncating all trailing spaces.
They are different from IsNull or empty string condition (CASE WHEN IS NULL OR <>'')
References
LTRIM (Transact-SQL)
RTRIM (Transact-SQL)
Combine COALESCE() with LTRIM() and RTRIM():
AND RTRIM(LTRIM(COALESCE(A.EXP_GRAD_TERM, ''))) <> ''
COALESCE(A.EXP_GRAD_TERM, '') will return '' for every null value.
see if you want to get the values that are not null and not empty you can use isnull (exp, replace_value)
AND RTRIM(LTRIM(ISNULL(A.EXP_GRAD_TERM, ''))) <> ''
isnull isnull replaces a null value for which you want to replace it. you could also use COALESCE which "returns the first non-null expression among your arguments"
One apparent advantage that COALESCE has over ISNULL is that it supports more than two inputs, whereas ISNULL supports only two. Another advantage of COALESCE is that it's a standard function (namely, defined by the ISO/ANSI SQL standards), whereas ISNULL is T-SQL–specific. These differences between the two functions are fairly straightforward.
if you want more information about the differences between ISNULL and COALESCE you can see it on this link

Case when statement SQL

I am facing some difficulties for a Datawarehouse transformation task, I have some source columns which are coming in varchar format, data contained: Blanks, -, decimal numbers such as (1234.44).
Those columns in target are declared as number.
I am trying to treat that data with this code but I keep receiving invalid number error:
CASE WHEN
LENGTH(TRIM(TRANSLATE(column78input, '-', ' '))) is null then null
WHEN column78input IS NULL THEN 0
else to_number(column78input)
END
In first when statement I am trying to check if there is - in source, it returns null when found, and if you find it then place it as null (replacing dashes with nulls in essence)
In second when statements I am trying to treat those blanks, I thought that they might cause the error
And finally in else statement I want to parse it from varchar to number to load in target table.
If someone has some kind of suggestion, please help!
Thanks
Try with
CASE
WHEN INSTR(column78input, '-') > 0 OR column78input IS NULL THEN 0
ELSE TO_NUMBER(REPLACE(column78input, ' '))
END
INSTR returns the first position of a character in a string. So if there is no dash, it would return 0. A value greater than 0 means there is at least one dash in the string.
Here are a few mistakes in your code :
A case when statement will exit when a condition is met. So you can remove the dash in the first condition and expect it to continue to process your string in the next condition. In your code if a string had a dash, the result would be null.
LENGTH function returns the number of characters in a string. It will return a null value only if the string is null. So it's easier to directly write column78input IS NULL
You current first condition is basically this : "After replacing the dash by a space and removing all the leading/trailing spaces, if the string is null then". Because you are replacing the dashes in the string, you can't check if there is an occurrence or not.

Hidden character in SQL column value in oracle

I have a value in column of a table and somehow there is some hidden character at the end of the string. I cannot see it or remove it. The string is placed below. The total characters that I can see in this string is 25, but I when check the length of the string it is showing as 26. I tried TRIM function but thinking it could be a space, but it is not. How to remove this kind of characters from string in oracle query. Actually, I am using regexp_replace to replace some part of this string, but because of this issue the regex not able to match the last number in the string to replace everything before it.
28/110/41492/171486/98122
Here is my regex function
regexp_replace(trim(ATTRIBUTE_VALUE), '(^|.*?/)' || '98122' || '(/|$)', 'replaced' || '\2', 1, 1)
Do this in two steps:
remove all non-printable characters
apply your replace pattern
This is:
regexp_replace
(
regexp_replace(attribute_value, '[^[:print:]]'), -- printable string
'(^|.*?/)' || '98122' || '(/|$)', -- search pattern
'replaced' || '\2', -- replace pattern
1, -- position
1 -- occurrence
)

Like operator and Trailing spaces in SQL Server

This one matches column_name like 'CharEndsHere%'
and
This one doesn't column_name like 'CharEndsHere'
I know that like operator will consider even the trailing spaces, so I just copied the exact column value (with trailing spaces) and pasted it.
Something like column_name like 'CharEndsHere ' yet it doesn't match -- why?.
I haven't used '=' operator since the columns type is ntext
Is there something I am missing here or shouldn't I use like operator in this way?
Edited : column_name like 'CharEndsHere__' (__ denoted the spaces) 'CharEndsHere ' is the exact value in that cell, using like in this way valid or no?
Edit :
This is the code I tried,
SELECT *
FROM [DBName].[dbo].[TableName]
WHERE [DBName].[dbo].[TableName].Address1 LIKE rtrim('4379 Susquehanna Trail S ')
I have also tried without using rtrim, yet the same result
Edit: According to Blindy's answer,
If a comparison in a query is to return all rows with the string LIKE 'abc' (abc
without a space), all rows that start with abc and have zero or more trailing
blanks are returned.
But in my case, I have queried, Like 'abc' and there is a cell containing 'abc '(with trailing spaces) which is not returned. That's my actual problem
This is a case of reading the documentation, it's very explicitly stated here: http://msdn.microsoft.com/en-us/library/ms179859.aspx
When you perform string comparisons by using LIKE, all characters in the pattern string are significant. This includes leading or trailing spaces. If a comparison in a query is to return all rows with a string LIKE 'abc ' (abc followed by a single space), a row in which the value of that column is abc (abc without a space) is not returned. However, trailing blanks, in the expression to which the pattern is matched, are ignored. If a comparison in a query is to return all rows with the string LIKE 'abc' (abc without a space), all rows that start with abc and have zero or more trailing blanks are returned.
Edit: According to your comments, you seem to be looking for a way to use like while ignoring trailing spaces. Use something like this: field like rtrim('abc '). It will still use indexes because rtrim() is a scalar operand and it's evaluated before the lookup phase.

Oracle: remove first 4 characters from a string

So I want to remove the first 4 characters from a string in oracle. Those characters can be different every time.
In my case I need to take away the first 4 characters of an IBAN and put them at the end of the string. I got the part of putting them to the end of the string but I can't get the first 4 characters to be removed. Every solution I find on the internet removes specified characters, not characters from a certain position in the string (1 to 4).
I used the code below to get the first 4 characters to the end of the string and wanted to try something similar for removing them at the front but without success.
SELECT SUBSTR(iban_nummer, 1, 4) INTO iban_substring FROM dual;
iban_nummer := iban_nummer || iban_substring;
See the docs:
substring_length ...
When you do not specify a value for this argument, then the function returns all characters to the end of string. When you specify
a value that is less than 1, the function returns NA.
So iban_nummer := substr(iban_nummer, 5) || substr(iban_nummer, 1,4) should work. The first part selects all characters beginning from the 5th, the second character numbers 1..4.
update table_name set col_name=substr(col_name,5);
try regexp, like:
SELECT regexp_replace(t.iban_nummer,'(.{4})(.*)','\2\1') FROM t;
Alternative way using regexp :
SELECT regexp_replace(t.iban_nummer,'^.{4}(.*)','\2\1') FROM dual;