SUBSTRING
1、文法
SUBSTRING ( expression ,start , length )
2、參數
expression
是字串、二進位字串、文本、映像、列或包含列的運算式。不要使用包含彙總函式的運算式。
start
指定子字串開始位置的整數。start 可以為 bigint 類型。
length
一個正整數,指定要返回的 expression 的字元數或位元組數。如果 length 為負,則會返回錯誤。length 可以是 bigint 類型。
3、備忘
必須以字元數指定使用 ntext、char 或 varchar 資料類型的位移量(start 和 length)。必須以位元組數指定使用 text、image、binary 或 varbinary 等資料類型的位移量。
4、例子:
| 代碼如下 |
複製代碼 |
|
select substring('GXS',-1,3) :G
select substring('GXS',-1,2) :NULL
select substring('GXS',0,1) :NULL
select substring('GXS',1,1) :G
select substring('GXS',1,2) :GX |
程式碼範例:
下面的樣本返回 Employees 表中每位僱員的名字首字母及完整姓氏:
| 代碼如下 |
複製代碼 |
SELECT SUBSTRING(First Name,1,1) AS Initial, Last Name
FROM Employees
|
下面是結果集:
Initial..........Last Name
-------------------------
A................Funk
M................Pearson
L................Calafato
N................Danner
J................Lee
S................Byham
M................Sutter
R................King
A................Doyle
樣本
A. 在字串上使用 SUBSTRING
下例顯示如何只返回字串的一部分。該查詢在一列中返回 authors 表中的姓氏,在另一列中返回 authors 表中的名字首字母。
| 代碼如下 |
複製代碼 |
USE pubs
SELECT au_lname, SUBSTRING(au_fname, 1, 1)
FROM authors
ORDER BY au_lname
|
下面是結果集:
au_lname
---------------------------------------- -
Bennet A
Blotchet-Halls R
Carson C
DeFrance M
del Castillo I
...
Yokomoto A
(23 row(s) affected)
下例顯示如何顯示字串常量 abcdef 中的第二個、第三個和第四個字元。
SELECT x = SUBSTRING('abcdef', 2, 3)
下面是結果集:
x
----------
bcd
(1 row(s) affected)
B. 在 text、ntext 和 image 資料上使用 SUBSTRING
下例顯示如何從 pubs 資料庫的 publishers 表內的每個 text 和 image 資料列中返回前 200 個字元。text 資料以 varchar 的形式返回,image 資料則以 varbinary 的形式返回。
| 代碼如下 |
複製代碼 |
USE pubs
SELECT pub_id, SUBSTRING(logo, 1, 10) AS logo,
SUBSTRING(pr_info, 1, 10) AS pr_info
FROM pub_info
WHERE pub_id = '1756'
|
下面是結果集:
pub_id logo pr_info
------ ---------------------- ----------
1756 0x474946383961E3002500 This is sa
(1 row(s) affected)
下例顯示 SUBSTRING 在 text 和 ntext 資料上的效果。首先,下例在 pubs 資料庫內建立一個名為 npr_info 的新表。然後,在 npr_info 表中用 pub_info.pr_info 列的前 80 個字元建立 pr_info 列,並添加ü作為首字元。最後,INNER JOIN 檢索所有出版商標識號以及 text 和 ntext 出版商資訊列的 SUBSTRING。
| 代碼如下 |
複製代碼 |
|
IF EXISTS (SELECT table_name FROM INFORMATION_SCHEMA.TABLES
WHERE table_name = 'npub_info')
DROP TABLE npub_info
GO
-- Create npub_info table in pubs database. Borrowed from instpubs.sql.
USE pubs
GO
CREATE TABLE npub_info
(
pub_id char(4) NOT NULL
REFERENCES publishers(pub_id)
CONSTRAINT UPKCL_npubinfo PRIMARY KEY CLUSTERED,
pr_info ntext NULL
)
GO
-- Fill the pr_info column in npub_info with international data.
RAISERROR('Now at the inserts to pub_info...',0,1)
GO
INSERT npub_info VALUES('0736', N'üThis is sample text data for New Moon Books, publisher 0736 in the pubs database')
INSERT npub_info values('0877', N'üThis is sample text data for Binnet & Hardley, publisher 0877 in the pubs databa')
INSERT npub_info values('1389', N'üThis is sample text data for Algodata Infosystems, publisher 1389 in the pubs da')
INSERT npub_info values('9952', N'üThis is sample text data for Scootney Books, publisher 9952 in the pubs database')
INSERT npub_info values('1622', N'üThis is sample text data for Five Lakes Publishing, publisher 1622 in the pubs d')
INSERT npub_info values('1756', N'üThis is sample text data for Ramona Publishers, publisher 1756 in the pubs datab')
INSERT npub_info values('9901', N'üThis is sample text data for GGG&G, publisher 9901 in the pubs database. GGG&G i')
INSERT npub_info values('9999', N'üThis is sample text data for Lucerne Publishing, publisher 9999 in the pubs data')
GO
-- Join between npub_info and pub_info on pub_id.
SELECT pr.pub_id, SUBSTRING(pr.pr_info, 1, 35) AS pr_info,
SUBSTRING(npr.pr_info, 1, 35) AS npr_info
FROM pub_info pr INNER JOIN npub_info npr
ON pr.pub_id = npr.pub_id
ORDER BY pr.pub_id ASC |