How to add numeric values to an integer field in SQL

Posted on 2013-09-09
Medium Priority
Last Modified: 2013-09-18
The table I am managing has the following fields:

YearId int,
Year int

YearId is the primary key and Year is the real value that is holding the value of a numeric value.  So basically I wanted to start from 1900 and make the Year column go all the way to 2015.  How would I do that using only SQL?
Question by:VBBRett
LVL 41

Accepted Solution

Kyle Abrahams earned 668 total points
ID: 39477882
declare @i int
set @i = 1900

while @i <=2015
   insert into <table> (year) @i
   set @i = @i + 1

I'm assuming you have an identity on yearid as well.
LVL 12

Assisted Solution

Tony303 earned 668 total points
ID: 39478028
If the Id field isn't an identity, then we can add it to Ged's code above...
NOTE, I made a table called "Years"

declare @yr int
declare @id int
set @yr = 1900
set @id = 1

while @Yr <=2015 AND @id >= 1
   insert into years (id ,year) Values (@id ,@yr)
   set @yr = @yr + 1
   SET @id = @id + 1
LVL 35

Assisted Solution

by:David Todd
David Todd earned 664 total points
ID: 39478570

There isn't any reason to loop. Look up all the uses for a Tally table or Numbers table.

Okay, a little more work to set it up, but if the number of inserts is substantial, what's illustrated here is a lot quicker than a loop.

use ExpertsExchange

create table dbo.Number(    
	n int constraint pk_Number primary key clustered 

if object_id( N'tempdb..#digit', N'U' ) is not null 
	drop table #digit;
create table #digit(
	i int

insert #digit( i ) values ( 0 ), ( 1 ), ( 2 ), ( 3 ), ( 4 ), ( 5 ), ( 6 ), ( 7 ), ( 8 ), ( 9 )
insert dbo.Number( n )
		(( h.i * 10 ) + t.i ) * 10 + o.i
	from #digit o
	cross join #digit t
	cross join #digit h

--insert dbo.YourTableName( _year )
	1900 + N.n as _year
from dbo.Number N
	1900 + N.n <= 2015

Open in new window


Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article, we will show how to detach and attach a database and then show how to repair a corrupt database and attach it, If it has some errors. We will show how to detach and attach using SSMS or using T-SQL sentences.
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question