Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get next available number from table

Posted on 2009-05-05
14
Medium Priority
?
261 Views
Last Modified: 2013-12-24
I have an ID field that is actually a text field in the SQL table which contains an ID number for the records.  Some of the data looks like this:

myID
90456
TN945
AS903
TN946, etc.

I need to create a query to show the end user the next available ID number for the ID's starting in TN, for the ID starting with AS, and for the ID's that is all numbers.  When I try to do the top 1 in the query and then in my cfset statement try to add a 1 to get the next record, of course it is telling me 'The value "" cannot be converted to a number' since I am trying to add a number to a text field.  

How can I get these numbers?
<cfquery datasource="mydatasource" name="getIDs">
	SELECT top 1 myID
	FROM myTable
    WHERE (myID NOT LIKE 'TN%'
		and myID NOT LIKE 'AS%')
    order By myID desc
</cfquery>
 
<cfquery datasource="mydatasource" name="getTNIDs">
	SELECT myID
	FROM myTable
    WHERE myID LIKE 'TN%'
    order By myID desc
</cfquery>
 
<cfquery datasource="mydatasource" name="getASIDs">
	SELECT myID
	FROM myTable
    WHERE myID LIKE 'AS%'
    order By myID desc
</cfquery>
 
<cfoutput>
<cfset NextID = (#getIDs.myID# + 1)>
<cfset NextTNID = (#getTNIDs.myID# + 1)>
<cfset NextASID = (#getASIDs.myID# + 1)>
</cfoutput>

Open in new window

0
Comment
Question by:Lee R Liddick Jr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +1
14 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24306625

      SELECT top 1 cast(myID as int ) +1
      FROM myTable
    WHERE (myID NOT LIKE 'TN%'
            and myID NOT LIKE 'AS%')
    order By myID desc

 

      SELECT 'TN' + cast(   CAST( replace(myID,'TN', '' ) as int ) +1   as varchar )
      FROM myTable
    WHERE myID LIKE 'TN%'
    order By myID desc

 

      SELECT 'TN'  + CAST(  CAST( replace(myID,'AS', '' ) as int )  + 1   as varchar )
      FROM myTable
    WHERE myID LIKE 'AS%'
    order By myID desc

 






0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24306664
can't you use something like

SELECT substring(myID,1,2) + cast(convert(int,substring(myID,3,len(myID)-1))+1 as varchar)
        FROM myTable
    WHERE myID LIKE 'TN%'
    order By myID desc


it will give you incremented number itself
0
 

Author Comment

by:Lee R Liddick Jr
ID: 24306700
aneeshattingal...I am getting a CF error of 'incorrect syntax near the keyword 'as'.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 31

Expert Comment

by:RiteshShah
ID: 24306712
have you tried mine?
0
 

Author Comment

by:Lee R Liddick Jr
ID: 24306728
Ritesh...I already have established numbers in the tables, the end users want to be able to see what the next available number is.  Understanding that if two people do this at the same time, they may input the same number anyway.
0
 

Author Comment

by:Lee R Liddick Jr
ID: 24306773
Ritesh...not sure if it works or not.  It seems they also have a letter of 'a' or 'b' after some of the ID's that ended up to be duplicated.  So there are two like TN903a and TN903b.
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 24307018
After your last post, I have to ask, "How do you define 'the next number'?"
For insance, if the ID is 90210, it would seem fairly obvious that "the next number" is 90211.  However, is "the next number" for "TN903a" going to be "TN904a" or "TN903b" (i.e. do you only increment the numeric or do you increment the alpha ending)?  Also, if "TN903" exists, is "the next number" supposed to be "TN903a".
What about "TN999a"?  If you increment the numeric, is "the next number" going to be "TN000a", "TN1000a", "TN000", or "TN1000"?  
In other words, what is the Business Rule definition of "the next number" such that there are "oh, yeah, didn't I mention . . . ", "most of the time but sometimes . . . ", or other exceptions?  Without that definition, you are not going to have a chance of meeting the requirement.
Wouldn't it be a better overall design to have 3 columns: IDPrefix (NULLable) for the TN, AS or whatever; ID (INT); and an IDSuffix (NULLable) for the trailing alpha character?  
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24307250
Can u try those SQL statements in SSMS, i am not good in coldfusion, in case it works, you can convert them to cf statements
0
 

Author Comment

by:Lee R Liddick Jr
ID: 24307534
aneesh...I did run the statements in SQL and got the same error.

8080...the ID's with the letter at the end is an anomoly and should not be in the calculation of the next available number.  In retrospect, that would be an ideal situation; however, I am dealing with an already 3 year established application that we have converted from a 'sharepoint' list application to a web based coldfusion app.  If I originally built this, I would have had a prefix and an autonumber to fill in the next so there would not be a need for any suffix.  Unfortunately, I do not have that luxury at this point and have to work with what I have.

So in answer to your question, if the last number was TN922b the next number would be TN923.
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 1200 total points
ID: 24307718
I didnt find any error
USE tempdb
go 
 
create table MyTable ( MyID varchar(10) )
insert into MyTable select 100
insert into MyTable select 200
insert into MyTable select 'TN1001'
insert into MyTable select 'TN1003'
insert into MyTable select 'AS1101'
insert into MyTable select 'AS1191'
 
SELECT top 1 cast(myID as int ) +1
FROM myTable
WHERE (myID NOT LIKE 'TN%'
    and myID NOT LIKE 'AS%')
order By myID desc
 
 
SELECT top 1 'TN' + cast(   CAST( replace(myID,'TN', '' ) as int ) +1   as varchar ) 
FROM myTable
WHERE myID LIKE 'TN%'
order By myID desc
 
SELECT top 1 'AS'  + CAST(  CAST( replace(myID,'AS', '' ) as int )  + 1   as varchar ) 
FROM myTable
WHERE myID LIKE 'AS%'
order By myID desc

Open in new window

0
 

Author Comment

by:Lee R Liddick Jr
ID: 24307807
aneesh...I found what I did...I typo'd the cast to 'case'!
0
 

Author Closing Comment

by:Lee R Liddick Jr
ID: 31578175
This code was a little different than your first post...not sure if that was my problem as well, but it's all working now.  Excellent help.  Thanks again!
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 24308394
How about a UDF?
Call it with:

SELECT dbo.NextSeqNumber('TN100a') AS NextValue;

ALTER FUNCTION dbo.NextSeqNumber 
	(
	 @CurrentValue AS VarChar(10)
	)
RETURNS VarChar(10)
AS
BEGIN
	DECLARE	@TempInt		AS INT;
	DECLARE	@TempSeq		AS	INT;
	DECLARE	@TempValue	AS	VARCHAR(10);
	DECLARE	@TempPrefix	AS	VarChar(03);
	DECLARE	@TempSuffix	AS	VarChar(01);
	DECLARE	@HasSuffix	AS Char(01);
	DECLARE	@HasPrefix	AS	Char(01);
	
	SET	@TempSuffix	=	RIGHT(@CurrentValue, 1);
	SET	@TempInt	=	ISNUMERIC(@TempSuffix);
 
	IF	(@TempInt = 1)
	BEGIN
		SET	@HasSuffix	=	'N';
		SET	@TempValue	=	@CurrentValue;
		SET	@TempSuffix	=	'';
	END
	ELSE
	BEGIN
		SET	@HasSuffix	=	'Y';
		SET	@TempValue	=	LEFT(@CurrentValue, LEN(@CurrentValue) - 1);
	END
	
	SET	@TempPrefix	=	'';
	SET	@TempInt	=	ISNUMERIC(@TempValue);
	
	WHILE	(@TempInt = 0)
	BEGIN
		SET	@TempPrefix	=	@TempPrefix + LEFT(@TempValue, 1);
		SET	@TempValue	=	RIGHT(@TempValue, LEN(@TempValue) - 1);
		SET	@TempInt	=	ISNUMERIC(@TempValue);
	END
	
	SET	@TempSeq	=	CAST(@TempValue	As	INT) + 1;
	SET	@TempValue	=	@TempPrefix	+	CAST(@TempSeq	AS	VarChar(07))	+	@TempSuffix;
	
	
	RETURN @TempValue
END

Open in new window

0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 24344431
Just a passing curiosity but, you do realize that the selected solution is going to fail whenever it encounters a trailing aplha character, don't you? ;-)
0

Featured Post

Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

598 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