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'

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/database/SQLServer/

結果:

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開始的!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.