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 |
|
nn
Starting Member
2 Posts |
Posted - 2008-10-15 : 22:35:06
|
| Hi,I have a table with 10 columns. There is an ID column which is 17 characters long (eg., '000763-P06-LAWNBL' ). There is a description column which is a varchar(30) datatype.I need to sort the rows by the ID column and then the Description. If there is more than one of the same description for the a particular ID, I need to append the value in both the ID and the description field by a 01, 02 and 03, and so on.How do I code this in T-SQL? Sorry, but I need this urgently. |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2008-10-15 : 23:15:17
|
| Can you give sample data and expected output? |
 |
|
|
nn
Starting Member
2 Posts |
Posted - 2008-10-15 : 23:58:40
|
| Sorry this is not formatted. InputId====003718-P01-SIDEWK-SWK003478-P01-PATHWY-PTH003478-P01-SSOCCE-SOC003478-P01-SSOCCE-SOC003479-P01-PATHWY-PTHDesc========Sidewalk #Pathway #Sport Field-Soccer #Sport Field-Soccer #Pathway #output------Id----003738-P01-PATHWY-PTH001004588-P01-PATHWY-PTH001005850-P01-NTAREA-NAT001005850-P01-PICNIC-PCN001005854-P01-TURFS-TUR001005985-P01-SIDEWK-SWK001006008-P01-TURFS-TUR001006068-P01-SFBASB-BSB001006078-P01-PATHWY-PTH001006149-P01-NTAREA-NAT001006153-P01-NTAREA-NAT001006159-P01-PATHWY-PTH001006159-P01-SFBASB-BSB001006162-P01-SSOCCE-SOC001006162-P01-NTAREA-NAT001Desc------Pathway #01Pathway #01Naturalized Area #01Picnic Area #01Turf #01Sidewalk #01Turf #01Sport Field-Baseball #01Pathway #01Naturalized Area #01Naturalized Area #01Pathway #01Sport Field-Baseball #01Sport Field-Soccer #01Naturalized Area #01Pathway #01Pathway #01Pathway #01Naturalized Area #01Sport Field-Soccer #01Lawn Bowling #01Picnic Area #01Naturalized Area #01Sport Field-Football #01Sport Field-Football #02 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-16 : 02:29:58
|
| [code]SELECT id,Desc+ CAST(RIGHT('0'+COALESCE(tmp.Total,0),2) AS varchar(5))FROM YourTable tOUTER APPLY (SELECT COUNT(*) AS Total FROM YourTable WHERE Desc=t.Desc AND LEFT(id,CHARINDEX('-',id)-1)=LEFT(t.id,CHARINDEX('-',t.id)-1))tmp[/code] |
 |
|
|
|
|
|
|
|