I\'m still learning my ropes with SQL Server and maybe this question sounds very naive/ridiculous. Please bear with me on this. :)
I saw a function in SQL Server defined
Replace accepts the table column, and in this case it will do exactly what you expect it to do. It will do iterations on @InputString variable and exclude all non numeric characters that appear in the NonNumeric table and in the @InputString variable.
This is a hacky trick called Quirky Update
This is slow and in most cases something one should avoid
Some examples:
DECLARE @tbl TABLE(SomeInt INT);
INSERT INTO @tbl VALUES (1),(2),(3);
DECLARE @SumOfInts INT=0
SELECT @SumOfInts = @SumOfInts + SomeInt FROM @tbl;
SELECT @SumOfInts; --SELECT SUM(SomeInt) FROM @tbl is clearly better...)
String concatenation
DECLARE @tbl2 TABLE(SomeText VARCHAR(100));
INSERT INTO @tbl2 VALUES ('a'),('b'),('c');
DECLARE @ConcatString VARCHAR(100)='';
SELECT @ConcatString = @ConcatString + ', ' + SomeText FROM @tbl2;
SELECT @ConcatString;
Better was the usual approach with FOR XML PATH('')
and STUFF()
, which is a hacky workaround too. (Newer versions will bring a built-in function at last!)
REPLACE
it works!This Quirky Update is the only way I know to use a table's values for a step-by-step replacement of many values you can maintain in a table dynamically:
Look at this:
DECLARE @ReplaceValues TABLE(this VARCHAR(100),[by] VARCHAR(100));
INSERT INTO @ReplaceValues VALUES('$',' Dollar'),('€',' Euro'),('abbr.','abbreviations');
DECLARE @SomeText VARCHAR(MAX)=
'This text is about 100 $ or 100 € and shows how to replace abbr.!';
SELECT @SomeText=REPLACE(@SomeText,this,[by]) FROM @ReplaceValues;
SELECT @SomeText;
The result
--This text is about 100 Dollar or 100 Euro and shows how to replace abbreviations!
In general, yes you can use the value from a column for any place in SQL Server's syntax where a value is expected (as opposed to where a name is expected, such as the first argument to DATEPART
).
However, this is broken code - it appears to assume that each row will be evaluated in sequence and each row will be evaluated using values produced from previous rows. However, SQL Server only guarantees to perform one assignment (for the "last" row - however, with no ORDER BY
clause, "last" is ill-defined):
If the SELECT statement returns more than one value, the variable is assigned the last value that is returned.
Whether it may appear to work is no guarantee that more than a single assignment will actually take place.
Actually it will work with the table column and do a replace for each row in your table. But it only work because the @InputString
variable is affected each time.
Maybe you could use a local temporary table to store your NonNumeric
Be carefull because you are returning a NVARCHAR value while @InputString
is a VARCHAR.
A simple code to show you that the REPLACE
can accept values from a table
CREATE TABLE dbo.Test
(
RowID INT IDENTITY(1,1),
Test VARCHAR(20)
)
GO
INSERT INTO dbo.Test VALUES ('James')
INSERT INTO dbo.Test VALUES ('John')
GO
ALTER FUNCTION [dbo].[fn_CleanNumeric]
(
@InputString VARCHAR(500),
@Test VARCHAR(500)
)
RETURNS TABLE
AS
RETURN(
SELECT
REPLACE(@InputString, n.Test, '') AS Test
FROM
dbo.Test n WHERE Test = @Test)
GO
SELECT * FROM [dbo].[fn_CleanNumeric] ('TestJohn','John')
Test
--------
TestT
The SELECT
takes a random value and will check for that string pattern
in the variable @InputString
, thus passing a @Test variable ensures to REPLACE
the right string pattern