User-Defined string Functions SQLCLR MS SQL 2005
User-Defined string Functions MS SQL Server 2005 Transact-SQL SQLCLR (VB. Net, C#.Net, C++. Net)
Ladies and Gentlemen,
I am pleased to offer, free of charge, the following string functions MS SQL Server 2005 Transact-SQL SQLCLR (VB. Net, C#.Net, C++. Net):
AT(): Returns the beginning numeric position of the nth occurrence of a character expression within another character expression, counting from the leftmost character.
OCCURS(): Returns the number of times a character expression occurs within another character expression (including overlaps).
CHRTRAN(): Replaces each character in a character expression that matches a character in a second character expression with the corresponding character in a third character expression.
STRTRAN(): Searches a character expression for occurrences of a second character expression, and then replaces each occurrence with a third character expression. Unlike a built-in function Replace, STRTRAN has three additional parameters.
STRFILTER(): Removes all characters from a string except those specified.
GETWORDCOUNT(): Counts the words in a string.
GETWORDNUM(): Returns a specified word from a string.
GETALLWORDS(): Inserts the words from a string into the table.
PROPER(): Returns from a character expression a string capitalized as appropriate for proper names.
Plus, there are versions for MS SQL SERVER, SYBASE ASA, DB2, Oracle.
More than 8000 people have already downloaded my functions. I hope you will find them useful as well.
For more information about string UDFs MS SQL Server 2005 Transact-SQL SQLCLR (VB. Net, C#.Net, C++. Net) please visit the
http://www.universalthread.com/wconnect/wc.dll?LevelExtreme~2,54,33,29527
Please, download the file
http://www.universalthread.com/wconnect/wc.dll?LevelExtreme~2,2,29527
With the best regards.
AT(): Returns the beginning numeric position of the nth occurrence of a character expression within another character expression, counting from the leftmost character.
RAT(): Returns the numeric position of the last (rightmost) occurrence of a character string within another character string.
OCCURS(): Returns the number of times a character expression occurs within another character expression (including overlaps).
OCCURS2(): Returns the number of times a character expression occurs within another character expression (excluding overlaps).
PADL(): Returns a string from an expression, padded with spaces or characters to a specified length on the left side.
PADR(): Returns a string from an expression, padded with spaces or characters to a specified length on the right side.
PADC(): Returns a string from an expression, padded with spaces or characters to a specified length on the both sides.
CHRTRAN(): Replaces each character in a character expression that matches a character in a second character expression with the corresponding character in a third character expression.
STRTRAN(): Searches a character expression for occurrences of a second character expression, and then replaces each occurrence with a third character expression. Unlike a built-in function Replace, STRTRAN has three additional parameters.
STRFILTER(): Removes all characters from a string except those specified.
GETWORDCOUNT(): Counts the words in a string.
GETWORDNUM(): Returns a specified word from a string.
GETALLWORDS(): Inserts the words from a string into the table.
PROPER(): Returns from a character expression a string capitalized as appropriate for proper names.
RCHARINDEX(): Similar to the Transact-SQL function Charindex, with a Right search.
ARABTOROMAN(): Returns the character Roman numeral equivalent of a specified numeric expression (from 1 to 3999).
ROMANTOARAB(): Returns the number equivalent of a specified character Roman numeral expression (from I to MMMCMXCIX).
AT, PADL, PADR, CHRTRAN, PROPER: Similar to the Oracle functions PL/SQL INSTR, LPAD, RPAD, TRANSLATE, INITCAP.
Plus, there are CHM files in English, French, Spanish, German and Russian.
Plus, there are versions for MS SQL SERVER, SYBASE ASA, DB2, Oracle.
-- for instance
'Author: Igor Nikiforov, Montreal, EMail: udfunctions@gmail.com
'STRTRAN() User-Defined Function
'Searches a character expression for occurrences of a second character expression,
'and then replaces each occurrence with a third character expression.
'STRTRAN (@cSearched, @cExpressionSought , [@cReplacement]
'[, @nStartOccurrence] [, @nNumberOfOccurrences] [, @nFlags])
'Return Values nvarchar(4000)
'Parameters
'@cSearched Specifies the character expression that is searched.
'@cExpressionSought Specifies the character expression that is searched for in @cSearched.
'@cReplacement Specifies the character expression that replaces every occurrence of @cExpressionSought in @cSearched.
'If you omit @cReplacement, every occurrence of @cExpressionSought is replaced with the empty string.
'@nStartOccurrence Specifies which occurrence of @cExpressionSought is the first to be replaced.
'For example, if @nStartOccurrence is 4, replacement begins with the fourth occurrence of @cExpressionSought in @cSearched and the first three occurrences of @cExpressionSought remain unchanged.
'The occurrence where replacement begins defaults to the first occurrence of @cExpressionSought if you omit @nStartOccurrence.
'@nNumberOfOccurrences Specifies the number of occurrences of @cExpressionSought to replace.
'If you omit @nNumberOfOccurrences, all occurrences of @cExpressionSought, starting with the occurrence specified with @nStartOccurrence, are replaced.
'@nFlags Specifies the case-sensitivity of a search according to the following values:
'-
'@nFlags Description
'0 (default) Search is case-sensitive, replace is with exact @cReplacement string.
'1 Search is case-insensitive, replace is with exact @cReplacement string.
'2 Search is case-sensitive; replace is with the case of @cReplacement changed to match the case of the string found.
'The case of @cReplacement will only be changed if the string found is all uppercase, lowercase, or proper case.
'3 Search is case-insensitive; replace is with the case of @cReplacement changed to match the case of the string found.
'The case of @cReplacement will only be changed if the string found is all uppercase, lowercase, or proper case.
'-
'Remarks
'You can specify where the replacement begins and how many replacements are made.
'STRTRAN( ) returns the resulting character string.
'Specify –1 for optional parameters you want to skip over if you just need to specify the @nFlags setting.
'Example
'select dbo.STRTRAN('ABCDEF', 'ABC', 'XYZ',-1,-1,0) -- Displays XYZDEF
'select dbo.STRTRAN('ABCDEF', 'ABC', default,-1,-1,0) -- Displays DEF
'select dbo.STRTRAN('ABCDEFABCGHJabcQWE', 'ABC', default,2,-1,0) -- Displays ABCDEFGHJabcQWE
'select dbo.STRTRAN('ABCDEFABCGHJabcQWE', 'ABC', default,2,-1,1) -- Displays ABCDEFGHJQWE
'select dbo.STRTRAN('ABCDEFABCGHJabcQWE', 'ABC', 'XYZ', 2, 1, 1) -- Displays ABCDEFXYZGHJabcQWE
'select dbo.STRTRAN('ABCDEFABCGHJabcQWE', 'ABC', 'XYZ', 2, 3, 1) -- Displays ABCDEFXYZGHJXYZQWE
'select dbo.STRTRAN('ABCDEFABCGHJabcQWE', 'ABC', 'XYZ', 2, 1, 2) -- Displays ABCDEFXYZGHJabcQWE
'select dbo.STRTRAN('ABCDEFABCGHJabcQWE', 'ABC', 'XYZ', 2, 3, 2) -- Displays ABCDEFXYZGHJabcQWE
'select dbo.STRTRAN('ABCDEFABCGHJabcQWE', 'ABC', 'xyZ', 2, 1, 2) -- Displays ABCDEFXYZGHJabcQWE
'select dbo.STRTRAN('ABCDEFABCGHJabcQWE', 'ABC', 'xYz', 2, 3, 2) -- Displays ABCDEFXYZGHJabcQWE
'select dbo.STRTRAN('ABCDEFAbcCGHJAbcQWE', 'Aab', 'xyZ', 2, 1, 2) -- Displays ABCDEFAbcCGHJAbcQWE
'select dbo.STRTRAN('abcDEFabcGHJabcQWE', 'abc', 'xYz', 2, 3, 2) -- Displays abcDEFxyzGHJxyzQWE
'select dbo.STRTRAN('ABCDEFAbcCGHJAbcQWE', 'Aab', 'xyZ', 2, 1, 3) -- Displays ABCDEFAbcCGHJAbcQWE
'select dbo.STRTRAN('ABCDEFAbcGHJabcQWE', 'abc', 'xYz', 1, 3, 3) -- Displays XYZDEFXyzGHJxyzQWE
'See Also replace(), CHRTRAN()
'UDF the name and functionality of which correspond to the Visual FoxPro function
Public Shared Function Strtran(ByVal cSearched As String, ByVal cExpressionSought As String, Optional ByVal cReplacement As String = "", _
Optional ByVal nStartOccurrence As Short = -1, Optional ByVal nNumberOfOccurrences As Short = -1, Optional ByVal nFlags As Short = 0) As String
Dim StartPart As String = "", FinishPart As String = "", nAtStartOccurrence As Short = 0, nAtFinishOccurrence As Short = 0
If nStartOccurrence = -1 Then
nStartOccurrence = 1
End If
If nFlags = 0 OrElse nFlags = 2 Then
nAtStartOccurrence = At2(cExpressionSought, cSearched, nStartOccurrence)
nAtFinishOccurrence = IIf(nNumberOfOccurrences = -1, 0, At2(cExpressionSought, cSearched, nStartOccurrence + nNumberOfOccurrences))
ElseIf nFlags = 1 OrElse nFlags = 3 Then
nAtStartOccurrence = At2(cExpressionSought.ToLower, cSearched.ToLower, nStartOccurrence)
nAtFinishOccurrence = IIf(nNumberOfOccurrences = -1, 0, At2(cExpressionSought.ToLower, cSearched.ToLower, nStartOccurrence + nNumberOfOccurrences))
Else
cSearched = "Error, sixth parameter must be 0, 1, 2, 3 !"
End If
If nAtStartOccurrence > 0 Then
StartPart = cSearched.Substring(0, nAtStartOccurrence - 1)
If nAtFinishOccurrence > 0 Then
FinishPart = cSearched.Substring(nAtFinishOccurrence - 1)
cSearched = cSearched.Substring(nAtStartOccurrence - 1, nAtFinishOccurrence - nAtStartOccurrence)
Else
cSearched = cSearched.Substring(nAtStartOccurrence - 1)
End If
If nFlags = 0 OrElse (nFlags = 2 AndAlso cReplacement.Length = 0) Then
cSearched = Replace(cSearched, cExpressionSought, cReplacement)
ElseIf nFlags = 1 OrElse (nFlags = 3 AndAlso cReplacement.Length = 0) Then
cSearched = MyReplace(cSearched, cExpressionSought, cReplacement)
Else
Dim cNewSearched As String = "", cNewExpressionSought As String, cNewReplacement As String, nAtPreviousOccurrence As Short '
nAtStartOccurrence = -cExpressionSought.Length
For i As Integer = 1 To 2147483647
If nFlags = 3 Then
nAtStartOccurrence = cSearched.IndexOf(cExpressionSought, nAtStartOccurrence + cExpressionSought.Length, OrdinalIgnoreCase)
Else
nAtStartOccurrence = cSearched.IndexOf(cExpressionSought, nAtStartOccurrence + cExpressionSought.Length)
End If
If nAtStartOccurrence = -1 Then
nAtStartOccurrence = nAtPreviousOccurrence
Exit For
End If
If i > 1 Then
cNewSearched &= cSearched.Substring(nAtPreviousOccurrence + cExpressionSought.Length, nAtStartOccurrence - (nAtPreviousOccurrence + cExpressionSought.Length))
Else
cNewSearched &= cSearched.Substring(0, nAtStartOccurrence)
End If
cNewExpressionSought = cSearched.Substring(nAtStartOccurrence, cExpressionSought.Length)
If cNewExpressionSought.ToLower = cNewExpressionSought.ToUpper Then ' no letters in string
cNewReplacement = cReplacement
ElseIf cNewExpressionSought = cNewExpressionSought.ToUpper Then
cNewReplacement = cReplacement.ToUpper
ElseIf cNewExpressionSought = cNewExpressionSought.ToLower Then
cNewReplacement = cReplacement.ToLower
Else
cNewReplacement = Nothing
End If
If cNewReplacement Is Nothing Then
If Char.IsLetter(cNewExpressionSought.Chars(0)) AndAlso Char.IsUpper(cNewExpressionSought.Chars(0)) AndAlso _
cNewExpressionSought.Substring(1).ToLower = cNewExpressionSought.Substring(1) Then
cNewReplacement = cReplacement.Substring(0, 1).ToUpper + cReplacement.Substring(1).ToLower
Else
For j As Short = 0 To cExpressionSought.Length - 1
If Char.IsLetter(cNewExpressionSought.Chars(j)) Then ' this is letter
If Char.IsLower(cNewExpressionSought.Chars(j)) Then
cNewReplacement = cReplacement.ToLower
Else
cNewReplacement = cReplacement
End If
Exit For
End If
Next
End If
End If
If cNewReplacement Is Nothing Then
cNewReplacement = cReplacement
End If
cNewSearched &= cNewReplacement
nAtPreviousOccurrence = nAtStartOccurrence
Next
cSearched = cNewSearched & cSearched.Substring(nAtStartOccurrence + cExpressionSought.Length)
End If
End If
Return StartPart & cSearched & FinishPart
End Function