condition where a <> ' ' and a <> '' not working - sql

I wrote a SQL to query table mat from an oracle db where column A is not null. Column A is varchar and its default value is ' '. I wrote the sql below:
select * from mat where matnr='test' and A <>'' and A <> ' '
But it return an empty data set.
Then I ran:
select * from mat where matnr='test' and A <> ' '
This query worked. So what is the reason? Thx.

In Oracle, '' means NULL. Any direct comparison to NULL returns NULL instead of TRUE or FALSE, so you cannot say A <> '' - you must say A IS NOT NULL.
Another possibility would be to use the NVL function, replacing NULL with ' ', so that you could say
select * from mat where matnr='test' and NVL(A, ' ') <> ' '

Related

How to do not null check on LEFT function on the select query

I have a query that returns some demographics like firstName, lastName, MiddleName and i need to use LEFT function on each to filter the First Letter of each column like LEFt(firstName, 1).This is working fine when each column is not a null value. when it is null value
select otherColumns, LEFT(sub.LastName, 1) + ',' + LEFT(sub.FirstName, 1) + ' ' + LEFT(sub.MiddleName, 1) as patientInitials from <table> <inner joins> <some where conditions>;
But when one of demographics like middleName is null and other firstName, lastName are not null , patientInitials are evaulating to NULL, not sure why?
I resolved my issue by adding COALESCE
LEFT(sub.LastName, 1) + ',' + LEFT(sub.FirstName, 1) + ' ' + COALESCE((LEFT(sub.MiddleName, 1)),'') as patientInitials
But is there any other good way to check for notNull on the LEFT function ??
Help Appreciated!
But is there any other good way to check for notNull on the LEFT function ??
CONCAT function ignores NULLs:
SELECT CONCAT(LEFT(sub.LastName, 1), ',' ,
LEFT(sub.FirstName, 1),
' ' + LEFT(sub.MiddleName, 1)) patientInitials
FROM tab;
' ' + LEFT(sub.MiddleName, 1)) using ' ' will remove leading space in case if Middle Name is NULL.
The CONCAT_WS function also has a similar function:CONCAT_WS (Transact-SQL)

Split FullName Where has null values (db2 sql)

I have full name that contains blank or null value and cause an error when I split into first name,lastname.
here is the error:
ERROR [22011] [IBM][DB2/AIX64] The statement was not executed because a numeric argument of a scalar function is out of range.
here is my original code:
UPPER(right(AGENT_NM, (char_length (AGENT_NM) - position( ' ', AGENT_NM))))|| ', ' || UPPER(left(AGENT_NM, position( ' ', AGENT_NM) - 1)) AS AGENT_NAME,
here is what I have tried:
1-
CASE when REGEXP_COUNT(AGENT_NM,',')> 0 then left (AGENT_NM, position( ' ', AGENT_NM) - 1) END AS FNAME,
2-
CASE when(AGENT_NM= ' ') then Null Else left (AGENT_NM, position( '
> ', AGENT_NM) - 1) END AS FNAME,
However it returns blank.
Concatenate a space onto the end of your name first:
SELECT
TRIM(UPPER(RIGHT(AGENT_NMs, char_length (AGENT_NMs) - position( ' ', AGENT_NMs))))||
', ' ||
TRIM(UPPER(left(AGENT_NMs, position( ' ', AGENT_NMs) - 1)))
AS AGENT_NAME
FROM
(
SELECT
a.*,
COALESCE(agent_nm, '')||' ' as agent_nms
FROM
yourtable a
) x
Here we use coalesce to ensure the name is not null, and we then add a space on as something for the position function to find
The top bit is just your code tweaked to refer to the new name agent_nms and add an extra trim command for removing any extraneous spaces. It did look like it had one too many brackets in by the way
name=str(input("your name here"))
split_name=name.split(" ")

How to use subquery inside DECODE in Oracle

I am trying to use a sub-query inside and Oracle Decode statement as shown below
RPAD(NVL(DECODE(TRIM(ST.StudentCode),'AB','CA','TM','CH',(Select InternalNumber from Address where State = SA.STATECODE) <=2,'PAS', ST.StudentCode), ' '), 3, ' ')
when I am running this part with my original query I am getting error as "Missing right paranthesis" in the same line. What is being wrong here?
Just use a single case expression:
RPAD( (CASE WHEN TRIM(ST.StudentCode) = 'AB' THEN 'CA',
WHEN TRIM(ST.StudentCode) = 'TM' THEN 'CH',
WHEN (Select a.InternalNumber from Address a where a.State = SA.STATECODE) <= 2 THEN 'PAS'
ELSE COALESCE(ST.StudentCode, ' '),
), 3, ' ')
You can add case expression inside your sub query:
RPAD(NVL(DECODE(TRIM(ST.StudentCode),'AB','CA','TM','CH',CASE WHEN (Select InternalNumber from Address where State = SA.STATECODE) <=2 THEN 'PAS' ELSE ST.StudentCode END), ' '), 3, ' ')
Oracle Database searches for the first WHEN ... THEN pair for which expr is equal to comparison_expr and returns return_expr. If none of the WHEN ... THEN pairs meet this condition, and an ELSE clause exists, then Oracle returns else_expr. Otherwise, Oracle returns null.

How to check if field is NULL and blank?

I'm trying to remove the possibility of blank spaces by a value not existing in the database when creating the view for my lookup. The issue I'm having is that my CASE statement isn't working quite right when I'm trying to check for a NULL or blank value. It seems to work for those that are null but the blank doesn't seem to have as much luck. In this case I am trying to check for null or blank of importantField
CREATE VIEW Lookup4 AS
SELECT TOP 140000 CONCAT(no,
CASE WHEN (importantField is null OR importantField = '')
THEN '' ELSE ' ' + importantField END,
fieldname + ' ', anotherField2) AS UNRELATEDFIELD, Code,
CASE NAME
WHEN '101,,,,,' THEN 'value1e'
WHEN '14,,,,,' THEN 'value3'
WHEN '16,,,,,' THEN 'value4'
END AS NAME
FROM dbo.Lookup
Is this what you are after
CREATE VIEW Lookup4 AS
SELECT TOP 140000 CONCAT(no,
CASE WHEN (ISNULL(importantField,'') = '')
THEN '' ELSE ' ' + importantField END,
fieldname + ' ', anotherField2) AS UNRELATEDFIELD, Code,
CASE NAME
WHEN '101,,,,,' THEN 'value1e'
WHEN '14,,,,,' THEN 'value3'
WHEN '16,,,,,' THEN 'value4'
END AS NAME
FROM dbo.Lookup
If you only want to check for null and not for empty strings then you can also use ifnull as you tried. But that is not suitable for empty strings too.
SELECT IF(field1 IS NULL or field1 = '', 'empty', field1) as field1
from tablename
Try to change:
importantField is null
with
IsNull(importantField)
CREATE VIEW Lookup4 AS
SELECT TOP 140000 CONCAT(no,ifnull(importantField,'')<>'',
fieldname + ' ', anotherField2) AS UNRELATEDFIELD, Code,
CASE NAME
WHEN '101,,,,,' THEN 'value1e'
WHEN '14,,,,,' THEN 'value3'
WHEN '16,,,,,' THEN 'value4'
END AS NAME
FROM dbo.Lookup
Minor changes according to your result
Method 1:
Select *
From dbo.Lookup
Where IsNull(importantField, '') = ''
Method 2:
Select *
From dbo.Lookup
Where (importantField is NULL or importantField = '')

Eliminating a space in concatenated values when null

My SELECT statement reads something like this:
SELECT JKLL.LKJJ, LKJF.ASLKD, TRIM (ADDR.UNNBR) || ' ' || TRIM(ADDR.PREDIR) || ' ' ||, TRIM(ADDR.STREET)....
Where UNNBR is the address number and PREDIR is the predirection (NSEW).
When concatenating into the same column, if predir is null, I get two spaces between UNNBR and STREET, obviously.
Can I use a case statement to eliminate this space when PREDIR is null? If so, what would that syntax look like?
I sometimes do something like this for these situations:
(TSQL syntax)
SELECT
CASE WHEN ADDR.UNNBRIS IS NOT NULL THEN ADDR.UNNBR + ' ' ELSE '' END +
CASE WHEN ADDR.PREDIR IS NOT NULL THEN ADDR.PREDIR + ' ' ELSE '' END +
...
This way you only get the space if the field isn't null.
I would use ISNULL
SELECT JKLL.LKJJ, LKJF.ASLKD, TRIM(ADDR.UNNBR) + ISNULL(' ' + TRIM(ADDR.PREDIR),'') ...
A NULL value concatenated to anything yields a NULL. So something like this will work:
WITH ex as
(SELECT 'Pants' as item, null as other, 'George' as name)
SELECT COALESCE(item + ' ', '')
+ COALESCE(other + ' ', '')
+ COALESCE(name + ' ', '')
FROM ex