I have a query like this:
SELECT DISTINCT
[F_Exhibitor_Name]
FROM
[V_ExhibitorLocation]
WHERE
F_ExhibitionCode ='10996'
AND
[F_Exhibitor_Name] IS NOT NULL
ORDER BY
F_Exhibitor_Name
My first line is blank, which causes an error in the code. My current result set looks like this:
In SQL Server, a null and an empty string ('') are not the same. If you which to exclude both, you should explicitly check for both:
SELECT DISTINCT [F_Exhibitor_Name]
FROM [V_ExhibitorLocation]
WHERE [F_ExhibitionCode] = '10996' AND
[F_Exhibitor_Name] IS NOT NULL AND
[F_Exhibitor_Name] <> ''
ORDER BY [F_Exhibitor_Name]
I can suggest a trick for mixing IS NOT NULL AND <> '' like this:
SELECT DISTINCT
F_Exhibitor_Name
FROM
V_ExhibitorLocation
WHERE
F_ExhibitionCode = '10996'
AND
F_Exhibitor_Name > '' --or ISNULL(F_Exhibitor_Name, '') <> ''
ORDER BY
F_Exhibitor_Name
Related
I have a table like named innerjoined:
This current table has fields that are empty. And I have a code that removes rows with a blank value.
`SELECT productcode, Brand, product, size FROM innerjoined WHERE
COALESCE(productcode, '') <> '' AND
COALESCE(Brand, '') <> '' AND
COALESCE(product, '') <> '' AND
COALESCE(size, '') <> ''`
So it ends up just like this:
My question is:
How do I update the current table without creating a new one based on this formula?
I have tried the following:
`update innerjoined(SELECT productcode, Brand, product, size FROM innerjoined WHERE
COALESCE(productcode, '') <> '' AND
COALESCE(Brand, '') <> '' AND
COALESCE(product, '') <> '' AND
COALESCE(size, '') <> '')`
First, I just do not understand why you are not using:
SELECT productcode, Brand, product, size
FROM innerjoined
WHERE productcode <> '' AND
Brand <> '' AND
product <> '' AND
size <> '';
It is simpler and does the same thing.
Second, you appear to want delete:
delete i from innerjoined i
where productcode = '' or productcode is null or
brand = '' or brand is null or
product = '' or product is null or
size = '' or size is null;
Here is a db<>fiddle illustrating that all of this code works, with both empty strings and NULLs.
I have a table that has 3 phone number fields (among other fields).
DayTimePhone
EveningPhone
MobilePhone
I need to pull up records where there is a number in at least one of them.
SELECT Field1
,Field2
,Field3
,DayTimePhone
,EveningPhone
,MobilePhone
FROM Contact
WHERE DayTimePhone IS NOT NULL
AND EveningPhone IS NOT NULL
AND MobilePhone IS NOT NULL
But in some results I get rows where all three fields are blank. They don't have a NULL, just blank. So my code works to an extent.
I tried:
SELECT Field1, Field2, Field3
,CASE isnull (DayTimePhone, '-') WHEN '' THEN '-' WHEN ' ' THEN'-'
END DayTimePhone
,CASE isnull (EveningPhone, '-') WHEN '' THEN '-' WHEN ' ' THEN'-'
END EveningPhone
,CASE isnull (MobilePhone, '-') WHEN '' THEN '-' WHEN ' ' THEN'-'
END MobilePhone
INTO #TempTable
--------------------------------
SELECT * FROM #TempTable
WHERE DayTimePhone IS NOT NULL
AND EveningPhone IS NOT NULL
AND MobilePhone IS NOT NULL
AND DayTimePhone != '-'
AND EveningPhone != '-'
AND MobilePhone != '-'
But this code only gives me results WHERE there is a NULL or - in the fields.
How do I get results where there is at least 1 number in any of the three fields?
Edit
Please see my comment to #LONG answer. That solution worked but now the results are also showing rows where the DayTimePhone reads 0. Please read my comment below.
Another option is to simply check the length of the string representing the number. If it is null or too short then simply exclude it from the result.
SELECT * FROM #TempTable
WHERE (
(ISNULL(LEN(RTRIM([DayTimePhone])), 0) > 8)
OR
(ISNULL(LEN(RTRIM([EveningPhone])), 0) > 8)
OR
(ISNULL(LEN(RTRIM([MobilePhone])), 0) > 8)
)
Try:
SELECT * FROM #TempTable
WHERE (DayTimePhone IS NOT NULL AND REPLACE(DayTimePhone,' ','') != '')
OR (EveningPhone IS NOT NULL AND REPLACE(EveningPhone ,' ','') != '')
OR (MobilePhone IS NOT NULL AND REPLACE(MobilePhone ,' ','') != '')
Having blank is not always a good table design, try to update them with NULL
UPDATE:
From you comments, could you please indicate which type 0 phone that is?
If you still see '0' in the result, that means that is definitely not a single 0.
You can just go with WHERE DayTimePhone != '0-0-0' or DayTimePhone != '0-0', but this is not a skeleton key. If you are checking with U.S phone numbers, you can go with Validate U.S. Phone number and using LIKE, '[0-9]', expression pattern like this to check. I will try to give you a general way to check a validate U.S phone query.
A pretty simple way is:
SELECT tt.*
FROM #TempTable tt
WHERE tt.DayTimePhone <> '-' OR
tt.EveningPhone <> '-' OR
tt.MobilePhone <> '-';
NULL values will not return true for the <> comparison.
You should first clean you table from nulls and blank values.
That was actually done already. What you need now is to use the OR command instead of AND and check it for the value you replaced ('-'):
SELECT * FROM #TempTable
WHERE (DayTimePhone IS NOT NULL != '-')
OR (EveningPhone IS NOT NULL != '-')
OR (MobilePhone IS NOT NULL != '-')
I hope it helps...
I have a table with three columns and I want to check Null or Empty data from the columns either its is Imp or Main or Comp.
I am using the following query:
SELECT Imp,Main,Comp
FROM Items_tbl
WHERE Contributor = 37
AND NULLIF(Imp, '') IS NULL
AND NULLIF(Main, '') IS NULL
AND NULLIF(Comp, '') IS NULL;
So if you want to COUNT the rows you could just do the following:
SELECT COUNT(*)
FROM Items_tbl
WHERE Contributor=37
AND NULLIF(Imp, '') IS NULL
AND NULLIF(Main, '') IS NULL
AND NULLIF(Comp, '') IS NULL;
Or if you only need one of those columns to be NULL:
SELECT COUNT(*)
FROM Items_tbl
WHERE Contributor=37
AND (NULLIF(Imp, '') IS NULL
OR NULLIF(Main, '') IS NULL
OR NULLIF(Comp, '') IS NULL
);
Based on your comment regarding needing a count...
SELECT Count(*)
FROM Items_tbl
WHERE Contributor=37 and ISNULL(Imp, '') = ''
and ISNULL(Main, '') = ''
and ISNULL(Comp, '') = '' ;
Well I am not sure if I have understood the question correctly, but if you want all the rows where the 3 columns is either NULL/empty, you could try this :
SELECT ID, Contributor,
Imp,
Main,
Comp
FROM Items_tbl
WHERE Contributor = 37
AND (Imp IS NULL OR Imp = '')
AND (Main IS NULL OR Main = '')
AND (Comp IS NULL OR Comp = '')
You can see this here ->SQL fiddle
Note that for this query either all the 3 columns in question should either be all NULL or all empty.
In case, you want all the rows where even one of the aforementioned columns is NULL/empty, you could use a `UNION' to get this as follows :
SELECT ID, Contributor,
Imp,
Main,
Comp
FROM Items_tbl
WHERE Contributor = 37
AND (Imp IS NULL OR Imp = '')
AND (Main IS NULL OR Main = '')
AND (Comp IS NULL OR Comp = '')
UNION
SELECT ID, Contributor,
Imp,
Main,
Comp
FROM Items_tbl
WHERE Contributor = 37
AND (Imp IS NULL)
OR (Main IS NULL)
OR (Comp IS NULL)
See this here -> SQL fiddle
Hope this helps!!!
Try this.
Edit:
SELECT Imp,Main,Comp
FROM Items_tbl
WHERE Contributor=37 AND COALESCE(NULLIF(ISNULL(Imp,''),''),NULLIF(ISNULL(Main,''),''),NULLIF(ISNULL(Comp,''),'')) is NULL
I want to have compatible SQL for both Oracle database and Microsoft SQL server.
I want a compatible SQL expression that will return true for not null and not empty strings.
If I use:
column <> ''
it will work on Microsoft SQL server but not on Oracle database (as '' is null for Oracle)
If I use:
len(column) > 0
it will work on Microsoft SQL server but not on Oracle database (since it uses length() )
NULLIF is available on both Oracle (doc) and SQL Server (doc). This expression should work:
NULLIF(column, '') IS NOT NULL
In both servers, if column is NULL, then the output of NULLIF will just pass the NULL value through. On SQL Server, '' = '', so the output of NULLIF will be NULL. On Oracle, '' is already NULL, so it gets passed through.
This is my test on SQL Server 2008 R2 Express:
WITH SampleData AS
(SELECT 1 AS col1, NULL AS col2
UNION ALL
SELECT 2, ''
UNION ALL
SELECT 3, 'hello')
SELECT *
FROM SampleData
WHERE NULLIF(col2, '') IS NOT NULL;
And this is my test case on Oracle 10g XE:
WITH SampleData AS
(SELECT 1 AS col1, NULL AS col2 FROM DUAL
UNION ALL
SELECT 2, '' FROM DUAL
UNION ALL
SELECT 3, 'hello' FROM DUAL)
SELECT *
FROM SampleData
WHERE NULLIF(col2, '') IS NOT NULL;
Both return 3 as expected.
How about
CASE WHEN column = '' THEN NULL ELSE column END IS NOT NULL
I think the key here is to differentiate between the case when the empty string is equivalent to NULL and when it isn't:
WHERE CASE WHEN '' = '' THEN -- e.g., SQL Server this is true
CASE WHEN col <> '' AND col IS NOT NULL THEN 'Y'
ELSE 'N'
END
WHEN COALESCE(col,NULL) IS NOT NULL THEN 'Y' -- Not SS, e.g., Oracle
ELSE 'N'
END = 'Y';
If the first case is true then empty string is not the same as null, and we have to test for both string being not null and string not being the empty string. Otherwise, our task is easier because empty string and null evaluate the same.
A try to shorten #DCookie's answer. I like his ( '' = '' ) test.
CASE WHEN ( '' = '' ) THEN ( column <> '' )
ELSE ( column = column )
END
Sadly, the above will not work. The next works in SQL-Server. I can't test in Oracle now:
CASE WHEN '' = '' THEN CASE WHEN column <> '' THEN 1 ELSE NULL END
ELSE CASE WHEN column = column THEN 1 ELSE NULL END
END
which can be written also as:
( '' = '' AND column <> '' )
OR ( '' IS NULL AND column = column )
I have an sql statement which I now need to change so it only returns values back that are integers.
SELECT * FROM col WHERE name != 'NULL' <-- this works
But now I need to extend that to something like:
SELECT * FROM col WHERE name != 'NULL' && name (is an int)
But I cannot figure out the sql for this. Does anyone have any ideas?
SQL:
SELECT * FROM col WHERE NOT name IS NOT NULL AND name NOT LIKE '%[^0-9]%'
or
SELECT * FROM col WHERE NOT name IS NOT NULL AND ISNUMERIC(name) = 1
Warning, ISNUMERIC can return 1 for some non numeric characters such as +, -, or $
Here is some more information on ISNUMERIC
Also, when comparing against NULL in SQL you must use IS NULL or IS NOT NULL
For SQL server this is the Syntax:
SELECT * FROM col WHERE NOT name IS NULL AND ISNUMERIC(name) = 1 AND NOT name LIKE '%.%'
Assuming "." is used for decimals
If you use MSSQL,
SELECT * FROM col WHERE name <> 'NULL' AND IsNumeric(name) = 1