Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

** Stored Procedure to clean out Letters, Commas, Periods, and $ **

Status
Not open for further replies.

jonnywah

Programmer
Feb 7, 2004
80
US
I need help writing a Stored Procedure that cleans out Letters, Commas, Periods, and $ (dollar signs) in the price field (database type: varchar) of a Product table.

The price field is in the Product table, where the primary key is ProductId. I am new to this. Please help. Thank you in advance.
 
Why do you have all these "garbage" characters in the price field to begin with. This feild should be set up as a money/smallmoney, Integer or possibly a decimal datatype. If you plan on using this field in calculation like sales, ROI ... you are in for some serious headaches in converting this varchar into a usable datatype. In saying this, you can remove the unwanted characters by using a REPLACE command as in ....

DECLARE @TestVarChar VarChar(10)

SET @TestVarChar = '$,123.00'

SET @TestVarChar = REPLACE(REPLACE(REPLACE(@TestVarChar,'$',''),',',''),'.','')





Thanks

J. Kusch
 
JayKusch,

How can I use this code in a stored procedure? I want to loop through each price record, clean out periods, commas, and $, then print out the productId of the record that was cleaned???


Thanks,
JonnyWah
 
It would be a 2 pass Stored Procedure (SP). this first part would give you the ProductIDs that will be cleaned and then the second part would actually do the clean up. This is a snippette of what the code may look like ...

Code:
Create Procedure usp_CleanIt

  AS
SELECT ProductID
FROM ProductTable
WHERE PRICE LIKE '%.%' OR '%$%' OR '%,%' OR -- ... rest of items needing to be removed  

UPDATE ProductTable
SET Price = REPLACE(REPLACE(REPLACE(@TestVarChar,'$',''),',',''),'.','')
WHERE PRICE LIKE '%.%' OR '%$%' OR '%,%' OR -- ... rest of 
items needing to be removed

Thanks

J. Kusch
 
JayKusch,

I appreciate your help. Thank you!



-Jonnywah
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top