How to Replace null with value when null - sql

I have a join query, I want Replace a NULL value with a string if is null.
If it is not NULL, replace with a null string.
select distinct Code= R_Schedule.FoodId,Name= FoodName + ' = price ' +(CONVERT( varchar(50),R_Foods.FoodPrice) + ISNULL(str(R_Schedule.dayId ),' - public-')) from R_Foods join R_Schedule on R_Foods.FoodId=R_Schedule.FoodId where R_Schedule.DayId=6 or DayId is null
please this this Image :
http://i.stack.imgur.com/Kv81f.png
Important section of my query is :
ISNULL(str(R_Schedule.dayId ),' - public -'))
This section if it is null return 'public' else if it is not null return column's id.
I don't want return id if it is not null.

You can use the CASE WHEN. When dayId is null then it will return - public- Else it will concat with '' Empty string
which you can use the other value to set if you want.
select distinct
Code = R_Schedule.FoodId,
Name = CASE WHEN R_Schedule.dayId IS NULL THEN FoodName + ' = price ' +(CONVERT( varchar(50),R_Foods.FoodPrice)) + ' - public-'
ELSE FoodName + ' = price ' +(CONVERT( varchar(50),R_Foods.FoodPrice)) + 'othervalue' END
from
R_Foods join R_Schedule
on R_Foods.FoodId=R_Schedule.FoodId
where
R_Schedule.DayId=6
or DayId is null

Related

How do I use concat and case switch together in PostgresSQL?

I am trying to use concat as function and inside the same function use case switch statements in PostgresSQL. The end goal is populating a table with a bunch of statements that change according to the case.
Does anybody know how to solve this?
I tried to use '+' and ',' but I always get the same Syntax error
(SELECT concat(cast(idpersonale as varchar(5)),' = '
,case when dstitolo is not null then dstitolo else '' end + ' '
,case when dsnome is not null then dsnome else '' end + ' '
,case when dscognome is not null then dscognome else '' end
dscognome)
FROM global.glb_personale
WHERE cch.glb_personale.idpersonale =inter.interventoeseguitoda) as InterventoEseguitoDa
FROM cch.pats_cch_interventi inter
WHERE (idtiporecord is null or not idtiporecord in('uti','int')) and
NOT idintervento IN (SELECT idint
FROM cch.glo_error_data_2
WHERE (iddb = 'cch') AND (cnerror = 1))
order by idintervento;
Concatenation works with || operator or concat(text1, text2, text3, ...) function. Every text literal can be also an expression which gives a type text, like a CASE clause:
demo:db<>fiddle
SELECT
'you' || 'can' || 'concat' || 'like' || 'that' AS concat1,
concat('or', 'like', 'that') AS concat2,
concat('Mix' || 'both', 'up') AS concat3,
concat(
'And',
CASE WHEN false THEN 'now' ELSE 'doint' END || 'this',
CASE
WHEN true THEN 'with' || 'some'
WHEN false THEN 'CASE'
ELSE 'clauses'
END
) AS concat4
That means in your case: Change concat(text1 + text2 + text3) to concat(text1, text2, text3) (because the texts are function arguments) or to text1 || text2 || text3
since you are using concat(), replace + to , instead.
(SELECT concat(cast(idpersonale as varchar(5)),' = '
,case when dstitolo is not null then dstitolo else '' end, ' '
,case when dsnome is not null then dsnome else '' end, ' '
,case when dscognome is not null then dscognome else '' end
dscognome)
FROM global.glb_personale
WHERE cch.glb_personale.idpersonale =inter.interventoeseguitoda) as InterventoEseguitoDa
FROM cch.pats_cch_interventi inter
WHERE (idtiporecord is null or not idtiporecord in('uti','int')) and
NOT idintervento IN (SELECT idint
FROM cch.glo_error_data_2
WHERE (iddb = 'cch') AND (cnerror = 1))
While the concatenation operator (||) and the function (concat) combine strings there is a big difference between them: how they handle nulls.
If any operand is null then the concatenation operator result is NULL.
If any operand is null then the concatenation function moves to the next operand, leaving previous and subsequent results as they would had the null operand not been present. Example:
with dscognome (idpersonale,dstitolo ,dsnome ,dscognome) as
( values (1,'a','b','c')
, (2,'d','e',null)
, (3,'f',null,'g')
, (4,null, 'h', 'i')
, (5,null, null, null)
)
select idpersonale
, dstitolo || dsnome || dscognome "using || opreator"
, concat(dstitolo,dsnome,dscognome) "using concat function"
from dscognome;
Since the entire purpose of the case structure is to handle nulls, using the concatenation function entirely negates it's requirement and the query can be written:
with dscognome (idpersonale,dstitolo ,dsnome ,dscognome) as
( values (1,'a','b','c')
, (2,'d','e',null)
, (3,'f',null,'g')
, (4,null, 'h', 'i')
, (5,null, null, null)
)
SELECT concat(cast(idpersonale as varchar),' = '
, dstitolo , ' '
, dsnome, ' '
, dscognome )
from dscognome;

MSSQL Case denoted by an integer returns a string

I have an issue with my SELECT statement in SSRS.
I want to use an integer to return strings values.
I tried it with this SELECT clause:
SELECT CASE #param = '1' THEN value like '__%' ELSE value like ' '
But it doesn't work, so I tried to use this one instead:
WHERE
((#param = '1' AND value like '__%') OR (#param = '0' AND value = '%'))
The expected result is: When the case is "1" SELECT should return only values which are not ' '
When the case is "0" SELECT should return all values = ' ' + '__%'
Thank you for your help
Your case condition is wrong formatted. You missed WHEN and END .It should be like that:
SELECT CASE WHEN #param = '1' THEN value like '__%' ELSE value like ' ' END
EDIT : As much as I understand you want to see Value if the #param='1' and if not the result will be '__%'. If yes this following CASE usage should be correct:
SELECT CASE WHEN #param = '1' THEN value ELSE value= '__%' END
I fix it my self condition is
((#column = '1' AND column like '__%') OR (#column = '0'AND column like '_%'))
WHERE
((#param = '1' AND value != '') OR (#param = '0'))
just try this

SQL get fields which are null/empty for each record returned

I have the below query which I'm using to retrieve records which have at least one of the following columns set as NULL or '':
MainImage
Summary
Description
DECLARE #missingFields varchar(100)
SET #missingFields = ''
SELECT
c.[UnitReference] as 'UnitRef',
t.[NodeName] as 'Property',
#missingFields as 'Field/s missing'
FROM [DetailPage] d
INNER JOIN [CRM] c
ON d.ItemID = c.ItemID
INNER JOIN
[Tree] t
ON d.[ID] = t.[ID]
WHERE (ISNULL(d.MainImage, '') = '' OR ISNULL(d.Summary,'') = '' OR ISNULL(d.[Description],'') = '')
AND c.[IsListed] = 1 AND c.[IsMarketed] = 1
This returns the data I want however I also need to build up a string which lists which of the columns is null or empty for the returned record, e.g. "Main Image is empty, Description is empty" when a record has empty MainImage and Description columns.
I've tried:
#missingFields = CASE WHEN ISNULL(MainImage, '') = '' THEN 'Main image is null' ELSE '' END -- etc...
But I can't include this with the data retrieval operations. How would I go about doing this?
SELECT c.[UnitReference] AS 'UnitRef',
t.[NodeName] AS 'Property',
( CASE
WHEN d.MainImage IS NULL
THEN 'Main Image is Null, '
WHEN d.MainImage = ''
THEN 'Main Image is Empty, '
ELSE ''
END )+
( CASE
WHEN d.Summary IS NULL
THEN 'Summary is Null, '
WHEN d.Summary = ''
THEN 'Summary is Empty, '
ELSE ''
END )+
( CASE
WHEN d.[Description] IS NULL
THEN 'Description is Null, '
WHEN d.[Description] = ''
THEN 'Description is Empty, '
ELSE ''
END ) AS MissingFields
FROM [DetailPage] d
INNER JOIN [CRM] c
ON d.ItemID = c.ItemID
INNER JOIN [Tree] t
ON d.[ID] = t.[ID]
WHERE( d.MainImage IS NULL OR d.MainImage = '' )
OR ( d.Summary IS NULL OR d.Summary = '' )
OR ( d.[Description] IS NULL OR d.[Description] = '' )
AND c.[IsListed] = 1
AND c.[IsMarketed] = 1

SQL Server stored proc substitute an empty string if column is empty or null

I need to check to see if a certain coloumn in my stored proc is either empty or null.
This is a snippet of what I have right now:
SELECT * ,
CASE WHEN a.USER IS NULL
THEN b.ROLE
ELSE ISNULL(a.FirstName,'') + ' ' + (ISNULL(a.MiddleName+' ','') + ISNULL(a.LastName,'')
END AS 'CustomerName'
I am checking to see if a.MiddleName is NULL but how do I also check to see if its empty and if its empty to just insert a empty string (no space)?
Thanks
Change to
SELECT
* ,
CASE
WHEN a.USER IS NULL
THEN b.ROLE
ELSE CASE
WHEN ISNULL(a.MiddleName, '') = ''
THEN ISNULL(a.FirstName,'') + ' ' + ISNULL(a.LastName,'')
ELSE ISNULL(a.FirstName,'') + ' ' + a.MiddleName + ' ' + ISNULL(a.LastName,'')
END
END AS 'CustomerName'
Another sollution is:
SELECT * ,
CASE WHEN a.USER IS NULL
THEN b.ROLE
ELSE ISNULL(a.FirstName,'') + replace( ( ' ' + ISNULL(a.MiddleName+' ',' '),' ',' ') + ISNULL(a.LastName,'')
END AS 'CustomerName'

T SQL Conditional String Concatenation

Have a 5 columns of address data. I need to concatenate these fields into a single address with spaces in between the values if they exist. If the column has a null value I should skip it and not enter any space.
select
case
when street_number != '' THEN (cast(street_number as int))
end as street_number,
case
when street_ext != '' then
case
when street_ext = 50 then '1/2'
end
end as street_ext,
case
when street_direct ! = '' then street_direct
end as street_direct,
case
when site_street ! = '' then site_street
end as site_street,
case
when site_address ! = '' then site_address
end as site_address
from parcel
what I'd like to do is have a variable and assign it to the value of the first column street_number, then when I move on to the next column, street_ext, if it isn't null I'd like to check to see if the variable is null and if not, append a space and the value...and so on down the road.
I'm rusty as hell and could use a push in the right direction.
Thanks everyone.
Use the "+" to concatenate strings in TSQL:
SELECT CASE
WHEN LEN(p.street_number) > 0 THEN p.street_number + ' '
ELSE ''
END +
CASE
WHEN p.street_ext = 50 THEN '1/2'
WHEN LEN(p.street_ext) > 0 THEN ''
ELSE p.street_ext
END + ' ' +
CASE
WHEN LEN(p.street_direct) > 0 THEN p.street_direct + ' '
ELSE ''
END +
CASE
WHEN LEN(p.site_street) > 0 THEN p.site_street + ' '
ELSE ''
END +
CASE
WHEN LEN(p.site_address) > 0 THEN p.site_address + ' '
ELSE ''
END AS full_address
FROM PARCEL p
The LEN function returns zero if the string value is NULL, or a zero length string.
Nested isnulls could do what you need. Something like:
SELECT
ISNULL(streetnumber + ' ', '')
+ ISNULL(streetext + ' ', '')
etc
relying on the fact that NULL + ' ' = NULL.
Something along the lines of:
select coalesce(street_number+' ','')+
coalesce(case when street_ext=50 then '1/2' else null end+' ','')+
coalesce(street_direct+' ','')+
coalesce(site_street+' ','')+
coalesce(site_address,'')
from parcel
I have assumed your data types are all varchar or similar for simplicity. If you are OK with removing any double spaces, how about:
rtrim(ltrim(replace(isnull(street_number) + ' '
+ isnull(street_ext) + ' '
+ isnull(street_direct) + ' '
+ isnull(site_street) + ' '
+ isnull(site_address), ' ', ' ')))
First I would declare the seperator as a variable, because customers are notorious for changing these.
I would do this as follows:
DECLARE #AddressSeperator NVARCHAR(5) = ' '
...and then for the column declation, I'd use the following:
, CONCAT
(
(CASE WHEN LEN(p.street_number) > 0 THEN p.street_number + #AddressSeperator ELSE '' END)
, (CASE WHEN p.street_ext = 50 THEN '1/2' + #AddressSeperator WHEN LEN(p.street_ext) > 0 THEN p.street_ext + #AddressSeperator ELSE '' END)
, (CASE WHEN LEN(p.street_direct) > 0 THEN p.street_direct + #AddressSeperator ELSE '' END)
, (CASE WHEN LEN(p.site_street) > 0 THEN p.site_street + #AddressSeperator ELSE '' END)
, ISNULL(p.site_address, '')
) AS [full_address]