Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have two columns. sno as primary key and character in the second column. i'd like to insert a int the 1st column. but while inserting 2nd date it should insert automatically B in to that 2nd row of character column and C in 3rd and D in 4th and so on.. Is there any way?
Posted
Comments
Kuthuparakkal 8-Aug-13 1:40am    
Post sample output
ARUN K P 8-Aug-13 1:57am    
how to attach a file in this? i've taken a screen shot but there is no option of attaching a file here.
ARUN K P 8-Aug-13 1:58am    
sno character
1 a
2 b
3 c

tats how my output should be
Kuthuparakkal 8-Aug-13 3:08am    
See the solution posted....
Status BreakPoint 8-Aug-13 1:40am    
Which database do you use?

1 solution

Use the following function


SQL
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Kuthuparakkal
-- Create date: 2013-08-08
-- Description:	Map Number to Letter
-- =============================================
CREATE FUNCTION dbo.fn_GetCharacter
(
	@SNo INT
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
	-- Declare the return variable here
	DECLARE @Divident INT = @SNo
	DECLARE @Character NVARCHAR(MAX)=''
	DECLARE @Modulo INT

	-- Add the T-SQL statements to compute the return value here
	WHILE(@Divident > 0)
		BEGIN
			 SET @Modulo = (@Divident - 1) % 26;
			 SET @Character = CHAR(65 + @Modulo) + @Character
			 SET @Divident = ((@Divident - @Modulo) / 26);
		END

	-- Return the result of the function
	RETURN @Character

END
GO


Now Create your table:
SQL
 CREATE TABLE dbo.tbl_myTable
(
 [SNo] INT IDENTITY(1,1)
,[Character] NVARCHAR(MAX)
)


Now insert like this:
SQL
INSERT tbl_myTable DEFAULT VALUES
UPDATE tbl_myTable SET [CHARACTER] = dbo.fn_GetCharacter(SCOPE_IDENTITY())
WHERE SNo = SCOPE_IDENTITY()
 
Share this answer
 
Comments
Manas Bhardwaj 8-Aug-13 3:08am    
Good one +5!
Kuthuparakkal 8-Aug-13 3:11am    
Thanks Manas!
ARUN K P 8-Aug-13 3:10am    
ok thanks kuthuparakkal.i ll try this and get back.
Kuthuparakkal 8-Aug-13 3:11am    
No Probs!
ARUN K P 8-Aug-13 3:15am    
yeah it s working fine thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900