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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 T-SQL Query Help

Author  Topic 

Sonu619
Posting Yak Master

202 Posts

Posted - 2011-09-22 : 12:00:11
Hi guys,

Here is my table structure
ID,FNAME, LNAME, CODE
1,JIM,SMITH,BASE
2,CARLOS,B,OUT
1,CHRIS,UR,BASE
2,NOR,SLI,QUI
My requirement is or I want end result like listed below
ID,FNAME,LNAME,CODE
1,JIM,SMITH,BASE
2,CARLOS,B,OUT
1-A,CHRIS,UR,BASE
2,NOR,SLI,QUI
And I am using this query
;WITH cte1 AS
(
SELECT ID, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY (SELECT NULL)) - 1 AS rn
FROM Table_Name
)

update cte1 set ID =
ID + CASE WHEN rn > 0 THEN + CHAR(ASCII('A')+rn-1) ELSE '' END
After I run above query giving me this result
ID,FNAME,LNAME,CODE
1,JIM,SMITH,BASE
2,CARLOS,B,OUT
1-A,CHRIS,UR,BASE
2-A,NOR,SLI,QUI
Note:- But I want to implement this query on where Code = ‘BASE. I tried to use WHERE clause here and there but no luck, Any advice would be big help. Thanks.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-22 : 12:05:03
so if there's one more instance you append B,C etc?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Sonu619
Posting Yak Master

202 Posts

Posted - 2011-09-22 : 12:09:31
Want to give you update, I got it, Below is the syntax,

;WITH cte1 AS
(
SELECT ID, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY (SELECT NULL)) - 1 AS rn
FROM Table_Name Where CODE = 'BASE'
)

update cte1 set ID =
ID + CASE WHEN rn > 0 THEN + CHAR(ASCII('A')+rn-1) ELSE '' END

Thanks.
Go to Top of Page
   

- Advertisement -