Avatar of Zack
Zack
Flag for Australia asked on

Setting a NULL value to an empty string in SQL

Heyas,

I have the following query:

SET Environment.Variables.flag3 =(THE(SELECT ITEM f3.InvCode FROM Database.WMB_DBW_SAP_CODE as f3 WHERE f3.ItemName='presendonadmit' AND f3.DBWCode=InputRoot.MRM.ns:DG1[3].ns:DG26PresentOnAdmissionInd));

If this SET statement returns a NULL value is their way to change  'f3.DBWCode' field to an empty string ''.

Any assistance is always appreciated.

Thank you.
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Zack

8/22/2022 - Mon
Karen

COALESCE(f3.DBWCode, '')
Pawan Kumar

You can use below..

ISNULL(f3.DBWCode, '')
Zack

ASKER
Heyas,

Can you be a bit more descriptive as to how I would use these?

Thank you.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Pawan Kumar

Hi,

Please find the usage below.

ISNULL

SELECT Col1, ISNULL(Col2,0) Col2
FROM TableName

Open in new window


SELECT Col1, ISNULL(Col2,'') Col2
FROM TableName

Open in new window



COALESCE

SELECT Col1 , COALESCE(Col2,0) Col2
FROM TableName

Open in new window



SELECT Col1 , COALESCE(Col2,'') Col2
FROM TableName

Open in new window



Definition -

ISNULL - Replaces NULL with the specified replacement value.

ISNULL ( check_expression , replacement_value )  

From MSDN - The value of check_expression is returned if it is not NULL; otherwise, replacement_value is returned after it is implicitly converted to the type of check_expression, if the types are different. replacement_value can be truncated if replacement_value is longer than check_expression.

COALESCE - Evaluates the arguments in order and returns the current value of the first expression that initially does not evaluate to NULL.

COALESCE ( expression [ ,...n ] )  

FROM MSDN -

The COALESCE expression is a syntactic shortcut for the CASE expression. That is, the code COALESCE(expression1,...n) is rewritten by the query optimizer as the following CASE expression:
CASE
WHEN (expression1 IS NOT NULL) THEN expression1
WHEN (expression2 IS NOT NULL) THEN expression2
...
ELSE expressionN
END

REF [ https://msdn.microsoft.com/en-in/library/ms190349.aspx ]
Karen

hmm... hang on, do you mean
If this SET statement returns a NULL value
or
If this SELECT statement returns a NULL value
Zack

ASKER
Hi Snowberry,

Clarification: If this SET statement returns a NULL value.

Thank you.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Pawan Kumar

May be..this

SET Environment.Variables.flag3 = ISNULL (SELECT ITEM f3.InvCode FROM Database.WMB_DBW_SAP_CODE as f3 
									WHERE f3.ItemName='presendonadmit' 
									AND f3.DBWCode=InputRoot.MRM.ns:DG1[3].ns:DG26PresentOnAdmissionInd) , '' )

Open in new window

Pawan Kumar

Or you can check like below..after SET,...

ISNULL ( Environment.Variables.flag3 , '' )
Karen

ok, in that case your "subject" confused me

IF Environment.Variables.flag3 IS NULL THEN
    UPDATE Database.WMB_DBW_SAP_CODE as f3 SET f3.DBWCode = '' WHERE f3.ItemName='presendonadmit' 

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Zack

ASKER
Hi Pawan,

Thank for the input but I what I need is just the ISNULL to filter just on the f3.DBWCode field see below for a pseudocode example.

SET Environment.Variables.flag3 =(THE(SELECT ITEM f3.InvCode FROM Database.WMB_DBW_SAP_CODE as f3 WHERE f3.ItemName='presendonadmit' AND f3.DBWCode=ISNULL(InputRoot.MRM.ns:DG1[3].ns:DG26PresentOnAdmissionInd),'');

Thank you.
Zack

ASKER
Hi Snowberry,

My apologies to you I just re-read my question I didn't make it 100% clear what I require is something like this:

SET Environment.Variables.flag3 =(THE(SELECT ITEM f3.InvCode FROM Database.WMB_DBW_SAP_CODE as f3 WHERE f3.ItemName='presendonadmit' AND f3.DBWCode=ISNULL(InputRoot.MRM.ns:DG1[3].ns:DG26PresentOnAdmissionInd),'');

How I filter just on the f3.DBWcode field for ISNULL?

Thank you.
Pawan Kumar

So do you need records where f3.DBWcode IS NULL ?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Pawan Kumar

may be..

SET Environment.Variables.flag3 =(THE(SELECT ITEM f3.InvCode FROM Database.WMB_DBW_SAP_CODE as f3 WHERE f3.ItemName='presendonadmit' AND f3.DBWCode=ISNULL(InputRoot.MRM.ns:DG1[3].ns:DG26PresentOnAdmissionInd),'');

AND f3.DBWCode IS NULL
Karen

Do you want to do the following, but in only one statement?

SET Environment.Variables.flag3 =(THE(SELECT ITEM f3.InvCode FROM Database.WMB_DBW_SAP_CODE as f3 WHERE f3.ItemName='presendonadmit' AND f3.DBWCode=InputRoot.MRM.ns:DG1[3].ns:DG26PresentOnAdmissionInd));
IF Environment.Variables.flag3 IS NULL THEN
    SET Environment.Variables.flag3 =(THE(SELECT ITEM f3.InvCode FROM Database.WMB_DBW_SAP_CODE as f3 WHERE f3.ItemName='presendonadmit' AND f3.DBWCode=''));

Open in new window

Pawan Kumar

Or may be...this

SET Environment.Variables.flag3 = 

CASE WHEN (THE(SELECT ITEM f3.InvCode FROM Database.WMB_DBW_SAP_CODE as f3 WHERE f3.ItemName='presendonadmit' AND f3.DBWCode=InputRoot.MRM.ns:DG1[3].ns:DG26PresentOnAdmissionInd)) IS NULL
THEN 
	(THE(SELECT ITEM f3.InvCode FROM Database.WMB_DBW_SAP_CODE as f3 WHERE f3.ItemName='presendonadmit' AND f3.DBWCode=''));
END

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Zack

ASKER
Hi Pawan,

Yes, I need the SET statement to flag when  f3.DBWCode ISNULL and change it to ''.

Thank you.
Karen

Do you want to know when  f3.DBWCode is NULL, or when Environment.Variables.flag3 is NULL ??????
Pawan Kumar

Didnt get can you explain via Pesudo code?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Zack

ASKER
Hi Snowberry,

When f3.DBWCode  ISNULL in the SET Statement: SET Environment.Variables.flag3 =(THE(SELECT ITEM f3.InvCode FROM Database.WMB_DBW_SAP_CODE as f3 WHERE f3.ItemName='presendonadmit' AND f3.DBWCode=InputRoot.MRM.ns:DG1[3].ns:DG26PresentOnAdmissionInd));

My apologies for the lack of clarification initially.

Thank you.
Pawan Kumar

May be this

SET Environment.Variables.flag3 = 
    	
	(THE(SELECT ITEM f3.InvCode FROM Database.WMB_DBW_SAP_CODE as f3 WHERE f3.ItemName='presendonadmit' AND ISNULL(f3.DBWCode , 
	
	(THE(SELECT ITEM f3.InvCode FROM Database.WMB_DBW_SAP_CODE as f3 WHERE f3.ItemName='presendonadmit' AND f3.DBWCode=''))

	=InputRoot.MRM.ns:DG1[3].ns:DG26PresentOnAdmissionInd))

Open in new window

Pawan Kumar

SET Environment.Variables.flag3 =
          
      (THE(SELECT ITEM f3.InvCode FROM Database.WMB_DBW_SAP_CODE as f3 WHERE f3.ItemName='presendonadmit' AND ISNULL(f3.DBWCode ,
      
      (THE(SELECT ITEM f3.InvCode FROM Database.WMB_DBW_SAP_CODE as f3 WHERE f3.ItemName='presendonadmit' AND f3.DBWCode=''))

      =InputRoot.MRM.ns:DG1[3].ns:DG26PresentOnAdmissionInd))


This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Karen

it is really unclear what you want

lets try an example

if there is a record in f3 table

InvCode = 123
ItemName = 'presendonadmit'
DBWCode = 456

then your query will return 123 if InputRoot.MRM.ns:DG1[3].ns:DG26PresentOnAdmissionInd = 456

So what problem are you trying to cope with ?
Zack

ASKER
Hi Snowflake,

The best way I can clarify is if 'InputRoot.MRM.ns:DG1[3].ns:DG26PresentOnAdmissionInd' ISNULL change to ''.

Thank you.
Pawan Kumar

Ok try this then


SET Environment.Variables.flag3 =

 (THE(SELECT ITEM f3.InvCode FROM Database.WMB_DBW_SAP_CODE as f3 WHERE f3.ItemName='presendonadmit' AND f3.DBWCode= ISNULL(InputRoot.MRM.ns:DG1[3].ns:DG26PresentOnAdmissionInd,'')))
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Pawan Kumar

or you need like below

SET Environment.Variables.flag3 =

 (THE(SELECT ITEM f3.InvCode FROM Database.WMB_DBW_SAP_CODE as f3 WHERE f3.ItemName='presendonadmit' AND ISNULL(f3.DBWCode,'')= ISNULL(InputRoot.MRM.ns:DG1[3].ns:DG26PresentOnAdmissionInd,'')))
Karen

so

if there is a record in f3 table

InvCode = 123
ItemName = 'presendonadmit'
DBWCode = 456

then you want your query to return 123 if InputRoot.MRM.ns:DG1[3].ns:DG26PresentOnAdmissionInd IS NULL


OR


if there is a record in f3 table

InvCode = 123
ItemName = 'presendonadmit'
DBWCode = ''

then you want your query to return 123 if InputRoot.MRM.ns:DG1[3].ns:DG26PresentOnAdmissionInd IS NULL
Zack

ASKER
Hi Snowflake

Correction:

so

if DBWCode equals NULL

InvCode = 123
ItemName = 'presendonadmit'
DBWCode = NULL

Then I want DBWCode to be:

InvCode = 123
ItemName = 'presendonadmit'
DBWCode = ''

I think SET Environment.Variables.flag3 =

 (THE(SELECT ITEM f3.InvCode FROM Database.WMB_DBW_SAP_CODE as f3 WHERE f3.ItemName='presendonadmit' AND ISNULL(f3.DBWCode,'')= ISNULL(InputRoot.MRM.ns:DG1[3].ns:DG26PresentOnAdmissionInd,'')))

Posted by Pawan will work I will try in the next hr.

Thanks.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Karen

are you saying you want to update the database????
your last comment does not make any sense
Pawan Kumar

I think he wanted to compare these fields and WHEN both are NULL then the comparison was failing.

Now when we are replacing NULLs with '' the comparison will happen.

SOLUTION
Karen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Pawan Kumar

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Zack

ASKER
Thank you very much for the help guys.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.