Sql Server substring(expression, start, length)函數

來源:互聯網
上載者:User

Syntax for SUBSTRING:  SUBSTRING(expression, start, length)
expression
        字串、二進位字串、文本、映像、列或包含列的運算式。請勿使用包含彙總函式的運算式。
start
        整數或可以隱式轉換為int 的運算式,指定子字串的開始位置。
length

        整數或可以隱式轉換為 int 的運算式,指定子字串的長度。

例子:

1、起始位置為0,截取長度為3

select substring(DWMC,0,3) as Sub,DWMC as DWMC from DW where DWNM='00010010'
結果:

2、起始位置為1,截取長度為3

select substring(DWMC,1,3) as Sub,DWMC as DWMC from DW where DWNM='00010010'
結果:

3、起始位置為-1,截取長度為3

select substring(DWMC,-1,3) as Sub,DWMC as DWMC from DW where DWNM='00010010'
結果:


4、起始位置為-2,截取長度為3

select substring(DWMC,-2,3) as Sub,DWMC as DWMC from DW where DWNM='00010010'
結果:

5、起始位置為-9,截取長度為3

select substring(DWMC,-9,3) as Sub,DWMC as DWMC from DW where DWNM='00010010'

結果:

        通過上面的例子,可以看出substring的截取,類似於X座標上截取絕對長度,而對於字串而言,負數及零的位置都是空,真正有數的位置是從1開始的!



相關文章

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.