SQL Server substring function usage Summary

Source: Internet
Author: User
Tags sql server substring

Substring function is used for SQL Server operations.
Substring (expression, start, length)

Parameters
Expression
String, binary string, text, image, column, or expression that contains a column. Do not use expressions that contain aggregate functions.

Start
An integer or expression that can be implicitly converted to an int indicates the start position of the substring.

Length
An integer or expression that can be implicitly converted to an int, which refers to the length of the stator string (but the result is supplemented with a length returned, it depends on the Expression length and Start Settings !!!!!!!!!!!).

Return Value
If expression is a supported character data type, character data is returned. If expression is a supported binary data type, binary data is returned. If start = 1, the substring starts with the first character of the expression.

It should be added that, if the start position is set to 1, it will count from the first character, and so on,

Select substring (ziduan, 1, 6)

The first six characters of the field will be selected, for example, 200908.

If it is set to 0,

Similarly, run select substring (ziduan)

The result is 20090. If it is changed to-1

Similarly, run select substring (ziduan)

The result is 2009.

Similarly-2,-3, ...... and so on
Below are some otherArticle.
Substring ('20140901',-98765) Result: 9
Substring ('20140901', 98765) Result: 98
Substring ('20140901', 98765) Result: 987
I found some information about the function, but I still don't quite understand it.
Substring ($ sourcestring, $ startingloc, $ length)
$ Sourcestring
Resource string.

$ Startingloc
The starting point of the substring in the resource string. If the value is negative or 0, only the characters whose position is greater than zero are returned. If the value is longer than $ sourcestring, a zero-length string is returned.

$ Length
[Optional] number of characters to be retrieved. If this value is not specified, it returns all characters starting from the position specified in $ startingloc until the end of the string.

If the value is negative or 0, only the characters whose position is greater than zero are returned.

Getting started with SQL
Select substring (convert (varchar, birthdate), 5, 1) as Mon, * from employees
Where substring (convert (varchar, birthdate), 5, 1) = '8'

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.