Returning Null Value instead of 'NOT UPDATED' - sql

SELECT top 1 'Uploaded_Date' =
CASE isnull(Uploaded_Date,'')
WHEN Uploaded_Date THEN 'NOT UPDATED'
else uploaded_date
END
FROM ABC
or
SELECT top 1 'Uploaded_Date' =
CASE isnull(Uploaded_Date,0)
WHEN 0 THEN 'NOT UPDATED'
else uploaded_date
END
FROM ABC
when the column Uploaded_date is null or empty it is supposed to return NOT UPDATED but instead NULL is get returned..!
Thanks in Advance

Try with the below query.
SELECT top 1
CASE WHEN (Uploaded_Date IS NULL or LTRIM (RTRIM (isnull(Uploaded_Date,'')))='NULL')
THEN CAST('NOT UPDATED' AS VARCHAR(50))
ELSE CAST(uploaded_date AS VARCHAR(50)) END as 'Uploaded_Date'
FROM ABC

You are trying to set an int value to Uploaded_Date which is a varchar(50).Instead of storing int 0 store '0' i.e varchar and compare in the case statement.It should work.
SELECT top 1 'Uploaded_Date' =
CASE isnull(Uploaded_Date,'0')
WHEN '0' THEN 'NOT UPDATED'
else uploaded_date
END
FROM ABC

Related

Create a custom column based on the values of three other columns

In this project I'm currently working on (I'm building a bridge between a desktop app and a new e-shop) there is a products table that has some spare columns defined that can be used for whatever reason the end user might need some custom data to be stored into.
So, the user needed to set a true/false flag to determine whether the products would appear in three different sliders... Unfortunately, the person who implemented this, didn't even use the same type of spare columns... So,
Slider1's flag is stored in a varchar(50) column
Slider2's flag is stored in a float column
Slider3's flag is stored in a float column
Additionally I ran a SELECT DISTINCT <column> for each one of them to get an idea of the actual data stored in each column and got the following results:
The varchar column has the following data stored in it:
FLDSTRING1
NULL
''
0
1
194276400456
The float column has the following data stored:
FLDFLOAT5
NULL
0
1
And the other float column has this:
FLDFLOAT6
NULL
1
Also, I ran the following query to find the different combinations of the data stored for each column:
SELECT FLDSTRING1, FLDFLOAT5, FLDFLOAT6
FROM MATERIAL
GROUP BY FLDSTRING1, FLDFLOAT5, FLDFLOAT6
and got the following combinations...
FLDSTRING1
FLDFLOAT5
FLDFLOAT6
NULL
NULL
NULL
NULL
NULL
1
NULL
0
NULL
NULL
1
NULL
NULL
1
1
''
NULL
NULL
''
NULL
1
0
NULL
NULL
0
0
NULL
1
NULL
NULL
1
NULL
1
1
0
NULL
1
1
NULL
1
1
1
194276400456
0
NULL
What I need after all this introduction...
I want a concatenated string of three comma-separated values like this
NEWPROD for when FLDSTRING1 would evaluate to true - anything not NULL, 0, or ''
CUSTOM1 for when FLDFLOAT5 would evaluate to true - basically the value 1
CUSTOM2 for when FLDFLOAT6 would evaluate to true - again value 1
After some trial and error I managed to bring this to a point that it kind of works, in the sense that it brings the correct values, not comma-separated though...
SELECT
FLDSTRING1, FLDFLOAT5, FLDFLOAT6,
CONCAT(CASE WHEN ISNULL(FLDSTRING1, '') = '' THEN '' ELSE 'NEWPROD' END,
CASE WHEN ISNULL(FLDFLOAT5, '') = '' THEN '' ELSE 'CUSTOM1' END,
CASE WHEN ISNULL(FLDFLOAT6, '') = '' THEN '' ELSE 'CUSTOM2' END) AS TAGS
FROM
MATERIAL
GROUP BY
FLDSTRING1, FLDFLOAT5, FLDFLOAT6;
FLDSTRING1
FLDFLOAT5
FLDFLOAT6
TAGS
NULL
NULL
NULL
NULL
NULL
1
CUSTOM2
NULL
0
NULL
NULL
1
NULL
CUSTOM1
NULL
1
1
CUSTOM1CUSTOM2
''
NULL
NULL
''
NULL
1
CUSTOM2
0
NULL
NULL
NEWPROD
0
0
NULL
NEWPROD
1
NULL
NULL
NEWPROD
1
NULL
1
NEWPRODCUSTOM2
1
0
NULL
NEWPROD
1
1
NULL
NEWPRODCUSTOM1
1
1
1
NEWPRODCUSTOM1CUSTOM2
194276400456
0
NULL
NEWPROD
Problem #1 is I don't quite understand how this works... I mean, value 0 isn't '', but still for the combination of NULL 0 NULL I get an empty value, which is what I wanted... But how does it do that?
And also, can someone update my final query to comma-separate the calculated TAGS column? Problem #2 is that I don't want it to contain just two commas, like ,,, when the combination wouldn't justify any of the three values to appear... It should work like PHP's implode() works...
To help you help me with this, I'm including a fiddle with the setup of the scenario I describe here... Thanks in advance!
Since you are using SQL Server 2014, instead of CONCAT_WS you may try STUFF as shown below. By prepending the delimiter , before all strings ,the STUFF will remove the first comma found.
SELECT
FLDSTRING1,
FLDFLOAT5,
FLDFLOAT6,
STUFF(
CONCAT(
CASE WHEN FLDSTRING1 IS NULL OR FLDSTRING1 IN ('0','') THEN '' THEN '' ELSE ',NEWPROD' END,
CASE WHEN FLDFLOAT5 IS NULL THEN '' ELSE ',CUSTOM1' END,
CASE WHEN FLDFLOAT6 IS NULL THEN '' ELSE ',CUSTOM2' END
),
1,1,''
) AS TAGS
FROM #MATERIAL
GROUP BY FLDSTRING1, FLDFLOAT5, FLDFLOAT6;
View working demo db fiddle
Let me know if this works for you.
Use the CONCAT_WS() function to concat values into a comma (or other separator) separated list, which ignores nulls.
To use CONCAT_WS(), you want to pass it a true NULL if the value is "blank" (by your definition), otherwise your custom label:
SELECT DISTINCT
FLDSTRING1,
FLDFLOAT5,
FLDFLOAT6,
CONCAT_WS(',',
CASE WHEN FLDSTRING1 IS NULL OR FLDSTRING1 = '' OR FLDSTRING1 = '0' THEN NULL ELSE 'NEWPROD' END,
CASE WHEN FLDFLOAT5 IS NULL OR FLDFLOAT5 = 0 THEN NULL ELSE 'CUSTOM1' END,
CASE WHEN FLDFLOAT6 IS NULL OR FLDFLOAT6 = 0 THEN NULL ELSE 'CUSTOM2' END) AS TAGS
FROM MATERIAL
Replaced GROUP BY with DISTINCT because it's simpler and (here) achieves the same thing.
If CONCAT_WS is not available:
SELECT DISTINCT
FLDSTRING1,
FLDFLOAT5,
FLDFLOAT6,
REPLACE(REPLACE(REPLACE(CONCAT(
CASE WHEN FLDSTRING1 IS NULL OR FLDSTRING1 = '' OR FLDSTRING1 = '0' THEN 'X' ELSE 'NEWPROD' END,
',',
CASE WHEN FLDFLOAT5 IS NULL OR FLDFLOAT5 = 0 THEN 'X' ELSE 'CUSTOM1' END,
',',
CASE WHEN FLDFLOAT6 IS NULL OR FLDFLOAT6 = 0 THEN 'X' ELSE 'CUSTOM2' END
), ',X', ''), 'X,', ''), 'X', '') AS TAGS
FROM MATERIAL
See dbfiddle.

Get the value of END AS column in case when in where clause

I have a query where i used case when to KNOWN_AS column stating when its null then 'null' else 'not null' end as known_as2. Now in where clause i want to bring rows which only contain 'Not null' .
SELECT i.individual_ref, 0, 'KNOWNAS', i.FORENAMES, i.KNOWN_AS,
case when KNOWN_AS is null then 'Null' else ' Not null' end as known_as
FROM TestDatabase.dbo.INDIVIDUAL I
JOIN TestDatabase.dbo.MEMBER M ON M.INDIVIDUAL_REF=I.INDIVIDUAL_REF
WHERE m.member_status IN(33,1316)
AND i.KNOWN_AS IS null or i.KNOWN_AS=''
and m.MEMBER_STATUS in (33,1316)
and LEN(i.FORENAMES) > '1' and i.FORENAMES !=''
AND i.FORENAMES IS NOT NULL
Reason i'm want help is :-
I have a table which contain Forename, surname and known_as field.
I want to get members who's known_as field is blank/null and forename is not null or blank and forename length is >1 . How can i achieve it. the member status is from another table call member where i want member who are in active and pending status hence i said WHERE m.member_status IN (33,1316). Any solution please.
Finally I have solved it using
SELECT i.individual_ref,0,'KNOWNAS',
case when KNOWN_AS is null then 'Null' else ' Not null' end as knownas2
FROM TestDatabase.dbo.INDIVIDUAL I
JOIN TestDatabase.dbo.MEMBER M ON M.INDIVIDUAL_REF=I.INDIVIDUAL_REF
WHERE m.member_status IN(33,1316)
and len(i.forenames)>2 and
(IsNull(i.forenames, '') <> '') and (i.known_as is null or i.known_as='')
I had to take len(i.forenames, '')>2 instead of 1 because some members also has forename by mistakenly updated as Mr.
Now in where clause i want to bring rows which only contain 'Not null' .
Just modify your WHERE clause with IS NOT NULL as :
SELECT i.individual_ref, 0, 'KNOWNAS', i.FORENAMES, i.KNOWN_AS
FROM DiTestDatabase.dbo.INDIVIDUAL I JOIN
DiTestDatabase.dbo.MEMBER M
ON M.INDIVIDUAL_REF = I.INDIVIDUAL_REF
WHERE m.member_status IN (33,1316) AND
LEN(i.FORENAMES) > 1 AND i.FORENAMES != '' AND
i.FORENAMES IS NOT NULL AND i.KNOWN_AS IS NOT NULL;
Note :
LEN() will return INT type. So, you don't need to use ''.
Might be below query will help you.
SELECT i.individual_ref, 0, 'KNOWNAS', i.FORENAMES, i.KNOWN_AS
,CASE WHEN i.KNOWN_AS IS NULL THEN 'null'
ELSE 'Not null' END AS KNOWN_AS2
FROM DiTestDatabase.dbo.INDIVIDUAL I JOIN
DiTestDatabase.dbo.MEMBER M
ON M.INDIVIDUAL_REF = I.INDIVIDUAL_REF
WHERE m.member_status IN (33,1316) AND
LEN(i.FORENAMES) > 1 AND i.FORENAMES != '' AND
i.FORENAMES IS NOT NULL AND i.KNOWN_AS IS NOT NULL;
this way we can get solution
SELECT * FROM (
SELECT i.individual_ref, 0, 'KNOWNAS', i.FORENAMES, i.KNOWN_AS,
case when KNOWN_AS is null then 'Null' else 'Not null' end as known_as
FROM DiTestDatabase.dbo.INDIVIDUAL I
JOIN DiTestDatabase.dbo.MEMBER M ON M.INDIVIDUAL_REF=I.INDIVIDUAL_REF
WHERE m.member_status IN(33,1316)
AND i.KNOWN_AS IS null or i.KNOWN_AS=''
and m.MEMBER_STATUS in (33,1316)
and LEN(i.FORENAMES) > 1 and i.FORENAMES !=''
AND i.FORENAMES IS NOT NULL) t
WHERE t.known_as='Not null'

How to compare two columns in the CASE statement

I have two columns Speed_A and Speed_B.
Now I will compare both columns and will select the higher one in a new table.
Something like this:
SELECT ...
CASE
WHEN a.Speed_A > a.Speed_B THEN a.Speed_A
WHEN a.Speed_A < a.Speed_B THEN a.Speed_B
ELSE 0 --unknown
END as SpeedLimit,
How does it work?
Assuming your speeds are numerically typed you might get this as easy as:
SELECT CASE WHEN a.Speed_A>a.Speed_B THEN a.Speed_A ELSE a.Speed_B END AS SpeedLimit
[...FROM SomeWhere...]
Your ELSE will only occur, when A and B are equal, In this case it doesn't matter, which one you return.
UPDATE
columnĀ“s data type is integer. And yes there are nulls
If A is null return B, If b is null return A (works for A and B is null implicitly). Otherwise both are not null and therefore the statement above should be perfect
SELECT CASE WHEN a.Speed_A IS NULL THEN a.Speed_B
ELSE CASE WHEN a.Speed_B IS NULL THEN a.Speed_A
ELSE CASE WHEN a.Speed_A>a.Speed_B THEN a.Speed_A ELSE a.Speed_B
END
END
END AS SpeedLimit
FROM Dummy
[...FROM SomeWhere...]
You can create a inline max function that return the max between two values something like:
create function dbo.InlineMax(#val1 int, #val2 int)
returns int
as
begin
if #val1 > #val2
return #val1
return isnull(#val2,#val1)
end
and you can use it like this:
SELECT dbo.InlineMax(a.Speed_A, a.Speed_B)
FROM ....
Nested Case and Case without Column Name
select
CASE ColumnName
WHEN 'enter value on which you want to execute Than Block'
THEN
CASE ColumnName
WHEN 1 THEN 'return value or Column Name'
ELSE 'return value or Column Name in case of else'
END
WHEN 'another condition, value of column on which you want to execute then block'
THEN
CASE WHEN 'Condition etc => ColumnName > 5 or ColumnName ='abc' ' THEN 'return value or column name' ELSE 'return value or column name' END
ELSE 'return value or column name'
END as status
FROM TableName

Nested Case Then in SQL

I'm trying to do the following which involves a complex query;
1. First I try to select the column based on some logic
2. then based on the value of the selected column a value is selected
3. finally based on the previous value a logic is applied to get the final value
How can I have the result from 2 in the ELSE of the 3rd Case..Then statement ?
CASE
CASE
CASE
WHEN MDC.Network ='I' THEN INNBenefitID ELSE OONBenefitID
END
WHEN 'INNDWO' THEN CASE WHEN DWO.Description IS NOT NULL THEN DWO.Description ELSE MDC.CostShareValue END
WHEN 'OONDWO' THEN CASE WHEN DWO.Description IS NOT NULL THEN DWO.Description ELSE MDC.CostShareValue END
ELSE isnull(dbo.fn_FormatText(MDC.CostShareValue, CostShareType),'''')
END
WHEN '0%' THEN 'Covered 100%'
WHEN '$0' THEN 'Covered 100%'
ELSE ????
END
Are you looking for something like that? :
declare #value varchar(255) = (
CASE
WHEN MDC.Network ='I' THEN INNBenefitID ELSE OONBenefitID
WHEN 'INNDWO' THEN CASE WHEN DWO.Description IS NOT NULL THEN DWO.Description ELSE MDC.CostShareValue
WHEN 'OONDWO' THEN CASE WHEN DWO.Description IS NOT NULL THEN DWO.Description ELSE MDC.CostShareValue
ELSE isnull(dbo.fn_FormatText(MDC.CostShareValue, CostShareType),'''')
END
)
CASE
WHEN '0%' THEN 'Covered 100%'
WHEN '$0' THEN 'Covered 100%'
ELSE #value
END
Really, really hard to do this right without a schema or sample data. In the future consider creating a SqlFiddle for us. But one approach you might consider is using a Common Table Expression (CTE) to define an intermediary structure which you then query.
;WITH theCTE AS (SELECT
CASE
CASE
WHEN MDC.Network ='I' THEN INNBenefitID ELSE OONBenefitID
END
WHEN 'INNDWO' THEN COALESCE(DWO.Description, MDC.CostShareValue)
WHEN 'OONDWO' THEN COALESCE(DWO.Description, MDC.CostShareValue)
ELSE COALESCE(dbo.fn_FormatText(MDC.CostShareValue, CostShareType), '')
END AS foo
FROM
bar
)
SELECT
CASE
WHEN foo IN ('0%', '$0') THEN 'Covered 100%'
ELSE '' -- Do something with foo --
END AS finalColumn
FROM
theCTE

CASE statement giving wrong results inside my stored procedure

I have a case inside my stored procedure which I use before executing the data.
DECLARE #Setup nvarchar(50)
SELECT
#ZipCode = CASE
WHEN REPLACE(#ZipCode, '0', '') = ''
THEN NULL ELSE #ZipCode
END,
#ZipCodeEND = CASE
WHEN REPLACE(#ZipCodeEND, '0', '') = ''
THEN NULL ELSE #ZipCodeEND
END,
SELECT
#Setup = CASE WHEN (LEN(ISNULL(#ZipCode, ''))) > 0 THEN '1' ELSE '0' END +
CASE WHEN (LEN(ISNULL(#ZipCodeEND,''))) > 0 THEN '1' ELSE '0' END
IF ISNULL(#ID, 0) = 0
BEGIN
INSERT INTO dbo.MapToStaticValuesTable(ZipCode, ZipCodeEND, Setup)
VALUES(#ZipCode, #ZipCodeEND, #Setup)
END
The problem here is even if zipcode and zipcodeEnd are empty and set to null after being saved into the table I keep getting the value "11" instead of getting "00".
Now if I do the same example with nvarchar values it would work, but since ZipCode and ZipCodeEnd are set to int it's acting weird.
It is acting weird because you are using string functions on integers. Not sure what you are trying to achieve with your code but I'm sure it can be done just by checking the values as integers.
I guess this could be what you are looking for.
select case when nullif(#ZipCode, 0) is null then '0' else '1' end +
case when nullif(#ZipCodeEND, 0) is null then '0' else '1' end
One example of weird
select isNull(#ZipCode, '')
return 0 if #ZipCode is null.