Solved

SQL select statement

Posted on 2013-06-24
5
571 Views
Last Modified: 2013-06-26
I am trying to build a transact-SQL statment that I can paste into an xml document.
This Transact-SQL statement gives me what I want:

Select
'<wp:comment_author>'+name+'</wp:comment_author>'+  CHAR(13)+CHAR(10) +
'<wp:comment_author_IP>'+source_ip+'</wp:comment_author_IP>'+  CHAR(13)+CHAR(10)

Result:
 <wp:comment_author>john Doe</wp:comment_author>  <wp:comment_author_IP>170.12.14.133</wp:comment_author_IP>  

However when the source_IP field is null all I get returned is
NULL
I would like to get the output below: (so if source_ip is null it will list out everything else, with nothing at all as the value where the source_ip normally goes.
Below is what I want:
 <wp:comment_author>john Doe</wp:comment_author>  
<wp:comment_author_IP></wp:comment_author_IP>
0
Comment
Question by:cbeverly
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39271474
you have to do this

Select
'<wp:comment_author>'+name+'</wp:comment_author>'+  CHAR(13)+CHAR(10) +
'<wp:comment_author_IP>'+isnull(source_ip,'')+'</wp:comment_author_IP>'+  CHAR(13)+CHAR(10) 

Open in new window

0
 
LVL 26

Accepted Solution

by:
Zberteoc earned 500 total points
ID: 39274761
When you build text from values of SQL variables or columns you will always have to wrap all the columns in ISNULL(col/variable, '<null_replacement_value>') especially when you concatenate. NULL involved in any oeration will result in NULL. SO just in case you should have the name wrapped in ISNULL as well:

Select
'<wp:comment_author>'+ISNULL(name,'')+'</wp:comment_author>'+  CHAR(13)+CHAR(10) +
'<wp:comment_author_IP>'+isnull(source_ip,'')+'</wp:comment_author_IP>'+  CHAR(13)+CHAR(10) 

Open in new window

0
 

Author Closing Comment

by:cbeverly
ID: 39277908
Thank you so much!!!
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39278796
Hey I posted the same solution a day before....
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 39278988
I agree, however is one thing to drop a solution "as is" and another thing to to give the solution along with some explanations of why and what. Split points could have worked though.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

816 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