Solved

SQL select statement

Posted on 2013-06-24
5
560 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you so much!!!
0
 
LVL 16

Expert Comment

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

Expert Comment

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

763 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

13 Experts available now in Live!

Get 1:1 Help Now