Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 6th, 2005, 10:53 AM
Authorized User
 
Join Date: May 2005
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default PLS HELP ME ..STRING CONCATINATION

Hi
        I am facing a problem in string concatination in sql server...i am taking 5 values from 5 diff columns...and i have to check if its not null then concatinate with "|"...

        select pgcd1 + "|"+ pgcd2 +"|" +pgcd3 + "|"+ pgcd4 as PGCD FROM TABLENAME...

         The problem i am facing is ...the "|" symbol should comes only in pictures if the field is having any value...

       desired output 1)- if ALL 3 fields r having values then

            pgcd1 | pgcd2 | pgcd3

 2) if first and last having values and second is null then

            pgcd1 | pgcd3

 3) IF FIRST IS NULL AND SECOND AND THIRD COLUMNS R HAVING VALUE THEN

            PGCD2 | PGCD3






   Can anybody help me....thx



Reply With Quote
  #2 (permalink)  
Old May 6th, 2005, 12:43 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Can you give more explination on why you want to set up your query this way?

Reply With Quote
  #3 (permalink)  
Old May 6th, 2005, 12:49 PM
Authorized User
 
Join Date: May 2005
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

actually ..thats the user requiremnt...They want the combination of the 5 columns in one column. ..all the five columns r coming from diff tables....and i have to join it using "|" symbol in one field ...

Reply With Quote
  #4 (permalink)  
Old May 6th, 2005, 12:55 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there..

you should depure this.. but thinking something like

SELECT
iif(isnull(pgcd1),"",pgcd1+"|")+... etc

i dont remember exact function for sql server.. but after that you will have to take always the last "|" ..



HTH

Gonzalo
Reply With Quote
  #5 (permalink)  
Old May 6th, 2005, 01:15 PM
Authorized User
 
Join Date: May 2005
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

select if(isnull (pgmcd1),"",pgmcd1,"|") as pgmcd from Temp_DISPromo_GRP1

not working...i guess something wrong in syntax

Reply With Quote
  #6 (permalink)  
Old May 6th, 2005, 01:20 PM
Friend of Wrox
 
Join Date: Jan 2004
Location: , , USA.
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

SELECT CASE
       WHEN (column1 IS NOT NULL) THEN column1 + space(1) + '|' + space(1) ELSE '' END +
       CASE
       WHEN (Column2 IS NOT NULL) THEN column2 + space(1) + '|' + space(1) ELSE '' END +
       CASE
       WHEN (Column3 IS NOT NULL) THEN column3 + space(1) + '|'+ space(1) ELSE '' END
From MyTable

Reply With Quote
  #7 (permalink)  
Old May 6th, 2005, 01:42 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

mmm Chirag you still need the substr to get out the last 2 chars b/c if column3 is null you will end with something like col1 + col2 + " | " ..

im ok????

HTH

Gonzalo
Reply With Quote
  #8 (permalink)  
Old May 6th, 2005, 01:50 PM
Authorized User
 
Join Date: May 2005
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ya...still i am struggling to get the desired results..

Reply With Quote
  #9 (permalink)  
Old May 6th, 2005, 01:56 PM
Authorized User
 
Join Date: May 2005
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

SELECT

       CASE
          WHEN (pgmcd1 IS NOT NULL) THEN pgmcd1 + space(1) + '|' + space(1) ELSE '' END +
       CASE
          WHEN (pgmcd2 IS NOT NULL) THEN pgmcd2 + space(1) + '|' ELSE '' END +
       CASE
          WHEN (pgmcd3 IS NOT NULL) THEN pgmcd3 + space(1) + '|' ELSE '' END +
        CASE
          WHEN (pgmcd4 IS NOT NULL) THEN pgmcd4 + space(1) + '|' ELSE '' END +
        CASE
          WHEN (pgmcd5 IS NOT NULL) THEN pgmcd5 + space(1) + '|' ELSE ''
       END
as pgmcd from Temp_DISPromo_GRP1



STILL I AM GETING THE RESULT LIKE..


================================================== =======

  | | | | |

A | |C | | E |

  |B | C | | E |





SOME OTHER SYNTAX I GUESS

Reply With Quote
  #10 (permalink)  
Old May 6th, 2005, 02:11 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Naperville, IL, USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this sample:

Code:
DECLARE @s1 varchar(10)
DECLARE @s2 varchar(10)
DECLARE @s3 varchar(10)
DECLARE @s4 varchar(30)

SET @s1 = NULL
SET @s2 = 'two'
SET @s3 = 'three'

SET @s4 = '|' + isnull(@s1 + '|', '') + isnull(@s2 + '|', '') + isnull(@s3 + '|', '')
Print @s4
Rand
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help pls with connection string arian29 VB Databases Basics 10 August 29th, 2008 11:17 PM
I need help!!!!!!!!!!!!!!!!!!!!!!!!!!!! pls!!!!!!! ironchef Java Basics 2 September 26th, 2006 02:35 PM
Pls help me!! hoailing22 ASP.NET 1.0 and 1.1 Basics 4 June 10th, 2005 11:24 PM
Concatination BSkelding MySQL 1 August 18th, 2004 09:47 PM
Concatination kilika SQL Server 2000 1 August 26th, 2003 10:44 AM



All times are GMT -4. The time now is 07:25 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.