I know the decode function, however, no idea what the stuff in translate means:
decode(translate(cookie_id,'0123456789','') = '','t',cookie_id,null) as cookie_id
decode((translate(pickup_date,'0123456789','') = '--' and length(pickup_date) = 10),'t',pickup_date,null) as pickup_date,
It is very simple. This expression:
translate(cookie_id, '0123456789', '')
returns NULL. Oracle treats the empty string as NULL and even explicitly warns:
You cannot use an empty string for to_string to remove all characters
in from_string from the return value. Oracle Database interprets the
empty string as null, and if this function has a null argument, then
it returns null.
So, the first example will alway yield NULL for the cookie_id.
Quite possibly, the author intended something like:
decode(translate(cookie_id, 'a0123456789', 'a'), '', cookie_id, null) as cookie_id
This would check to see if cookie_id only contains digits and then return the id. Personally, I find a regular expression easier to follow:
(case when regexp_like(cookie_id, '^[0-9]+$') then cookie_id end)
And this doesn't have to do funky things to deal with empty strings.
Related
I'm using SQL Server 2019.
select convert(bigint, ' ')
returns 0
select convert(numeric, ' ')
returns:
Error converting data type varchar to numeric.
Can anyone tell me why the behaviour between these two datatypes is different? I would have expected, that the second example returns 0 or 0.00.
From CAST and CONVERT:
"SQL Server also returns an error when an empty string (" ") is converted to numeric or decimal."
It's the documented behavior, that's why.
It's just the way it is implemented. Casting an empty string or a string with only spaces to an INT data type will result in 0. Some people regard this as logical behavior, others don't.
For a DECIMAL type, the same cast fails.
However, this is only true for spaces! Trying to cast other whitespace characters like tabs, carriage return or linefeed characters to an INT type will also fail.
If you want failed casts to return NULL instead of producing an error, you can use TRY_CONVERT instead:
select try_convert(numeric, ' ')
If you want both to behave in the same way (whether that would mean both returning NULL or both returning 0 or 0.0 respectively) you need to somehow work your way around it.
To have the cast to INT return NULL, you could e.g. combine TRY_CONVERT with NULLIF:
select try_convert(bigint, nullif(' ', ''))
This will also produce NULL for strings containing only (any number of) spaces.
If on the other hand, you prefer to see 0.0 results for empty strings or strings containing only spaces when casting to NUMERIC, you can filter these out explicitly:
select case when val like ' ' or val = '' then 0.0
else try_convert(numeric(5,1), val)
end as val
from strings
This db<>fiddle shows what results the different queries yield for various whitespace and non-whitespace string input.
I have table, in which a column has empty fields.when i try to run simple query like below it is not
returning the data.
select * from table1 where "colunm1" = ''
But below two queries returns data
1,
select * from table1
where coalesce("colunm1", '') = ''
2,
select * from table1
where "colunm1" is null
Can someone tell me the reason?
TIA
You have describe the behavior of a column that is NULL. NULL is not the same as an empty string.
It fails any equality comparison. However, you can use is null or (less preferentially) coalesce().
The only database that treats an empty string like a NULL value, is Oracle.
Logically, for a DBMS, that would be wrong.
Relational DBMSs are all about :
set theory
Boolean algebra
A NULL value is a value that we don't know. A string literal of '' is a string whose value we know. It is a string with no characters, with a length of 0. We don't know of a NULL as a string how long it is, how many and, if any, which, characters it contains.
So it is only logical that:
the comparison '' = '' will evaluate to TRUE
the comparison NULL = NULL will evaluate to FALSE , as any comparison with a NULL value will evaluate to FALSE.
And functions like COALESCE() or NVL(), IFNULL(), ISNULL() will return the first parameter if it does not contain a NULL value. That is consistent.
Except in Oracle
I know that nvl function works only for 'null', not empty string. I just wanted to know if there was a function that helps me replace blank/null values with another one.
I used instead:
CASE WHEN expression_1 is null or expression_1 = '' then expression_2 else expression_1
expression_1 column has empty cells, not null cells.
NVL() is a function most typically associated with Oracle. The equivalent standard SQL function is COALESCE().
By default, Oracle treats NULL strings and empty strings ('') as the same. So, '' IS NULL evaluates to "true" (and '' = '' rather confusingly evaluates to NULL, which is false in a WHERE clause).
So, you can use NVL() or COALESCE() on an empty string. These are the same:
NVL(NULL, 'A')
NVL('', 'A')
COALESCE(NULL, 'A')
COALESCE('', 'A')
Here is a db<>fiddle.
Incase the column contains blank spaces, you can use TRIM() besides NVL() so as you can gracefully remove blank spaces before you apply NVL().
NVL(TRIM(' '),'X') --> returns X
db<>fiddle -
https://dbfiddle.uk/?rdbms=oracle_18&fiddle=08435972cbfb799329e6609349c70a04
I believe you are looking Coalesce ():
coalesce(expression2, expression1)
Brings the first non-null value from expression2 else expression 1. If all expressions evaluate to null, then the COALESCE function will return null.
NVL function lets you substitute a value when a null value is encountered:
nvl(expression2, expression1)
Sql statement would replace expression 2 with with expression 1 if it finds null value in there.
Difference: NVL always evaluates both arguments, while COALESCE usually stops evaluation whenever it finds the first non-NULL
One awesome explanation given by #Quassnoi here
I am new to SQL and I am looking at established queries that we have, can anyone explain what this statement means in the WHERE clause
(isnull(cast(field_name as CHAR), '') = '').
Breaking down each statement:
CAST(field_name AS CHAR) converts the field_name column to a CHAR
This value is then passed as the first argument to the ISNULL() with the second being an empty string ''.
ISNULL(CAST(field_name AS CHAR), '')
This takes the result of the cast, and if it's a NULL value, returns '' instead.
Finally, it checks if that result is equal to ''.
Essentially, it's checking for NULL or empty string values in one fell swoop.
juergen d is correct. I'd add that it tries to cast the field to alphanumeric before it checks if it is null or blank. If the field's data type has no defined conversion to type CHAR, the query will error out. Have a look at this regarding conversions/casts.
T-SQL Data Types and Conversion Info
I've a scenrio which process many data in Oracle database. In some cases, the variable Sec_email will contain many values and in some cases Sec_email will contain null or ' '.
so can please any one tell me how to write a query for this?
I tried with
(C.SECONDARY_EMAIL IN ('?,?') OR '' = '' )
where C is the Client table.
When I use this i get the count as 0.
You can perform a not null check before the IN comparison like
Sec_email is not null and C.SECONDARY_EMAIL IN (...
One obvious problem is that Oracle (by default) treats empty strings as NULL. So: '' = '' is the same as NULL = NULL, which is never true.
Arrgh.
In any case, you are probably constructing the query, so use is null instead:
(C.SECONDARY_EMAIL IN ('?,?') OR '' IS NULL
I think the real problem, though, is the first comparison. The IN list has one element with a constant, not two (but perhaps that is your intention). If you want to put a variable number of values for comparison, one method uses regular expressions. For instance:
C.SECONDARY_EMAIL REGEXP_LIKE '^val1|val2|val3$' or '' IS NULL
If you would like to get a list of values when some of them is null you should use:
("some other conditions" OR C.SECONDARY_EMAIL IS NULL)
The question is if it is not null and not ' ' value what you are expecting, if it should be some king of
pattern you should use regular expression:
regexp_like(C.SECONDARY_EMAIL, '^(.+?[,]+?)+$')
Also, if you have a few conditions in where clause use should use brackets to group you conditions null check and another one.
All conditions i this case will be divided by OR.
(C.SECONDARY_EMAIL IS NULL OR regexp_like(C.SECONDARY_EMAIL, '^(.+?[,]+?)+$'))
or
(C.SECONDARY_EMAIL IS NULL OR regexp_like(C.SECONDARY_EMAIL, '^(.+?[,]+?)+$')
OR C.SECONDARY_EMAIL = ' ')