Computing.Net > Forums > Programming > extracting number from a string SQL

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

extracting number from a string SQL

Reply to Message Icon

Name: mo
Date: October 14, 2002 at 05:57:46 Pacific
OS: 2000
CPU/Ram: P111 / 256
Comment:

hi all

i need to pull numbers from a string (comments) and if i get 13 numbers then
populate another field, this is to pull
a possible barcode out of a comments field.

i've got a working solution, but would like to know if there is a neater solution,

Using SQL 7, in 2000 i would be able to have
a user defined funtion to handle the string without the cursor,

any input appreciated

cheers mo

CREATE PROCEDURE spxPPGenerateBarCodes AS

DECLARE @Comments varchar(50)

/* get a list of the comments that were
imported today by spxPPImportNewPartNumber
we can tell these by 'x-' */
DECLARE c1 CURSOR FOR
SELECT Comments
FROM tblItemMaster
WHERE BarCode = 'x-'
AND Comments IS NOT NULL

OPEN c1
/* start the loop through table */
FETCH NEXT FROM c1
INTO @Comments

WHILE @@FETCH_STATUS = 0
BEGIN

/* we count the length of the comment to
set the lenghth of the loop through to
extract numeric's */
DECLARE @COUNTER integer
SELECT @COUNTER = 1
WHILE (@COUNTER < (LEN(@Comments)+1))
BEGIN
BEGIN
IF ISNUMERIC(SUBSTRING(@Comments,@counter,1)) = 1
/* if we get a numeric we throw
it in the BarCode field */
BEGIN
UPDATE tblItemMaster
SET BarCode = (BarCode + (LTRIM(SUBSTRING(@Comments,@counter,1))))
WHERE Comments = @Comments
END
END
/* increment counter */
SELECT @COUNTER = @COUNTER +1
END
/* keep the loop going */
FETCH NEXT FROM c1
INTO @Comments

END
/* close and clear the cursor */
CLOSE c1
DEALLOCATE c1

/* we now extract our barcode */
BEGIN
UPDATE tblItemMaster
SET BarCode = 'NEW' + RIGHT(BarCode,13)
WHERE LEN(Barcode) = 15
AND LEFT(BarCode,2) = 'x-'
END

/* and set the leftovers back to NULL */
BEGIN
UPDATE tblItemMaster
SET BarCode = NULL
WHERE LEFT(BarCode,2) = 'x-'
END

RETURN



Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Zinc Application Framewor... close button, property sh...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: extracting number from a string SQL

Trying to extract characters from a string va www.computing.net/answers/programming/trying-to-extract-characters-from-a-string-va/19040.html

For and extracting nums from text www.computing.net/answers/programming/for-and-extracting-nums-from-text/11600.html

VB6 Opening a Form from a string in code www.computing.net/answers/programming/vb6-opening-a-form-from-a-string-in-code/2774.html