User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 391,952 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,836 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser:
Views: 10779 | Replies: 4
Reply
Join Date: Nov 2005
Posts: 3
Reputation: Dorothy.Maseko is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Dorothy.Maseko Dorothy.Maseko is offline Offline
Newbie Poster

Converting a string to number

  #1  
Nov 30th, 2005
I have a value E101 as my empId in my employees table and now i need to increment this value, i.e. E102, E103...etc. How can i get this to work? I've used the procedure but it's not working what am i doing wrong?

ALTER PROCEDURE CalcPrimaryKey
AS
DECLARE @numValue int, @charOldValue varchar(4), @charNewValue varchar(4)
BEGIN
SELECT @numValue = MAX(CAST(SUBSTRING(CustID, 2, 3)AS Integer))FROM Customer
@charOldValue = CustId
SET @numValue = @numValue + 1


SET @charNewValue = LEFT(CustID,1) + RIGHT('000' + CAST(@numValue AS varchar,3))


GO


and i get this error
Server: Msg 170, Level 15, State 1, Procedure CalcPrimaryKey, Line 6
Line 6: Incorrect syntax near '@charOldValue'.
Server: Msg 170, Level 15, State 1, Procedure CalcPrimaryKey, Line 10
Line 10: Incorrect syntax near ','.

Help needed urgently, Please
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Location: Dallas, TX
Posts: 481
Reputation: campkev is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Converting a string to number

  #2  
Nov 30th, 2005
Select 'e' + cast(cast(substring(max(CustID),2,3) as int)+1 as varchar) from customer
Reply With Quote  
Join Date: Jul 2005
Location: Dallas, TX
Posts: 481
Reputation: campkev is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Converting a string to number

  #3  
Nov 30th, 2005
of course, this will cause problems once you hit user e999 so I would do this

declare @length int
select @length =  len(max(CustID)) from customer 
Select 'e' + cast(cast(substring(max(CustID),2,@length-1) as int)+1 as varchar) from  customer 
Reply With Quote  
Join Date: Nov 2005
Posts: 3
Reputation: Dorothy.Maseko is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Dorothy.Maseko Dorothy.Maseko is offline Offline
Newbie Poster

Re: Converting a string to number

  #4  
Dec 2nd, 2005
Thanx Campkev that helped a lot, u r a *
Reply With Quote  
Join Date: Jul 2005
Location: Dallas, TX
Posts: 481
Reputation: campkev is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Converting a string to number

  #5  
Dec 2nd, 2005
no problem
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MS SQL Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 9:05 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC