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

May 6th, 2005, 10:53 AM
|
|
Authorized User
|
|
Join Date: May 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 6th, 2005, 12:43 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
Can you give more explination on why you want to set up your query this way?
|
|

May 6th, 2005, 12:49 PM
|
|
Authorized User
|
|
Join Date: May 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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 ...
|
|

May 6th, 2005, 12:55 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
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
|
|

May 6th, 2005, 01:15 PM
|
|
Authorized User
|
|
Join Date: May 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
select if(isnull (pgmcd1),"",pgmcd1,"|") as pgmcd from Temp_DISPromo_GRP1
not working...i guess something wrong in syntax
|
|

May 6th, 2005, 01:20 PM
|
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 6th, 2005, 01:42 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
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
|
|

May 6th, 2005, 01:50 PM
|
|
Authorized User
|
|
Join Date: May 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
ya...still i am struggling to get the desired results..
|
|

May 6th, 2005, 01:56 PM
|
|
Authorized User
|
|
Join Date: May 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 6th, 2005, 02:11 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |