Solved

SQL select statement

Posted on 2013-06-24
5
581 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 27

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 27

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

695 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