Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
s_anr
Yak Posting Veteran
81 Posts |
Posted - 2009-08-27 : 03:28:53
|
| Hi I have two tablesTable1======================id name class01 John one02 mary two10 peter fiveTable2=======================doc_id doc_rel doc_name01 1 TOP02 1-400001 D03 1-400001-400002 C04 1-400001-400138-400139 A05 1-400013 E06 1-400013-400014 B07 1-400013-400144 J08 1-400023 G09 1-400023-400024 K10 1-400023-400024-400150 I11 1-400023-400025 A12 1-400026 H13 1-400026-400027 FI run the follwoing query :SELECT a.id, a.name, b.rel_idfrom table1 a left outer join table2 b on a.id = b.doc_idOUTPUT is given below :id name doc_id01 john 102 mary 1-40000110 peter 1-4000023-4000024-400150I would like the output to be shown asid name doc_id01 john TOP02 mary 1-D10 peter 1-G-K-IIf anyone can help that wud be great.. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-08-27 : 09:44:06
|
| [code]SELECT t1.id,t1.name,LEFT(dl.docrellist,LEN(dl.docrellist)-1)FROM Table1 t1INNER JOIN Table2 t2ON t2.doc_id=t1.idCROSS APPLY (SELECT doc_name + '-' FROM Table2 WHERE SUBSTRING(t2.doc_rel,2,LEN(t2.doc_rel)-1) + '-' LIKE SUBSTRING(doc_rel,2,LEN(doc_rel)-1) + '-%' FOR XML PATH(''))dl(docrellist)[/code] |
 |
|
|
s_anr
Yak Posting Veteran
81 Posts |
Posted - 2009-08-27 : 23:52:48
|
That was bullseye Viksah. Thank you much. There is just one minot problem in the output If the doc_name has an ampersand "&"e.g. if doc name is "You & Me"The output is shown as"You & amp; Me" |
 |
|
|
s_anr
Yak Posting Veteran
81 Posts |
Posted - 2009-08-28 : 03:41:14
|
| I Figured it out. Done a replace of characters. Its working great now. Thanks !!! |
 |
|
|
|
|
|