Sei sulla pagina 1di 3

SQL Server 2000 Text Data Manipulation

Problem
Sometimes there is a need to manipulate string values using T-SQL code. With other
languages such as Visual Basic, C++, C#, VBScript, etc... there are a lot of
commands at your finger tips to manipulate string values. With SQL Server 2000
you don't have all the same options, but there are enough commands that if
correctly used together can result in the same functionality you get with other
programming languages.

Solution
The following is a list of SQL Server 2000 T-SQL commands that exist to allow you to
manipulate text data either from stored procedures, triggers, functions or embedded
SQL code.

Command Description
Returns the starting position of the specified
CHARINDEX( findTextData,
expression in a character string. The starting
textData, [startingPosition] )
position is optional.
LEFT( character_expression , Returns the left part of a character string with the
integer_expression ) specified number of characters.
Returns integer value of the length of the string,
LEN( textData )
excluding trailing blanks
Returns a character expression after converting
LOWER ( character_expression )
uppercase character data to lowercase
LTRIM( textData) Removes leading blanks
Returns integer value of the starting position of text
PATINDEX( findTextData, textData )
found in the string
REPLACE( textData, findTextData, Replaces occurrences of text found in the string
replaceWithTextData ) with a new value
REPLICATE( character_expression Repeats a character expression for a specified
, integer_expression ) number of times.
REVERSE( character_expression ) Returns the reverse of a character expression.
RTRIM( textData) Removes trailing blanks
SPACE( numberOfSpaces ) Repeats space value specified number of times
Deletes a specified length of characters and inserts
STUFF( textData, start , length ,
another set of characters at a specified starting
insertTextData )
point
SUBSTRING( textData,
Returns portion of the string
startPosition, length )
Returns a character expression with lowercase
UPPER( character_expression )
character data converted to uppercase.
Examples

The examples below are just simple SELECT statements using hard coded values.
You can use these functions when querying your tables, so you can manipulate the
string values as you query your data and return the modified result.

Query Value
SELECT CHARINDEX('SQL',
'Microsoft SQL Server - SQL 11 (SQL is found in the 11 position)
Server')
SELECT CHARINDEX('SQL', 24 (SQL is found in the 24 position,
'Microsoft SQL Server - SQL since we started looking in position
Server', 20) 20)
SELECT LEFT('Microsoft SQL Microsoft SQL Server (left 20
Server - SQL Server' , 20 ) characters of the string)
SELECT LEN('Microsoft SQL
33 (total length of the string)
Server - SQL Server')
SELECT LOWER('Microsoft SQL microsoft sql server - sql server
Server - SQL Server') (string in lower case)
Microsoft SQL Server - SQL
SELECT LTRIM( ' Microsoft
Server (trimmed string removing
SQL Server - SQL Server ')
leading spaces)
SELECT PATINDEX( '%SQL%',
'Microsoft SQL Server - SQL 11 (SQL is found in the 11 position)
Server' )
SELECT REPLACE( 'Microsoft Microsoft SQL Server 2005 - SQL
SQL Server - SQL Server', Server 2005 (string after we replace
'Server', 'Server 2005' ) 'Server' with 'Server 2005')
SELECT REPLICATE( 'x' ,
xxxxxxxxxx (x replicated 10 times)
10 )
SELECT REVERSE( 'Microsoft revreS LQS tfosorciM (string in
SQL Server' ) reverse)
Microsoft SQL Server - SQL Server
SELECT RTRIM( ' Microsoft
(string after removing trailing
SQL Server - SQL Server ')
spaces)
SELECT 'Microsoft' + Microsoft SQL Server
SPACE(10) + 'SQL Server' (string after inserting 10 spaces)
SELECT STUFF( 'Microsoft Microsoft 2005 Server (string after
SQL Server', 11 , 3 , replacing positions 11, 12, 13 with
'2005' ) '2005')
SELECT SUBSTRING( Microsoft (substring of statement
'Microsoft SQL Server', 1, starting at position 1 for 9
9 ) characters)
SELECT UPPER('Microsoft SQL MICROSOFT SQL SERVER - SQL SERVER
Server - SQL Server') (string in upper case)

In addition to using the commands by themselves you can use multiple commands at
the same time to provide more meaningful results.

Query Value
Microsoft SQL (find portion
SELECT LEFT('Microsoft SQL Server',
of string where a space is
CHARINDEX(' ', 'Microsoft SQL
found but starting at
Server - SQL Server',13) - 1)
position 13)
SELECT LEFT('Microsoft SQL Server', Microsoft (find portion of
CHARINDEX(' ', 'Microsoft SQL string where a space is
Server - SQL Server') - 1) found)
Microsoft SQL Server - SQL
SELECT LTRIM(RTRIM(' Microsoft SQL
Server (trim leading and
Server - SQL Server '))
trailing spaces)

Potrebbero piacerti anche