Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to store data in sql as following format

0001
0002
0003
0004
.
.
.
.
9999

my table is

create table tblAssest (code nvarchar(4))

waiting for a positive answer........
Posted

Here is one approach
SQL
--create table tblAssest (code nvarchar(4))

DECLARE @InitialCount INT
DECLARE @FinalCount INT

SET @InitialCount = 1
SET @FinalCount = 9999

WHILE  @InitialCount <= @FinalCount
BEGIN

    INSERT INTO tblAssest
    SELECT RIGHT('000' + CAST(@InitialCount AS VARCHAR),4)

    SET @InitialCount = @InitialCount + 1

END

SELECT * FROM tblAssest

--DROP TABLE tblAssest
 
Share this answer
 
A bit different answer. If you're actually storing numbers, don't format them in a table, never! Instead store them as numbers and handle all the formatting requirements on the client side.

The database is meant to store... well the data. Not the formatted values. The best results are achieved when the data is in it's natural form and when the data is represented to the user it's formatted properly using sufficient information about the client.
 
Share this answer
 
Comments
__TR__ 31-Aug-12 16:23pm    
Even though its not what the OP asked for, I think its a great tip. Thanks for the answer. My 5!
Wendelius 31-Aug-12 16:26pm    
Thanks TR :)
 
Share this answer
 
v2

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