Posted on September 7, 2008 by codeslammer
Although my previous post was a bit hasty, I wanted to follow up with some additional comments about the magic in the middle of the UDF created to remove any non-numeric characters from a column in a query result set. As you can see there is a relatively cryptic body of the function that somewhat [...]
Filed under: Data, SQL Server | 1 Comment »
Posted on September 2, 2008 by codeslammer
I recently had a need to get a distinct list of numbers from a freeform text field in a database. After some analysis, my needs were met by the following approach:
CREATE FUNCTION dbo.UFNG_ONLY_DIGITS (@StrVal AS VARCHAR(max))
RETURNS VARCHAR(max)
AS
BEGIN
WHILE PATINDEX(‘%[^0-9]%’, @StrVal) > 0
SET @StrVal = REPLACE(@StrVal,
SUBSTRING(@StrVal,PATINDEX(‘%[^0-9]%’, @StrVal),1),”)
RETURN @StrVal
END
The function can then be used [...]
Filed under: Data, SQL Server | 1 Comment »