SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 List by Running Number
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

hayashiryo
Starting Member

30 Posts

Posted - 10/02/2013 :  22:46:27  Show Profile  Reply with Quote
Hi all.

I need help with a query.

I trying to generate a list of running numbers from a table. However the field is in varchar format and there may be missing numbers inbetween. Please see the example below


EMP-TABLE
Emp#	Name
E1003	Matthew
E1004	Mark
E1007	Luke
E1009	John


Query I need to formulate.
I will input 1001 and 1010 as parameter.

Emp#	AssignedTo
1001
1002
1003	Mattherw
1004	Mark
1005	
1006	
1007	Luke
1008	
1009	John
1010


Thanks in advance.

khtan
In (Som, Ni, Yak)

Singapore
17438 Posts

Posted - 10/02/2013 :  23:44:48  Show Profile  Reply with Quote
declare	@emp_start	int,
	@emp_end	int

select	@emp_start = 1001,
	@emp_end = 1010

; with emp_no as
(
	select	no = @emp_start, emp_no = 'E' + convert(varchar(4), @emp_start)
	union all
	select	no = no + 1, emp_no = 'E' + convert(varchar(4), no + 1)
	from	emp_no
	where	no < @emp_end
)
select	n.no, AssignedTo = e.emp_name
from	emp_no n
	left join emp e	on	n.emp_no = e.emp_no



KH
Time is always against us

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000