Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #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



  #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?

  #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 ...

  #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
  #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

  #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

  #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
  #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..

  #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

  #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




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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.