Solved

SQL select statement

Posted on 2013-06-24
5
576 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

685 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