sql--String Handling functions

Source: Internet
Author: User

1) ASCII

Format:ascii (character_expression)

Function: Returns the ASCII value of the leftmost character of an expression.

eg

Select ASCII (' abcdef ')
Go

Result:

2) CHAR:

Format:char (integer_expression), integer_expression is an integer between 0~266

Function: Converts the ASCII code to a character.

eg

Select CHAR (97)
Go

Result:

3) Left

Format:left (character_expression, integer_expression)

Function: Returns the specified number of characters starting from the left of the string. Return value type is varchar

EG1:

Returns the leftmost 1 characters of a CNAME.

Use XSCJ
Select Left (cname,1)
From KC
ORDER BY Cname
Go

Result:

EG2:

Select Left (' ABCdef ', 3)
Go

Result:

4) LTRIM

Format:ltrim (character_expression)

Function: Removes leading spaces in the character_expression string-the spaces in front of the string, returns a string, character_expression is a character expression, and the return value type is varchar.

EG1:

Use the LTRIM character to delete the starting space in the character variable.

Select LTRIM (' Code changes the world ')
Go

Result:

EG2:

DECLARE @string varchar (40)

SET @string = ' China, an ancient and great country '

SELECT LTRIM (@string)

GO

6) REPLACE

Format:replace (' String_expression1 ', ' string_expression2 ', ' String_expression3 ')

Function: Replaces the second string expression contained in the first string expression with a 3rd string expression, String_expression1, String_expression2 ', string_expression3 are string expressions, The return value is of character type.

EG1:

DECLARE @var1 varchar, @var2 varchar, @var3 varchar (20)
SET @var1 = ' Violet star is a handsome guy. '/* A SET statement can only assign a value to a variable */
SET @var2 = ' stud '
SET @var3 = ' Meng Mei paper '
SELECT replace (@var1, @var2, @var3)
GO

EG2:

Select REPLACE (' Violet Star is a handsome guy. ', ' The Handsome Man ', ' the sister paper '
Go

Result:

7) SUBSTRING

format:substring (expression, start, length)

Function: Returns part of the data specified in expression. The argument expression can be a string, a binary string, a text,image field, or an expression; start, length is an integer that specifies the starting position of the substring, which specifies the length of the substring (the number of bytes to return). If expression is a character type and a binary type, the return value type is the same as the type of expression.

return value type

text

varchar

image

varbinary

nvarchar

EG1:

The following program returns the last name of the XS table in one column, and the name of the secondary school student in the other column.

SELECT SUBSTRING, Sname, SUBSTRING (Sname, 2, LEN (Sname)-1)
From XS
ORDER by Sname

Result:

EG2:

Select SUBSTRING (' 1139685206 ', 7,3) as ' I Love You '
Go

Result:

7) STR

Format:str (float_expression, length, decimal)

Function: Converts numeric data to character data. Length indicates the total length of the converted string data, and if the length of the data is less than the length of the string, a space is filled in front of the converted data string. Decimal indicates the number of decimal digits of the string data that need to be displayed after conversion.

The ATTENTION:STR function only sees a period (.) as a valid decimal point. If you use a different decimal point (for example, an international application), you can use CStr to convert the number to a string.

EG1:

DECLARE @a float,@b char (10)

Set @a=200.86

Set @b= (select STR (@a,6,2))

Select @b as Output

Go

Result:

EG2:

EG3:

EG4:

sql--String Handling functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.