Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

for my application

i want to insert a 10 digit number as SPLR000001

for this i have written

SELECT 'SPLR'+LEFT(CONVERT(VARCHAR(6),'000001'),6)

but i want to increment 000001 value to 1 like 000002
000003
,
,

how??
Posted
Comments
Ankur\m/ 11-Sep-12 6:30am    
Extract the numeric part, increment it and cast it again to varchar with specified length. Try and let us know if you have nay issue.

1. Fetch your Id(You can also create an Identity column and make use of it, so that avoid manual increment.
2. Genearate yr unique Id.

SQL
'SPLR' + RIGHT('00000' + CAST(Id AS NVARCHAR(10)),6)
 
Share this answer
 
Take a look at this thread (look at Solution 3). This may not be a solution to your problem but its a tip for you to consider.
Enter formated data in sql like 001,003,087,234[^]

You can generate unique consecutive numbers in SQL server using IDENTITY (Property)[^].

Here is a sample code
SQL
CREATE TABLE #SampleTable
(
	ID INT Identity(1,1),
	Column1 VARCHAR(50)
)

INSERT INTO #SampleTable
SELECT 'Row 1' UNION
SELECT 'Row 2' 

SELECT *, 'SPLR' + REPLICATE('0', 6 - LEN(ID)) + CAST(ID AS VARCHAR) FROM #SampleTable

DROP TABLE #SampleTable
 
Share this answer
 
v2
Hi,

try this.

SQL
DECLARE @ID INT = CAST(SUBSTRING('SPLR000001',5,10) AS INT)
SET @ID = @ID + 1
SELECT 'SPLR' + RIGHT(Replicate('0',6) + CAST(@ID AS VARCHAR),6)


Hope it works
 
Share this answer
 

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