Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with creating a new ID

Author  Topic 

maevr
Posting Yak Master

169 Posts

Posted - 2010-12-21 : 16:31:06
I need to create a new ID based on the criteria below:
myNewID should be based on myType, myYear and a seed number based on how many occurances there is of the specific sort.
IF myType is S then the myNewID should start with 'S' then check the myYear to set the next character and then a seed number.
myYear < 1960 = 1
myYear between 1960 and 1980 = 2
myYear > 1980 = 3

create table #table1(
myOldID varchar(20) not null
,myNewID varchar(10) null
,myType varchar(1) not null
,myYear int not null
)

insert into #table1(myOldID, myType, myYear) values('1080100', 'S', 1950)
insert into #table1(myOldID, myType, myYear) values('4888383', 'S', 1958)
insert into #table1(myOldID, myType, myYear) values('1191717', 'S', 1974)
insert into #table1(myOldID, myType, myYear) values('1080200', 'L', 1968)
insert into #table1(myOldID, myType, myYear) values('1031234', 'L', 1999)
insert into #table1(myOldID, myType, myYear) values('1997551', 'F', 2004)
insert into #table1(myOldID, myType, myYear) values('2000876', 'F', 2009)
insert into #table1(myOldID, myType, myYear) values('1876444', 'F', 1982)

Expected output:
1080100, S1001, S, 1950)
4888383, S1002, S, 1958)
1191717, S2001, S, 1974)
1080200, L2001, L, 1968)
1031234, L3001, L, 1999)
1997551, F3001, F, 2004)
2000876, F3002, F, 2009)
1876444, F3003, F, 1982)

X002548
Not Just a Number

15586 Posts

Posted - 2010-12-21 : 16:46:06
..needs some work


SELECT myType + CONVERT(char(1),
CASE WHEN myYear < = 1960 THEN 1
WHEN myYear > 1960 AND myYear < = 1980 THEN 2
WHEN myYear > 1980 THEN 3
ELSE 0
END), ROW_NUMBER() OVER(ORDER BY myYear)
FROM #table1



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

maevr
Posting Yak Master

169 Posts

Posted - 2010-12-21 : 17:03:35
Hi, thanks for the fast reply!

Your code looks like the one I wrote before I got stuck.
the seed number must begin from 1 for every new occurance

S1001
S1002
S1003
L2001
L3001
F3001
F3002
.
.
.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-12-21 : 17:40:33
Try making it a derived table, then find the MIN and use it to subtract itself -1 for each number in the range



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -