SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Adding alphabet to a number to make it unique


Adding alphabet to a number to make it unique

Author
Message
gavva.sravan
gavva.sravan
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 6
create table #temp_Alpha_num (
[uniquenum] [int] Not NULL,
[Somenum] [int] Not NULL,
)

Insert into #temp_Alpha_num(uniquenum,Somenum)
values
( 1 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values
( 2 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 3 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 4 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 5 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 6 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 7 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values( 8 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 9 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 10 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 11 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 12 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 13 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 14 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 15 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 16 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 17 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 18 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 19 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 20 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 21 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 22 , 121)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 23 , 101)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 24 , 101)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 25 , 101)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 26 , 101)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 27 , 101)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 28 , 101)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 29 , 101)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 30 , 101)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 31 , 101)
Insert into #temp_Alpha_num(uniquenum,Somenum)
values ( 32 , 101)

for the somenum column I need to add alphabets to make them unique. for example 121a,121b,121c....121z,121aa,121ab,101a and so on...


Could you guys please help. I dont know where to start..
pietlinden
pietlinden
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14500 Visits: 14165
You mean something like this?:

SELECT UniqueNum
,Somenum
, CHAR(ROW_NUMBER() OVER (PARTITION BY SomeNum ORDER BY UniqueNum ASC)+64) AS LetterSuffix
FROM #temp_Alpha_num
ORDER BY uniquenum,Somenum ASC;



so that when you get 121A to 121F and then maybe 101A to 101X or similar?
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18443 Visits: 6431
What happens when you get to 4 letter combinations?

Could get nasty! :-P


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
gavva.sravan
gavva.sravan
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 6
once the somenum exceeds the alphabet range it should be aa ab ac ad .but this code gives me other characters ([,],$,^,(,),)..
Sean Lange
Sean Lange
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64940 Visits: 17979
gavva.sravan (11/26/2013)
once the somenum exceeds the alphabet range it should be aa ab ac ad .but this code gives me other characters ([,],$,^,(,),)..


That is because this is a bad idea. Once you start "auto incrementing" characters you open up a whole possibility of bad things. What happens when you start getting into 3 and 4 letter words?

You already have a unique value per row. Why can't you use that? Or if you really need to use somenum why not cast it to a varchar add an underscore and then use row_number?

Something like this maybe?


select uniquenum, Somenum, CAST(Somenum varchar(10)) + '_' + cast(Suffix as varchar(5)) as SomeNum_PlusRowNum
from
(
SELECT UniqueNum
,Somenum
, ROW_NUMBER() OVER (PARTITION BY SomeNum ORDER BY UniqueNum ASC) AS Suffix
FROM #temp_Alpha_num
)
ORDER BY uniquenum, Somenum ASC;



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search