Rtrim... ish

Soldato
Joined
18 Oct 2002
Posts
6,795
hi guys,

i've got some data i want to apply an rtrim to, but it doesn't necessarily have spaces at the end of the field. example:

Code:
Celebrations                                   [D]

How can i remove all the annoying spaces and keep the "[D]", or whatever it is, at the end?

B@
 
If you're using SQL Server and C# then you can write an assembly that calls the C# regex functions, deploy it to the SQL Server and then expose it via a SQL scalar-valued function.

So if you have SSMS installed you want to be looking at the Datbase\Programmability\Functions and Datbase\Programmability\Assemblies folders.

[EDIT]Just found what we use:

Code:
    [Microsoft.SqlServer.Server.SqlFunction]
    public static bool IsRegexMatch(string Input,string Pattern)
    {
        // Put your code here
        Regex reg = new Regex(Pattern);

        return reg.IsMatch(Input);
    }

So we have a C# assembly with that function in, we published the assembly to a SQL Server database and then created a SQL function that basically points to that assembly function.

You might want to put some error checking in there as it will throw an exception if you give it a NULL string :o

[EDIT2]Just realised that just checks for a match but doesn't actually do a replacement. I'm sure you get the idea that you could use Spunkeys code in one of these functions and then call that :p
 
Last edited:
probably not the best solution, but the following should work:

PHP:
declare @myval nvarchar(100)
set @myval = 'Celebrations                                       [D]'
select replace(replace(replace(replace(replace(@myval,'     ',' '), '    ', ' '), '   ',' '),'  ',' '), '  ',' ')

Tried with various lengths of spacing, and it seems to work.
 
Last edited:
probably not the best solution, but the following should work:

PHP:
declare @myval nvarchar(100)
set @myval = 'Celebrations                                       [D]'
select replace(replace(replace(replace(replace(@myval,'     ',' '), '    ', ' '), '   ',' '),'  ',' '), '  ',' ')

Tried with various lengths of spacing, and it seems to work.

this works perfectly, thanks very much!!

B@
 
Back
Top Bottom