Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alphabet Series Generation in SQL Server

I am trying to generate a series of alphabets, using SQL Server.

Suppose I have a tables as follows

DECLARE @Funding TABLE (FundingDetailID INT,FundingID INT, DetailDescription VARCHAR(50))
INSERT INTO @Funding (FundingDetailID ,FundingID , DetailDescription) VALUES (1,107,'Desc 1')
INSERT INTO @Funding (FundingDetailID ,FundingID , DetailDescription) VALUES (1,107,'Desc 2')
INSERT INTO @Funding (FundingDetailID ,FundingID , DetailDescription) VALUES (1,107,'Desc 3')

I am trying to obtain the following result.

a) Desc 1
b) Desc 2
c) Desc 3

How do I generate “a)”, “b)”, … ? I am not allowed to add any extra temp table or table variable for storing the alphabets initially. They should be generated.

And this is to be done in SQL Server 2005.

Any thoughts ?

Thanks

Lijo Cheeran Joseph

like image 514
Lijo Avatar asked Dec 06 '25 00:12

Lijo


1 Answers

Use ROW_NUMBER() as follows

DECLARE @Funding TABLE (FundingDetailID INT,FundingID INT, DetailDescription VARCHAR(50))
INSERT INTO @Funding VALUES (1,107,'Desc 1')
INSERT INTO @Funding VALUES (1,107,'Desc 2')
INSERT INTO @Funding VALUES (1,107,'Desc 3')

SELECT CHAR (CAST (96+ROW_NUMBER() OVER (Order BY FundingDetailID) AS VARCHAR)) + ') ' + DetailDescription
FROM @Funding



-----------------------------------------------------
a) Desc 1
b) Desc 2
c) Desc 3
like image 89
Raj More Avatar answered Dec 07 '25 15:12

Raj More



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!