linux之SQL語句簡明教程---SUBSTRING,linux簡明教程

來源:互聯網
上載者:User

linux之SQL語句簡明教程---SUBSTRING,linux簡明教程

SQL 中的 substring 函數是用來抓出一個欄位資料中的其中一部分。這個函數的名稱在不同的資料庫中不完全一樣:

  • MySQL: SUBSTR( ), SUBSTRING( )
  • Oracle: SUBSTR( )
  • SQL Server: SUBSTRING( )

最常用到的方式如下 (在這裡我們用 SUBSTR( ) 為例):

SUBSTR (str, pos)

由 <str> 中,選出所有從第 <pos> 位置開始的字元。請注意,這個文法不適用於 SQL Server 上。

SUBSTR (str, pos, len)

由 <str> 中的第 <pos> 位置開始,選出接下去的 <len> 個字元。

假設我們有以下的表格:

Geography 表格

Region_Name Store_Name
East Boston
East New York
West Los Angeles
West San Diego

例1

SELECT SUBSTR (Store_Name, 3) 
FROM Geography 
WHERE Store_Name = 'Los Angeles';

結果:

's Angeles'

例2

SELECT SUBSTR (Store_Name, 2, 4) 
FROM Geography 
WHERE Store_Name = 'San Diego';

結果:

'an D'

Linux實測如下:


轉載請註明:小劉

相關文章

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.