Replace current table with coalesce statement - sql

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.

Related

How do I do multiple CASE WHEN conditions using SQL Server 18 for filtering results?

I'm trying to filter the results based on the status of two parameters from the user. These parameters are TaxCode and CompanyName.
Now I will tell you the rules and the query I am trying to write.
Rules:
1- If the name field is empty and only the taxcode field is full, only the columns with that taxcode should appear.
2- If the Taxcode field is empty and only the name field is full, only columns similar to that name should appear.
3- Finally, if both are filled, I want to combine the two expressions with or and I want them both to be common.
When TaxCode(IdNoPartNumber) is entered, I use this query to reach another table and reach its id(Uid):
select p.Uid FROM PartyIdentificationNumber as pin
join Party as p
on pin.Uid = p.Uid
where p.EI = 'E' and pin.IdNoPartNumber = #TaxCode
When searching for a name:
LastName LIKE #CompanyName
I don't know much about sql and the code I wrote started to get longer and confused me. Thank you from now. I hope that has been revealing:
SELECT *
FROM dbo.Party
WHERE Uid = CASE WHEN LastName IS NULL
THEN (
select p.Uid FROM PartyIdentificationNumber as pin
join Party as p
on pin.Uid = p.Uid
where p.EI = 'E' and pin.IdNoPartNumber = #TaxCode)
END
OR
LastName LIKE #CompanyName
I don't believe you need a CASE statement and think you could possibly do something like this with IF STATEMENTS:
DECLARE #TaxCode varchar(10) = ''
DECLARE #CompanyName varchar(20) = ''
IF (ISNULL(LTRIM(RTRIM(#CompanyName)),'') = '')
AND (ISNULL(LTRIM(RTRIM(#TaxCode)),'') <> '')
BEGIN
SELECT p.Uid
FROM PartyIdentificationNumber as pin
join Party as p on pin.Uid = p.Uid
WHERE p.EI = 'E' and pin.IdNoPartNumber = #TaxCode
END
ELSE IF (ISNULL(LTRIM(RTRIM(#TaxCode)),'') = '')
AND (ISNULL(LTRIM(RTRIM(#CompanyName)),'') <> '')
BEGIN
SELECT p.Uid
FROM PartyIdentificationNumber as pin
join Party as p on pin.Uid = p.Uid
WHERE LastName LIKE '%' + LTRIM(RTRIM(#CompanyName)) + '%'
END
ELSE IF (ISNULL(LTRIM(RTRIM(#TaxCode)),'') <> '')
AND (ISNULL(LTRIM(RTRIM(#CompanyName)),'') <> '')
BEGIN
PRINT 'REPLACE THIS WITH SELECT'
END
I am only guessing the SELECTS you need based on what you've written.
If you need to use CASE, then the basic format for that is as follows:
SELECT
CASE
WHEN (ISNULL(LTRIM(RTRIM(#CompanyName)),'') = '')
AND (ISNULL(LTRIM(RTRIM(#TaxCode)),'') <> '') THEN 'DO THIS 1'
WHEN (ISNULL(LTRIM(RTRIM(#TaxCode)),'') = '')
AND (ISNULL(LTRIM(RTRIM(#CompanyName)),'') <> '') THEN 'DO THIS 2'
WHEN (ISNULL(LTRIM(RTRIM(#TaxCode)),'') <> '')
AND (ISNULL(LTRIM(RTRIM(#CompanyName)),'') <> '') THEN 'DO THIS 3'
ELSE
'DO THIS 4'
END
FROM [TABLE_NAME]

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 = '')

Fill Variable with Case Statement Results

I have a questionnaire that my users have filled out (several thousand a day)
The result is each questionnaire record contains 70 something fields (that correspond to each question)
I've been asked to identify all the affirmatives for each of the 70 questions and concatentate them into one field (a summary of all the issues identified for that record).
In other languages (VBA in particular) I would accomlish this by initializing a variable to '', looping through my recordset and setting the variable to what it was previously + the field name of the issue. I'm not sure how to accomplish this in sql.
I've tried...
DECLARE #strFYI AS NVARCHAR
SET #strFYI = ''
SELECT
a.record_num
,CASE
WHEN a.Date_Missing = 'Yes' THEN #strFYI = #strFYI + 'Date_Missing, '
WHEN a.Unclear_Images = 'Yes' THEN #strFYI = #strFYI + 'Unclear_Images, '
WHEN a.Damage = 'Yes' THEN #strFYI = #strFYI + 'Damage, '
ELSE #strFYI
END AS FYI_Reasons
FROM
questionaretable a
But obviously that doesn't work. I'll also need to trim the last comma and space off the list once it's generated, but that shouldn't be a problem... I'm just not sure how to iterate through my records and build this concatenation in tsql :) I'm not even sure (because the syntax is wrong) if the variable would be reset to '' before each record was evaluated!
Can anyone help me out here?
This will be very ugly for 70 columns.
SELECT record_num, LEFT(strFYI, LEN(strFYI) - 2)
FROM (
SELECT
a.record_num,
(CASE WHEN a.Date_Missing = 'Yes' THEN 'Date_Missing, ' ELSE '' END) +
(CASE WHEN a.Unclear_Images = 'Yes' THEN 'Unclear_Images, ' ELSE '' END) +
(CASE WHEN a.Damage = 'Yes' THEN 'Damage, ' ELSE '' END) as strFYI
FROM
questionaretable a
) T
Maybe is cleaner using IIF
IIF ( boolean_expression, true_value, false_value )
SELECT record_num, LEFT(strFYI, LEN(strFYI) - 2)
FROM (
SELECT
a.record_num,
IIF(a.Date_Missing = 'Yes', 'Date_Missing, ' , '' ) +
IIF(a.Unclear_Images = 'Yes', 'Unclear_Images, ', '') +
IIF(a.Damage = 'Yes', 'Damage, ', '') as strFYI
FROM
questionaretable a
) T
As CElliot mention not IIF in 2008 so another solution may be
isnull((select 'Date_Missing, ' where a.Date_Missing = 'Yes'),'')

How to remove blank rows from SQL result set

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

Check null or empty data from three columns in SQL Server

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