Solved

Setting a NULL value to an empty string in SQL

Posted on 2016-10-04
32
35 Views
Last Modified: 2016-10-04
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.
0
Comment
Question by:Zack
  • 14
  • 9
  • 9
32 Comments
 
LVL 9

Expert Comment

by:Karen
Comment Utility
COALESCE(f3.DBWCode, '')
0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
You can use below..

ISNULL(f3.DBWCode, '')
0
 

Author Comment

by:Zack
Comment Utility
Heyas,

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

Thank you.
0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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 ]
0
 
LVL 9

Expert Comment

by:Karen
Comment Utility
hmm... hang on, do you mean
If this SET statement returns a NULL value
or
If this SELECT statement returns a NULL value
0
 

Author Comment

by:Zack
Comment Utility
Hi Snowberry,

Clarification: If this SET statement returns a NULL value.

Thank you.
0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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

0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
Or you can check like below..after SET,...

ISNULL ( Environment.Variables.flag3 , '' )
0
 
LVL 9

Expert Comment

by:Karen
Comment Utility
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

0
 

Author Comment

by:Zack
Comment Utility
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.
0
 

Author Comment

by:Zack
Comment Utility
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.
0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
So do you need records where f3.DBWcode IS NULL ?
0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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
0
 
LVL 9

Expert Comment

by:Karen
Comment Utility
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

0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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

0
 

Author Comment

by:Zack
Comment Utility
Hi Pawan,

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

Thank you.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 9

Expert Comment

by:Karen
Comment Utility
Do you want to know when  f3.DBWCode is NULL, or when Environment.Variables.flag3 is NULL ??????
0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
Didnt get can you explain via Pesudo code?
0
 

Author Comment

by:Zack
Comment Utility
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.
0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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

0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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))


0
 
LVL 9

Expert Comment

by:Karen
Comment Utility
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 ?
0
 

Author Comment

by:Zack
Comment Utility
Hi Snowflake,

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

Thank you.
0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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,'')))
0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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,'')))
0
 
LVL 9

Expert Comment

by:Karen
Comment Utility
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
0
 

Author Comment

by:Zack
Comment Utility
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.
0
 
LVL 9

Expert Comment

by:Karen
Comment Utility
are you saying you want to update the database????
your last comment does not make any sense
0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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.

0
 
LVL 9

Assisted Solution

by:Karen
Karen earned 250 total points
Comment Utility
Hopefully you are right Pawan!

By the way, since he is using ESQL I do not think ISNULL will work. But COALESCE should.
0
 
LVL 16

Accepted Solution

by:
Pawan Kumar Khowal earned 250 total points
Comment Utility
Ohhh yes thanks , great sir... Let me rewrite the query for him.

SET Environment.Variables.flag3 =

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

Author Closing Comment

by:Zack
Comment Utility
Thank you very much for the help guys.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now