office交流網(wǎng)--QQ交流群號(hào)及微信交流群

Access培訓(xùn)群:792054000         Excel免費(fèi)交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

微信交流群(請(qǐng)用微信掃碼)

        

Access的Mid函數(shù)在Sql Server的對(duì)應(yīng)函數(shù)SUBSTRING的用法教程

2017-08-21 10:04:00
zstmtony
原創(chuàng)
6657

使用Access的網(wǎng)友都熟悉mid字符串函數(shù),它可以獲取字符串中指定位置指定長度的字符串

那么在Sql server中有否這個(gè)函數(shù),如果沒有,有否替代函數(shù)呢?

答案是有的,那就是Substring函數(shù)


根據(jù)Transact-SQL 幫助參考,這個(gè)函數(shù)的使用方法如下: 

 
SUBSTRING
返回字符、binary、text 或 image 表達(dá)式的一部分。有關(guān)可與該函數(shù)一起使用的有效 Microsoft® SQL Server™ 數(shù)據(jù)類型的更多信息,請(qǐng)參見數(shù)據(jù)類型。 

語法
SUBSTRING ( expression , start , length ) 

參數(shù)
expression

是字符串、二進(jìn)制字符串、text、image、列或包含列的表達(dá)式。不要使用包含聚合函數(shù)的表達(dá)式。

start

是一個(gè)整數(shù),指定子串的開始位置。

length

是一個(gè)整數(shù),指定子串的長度(要返回的字符數(shù)或字節(jié)數(shù))。



說明  由于在 text 數(shù)據(jù)上使用 SUBSTRING 時(shí) start 和 length 指定字節(jié)數(shù),因此 DBCS 數(shù)據(jù)(如日本漢字)可能導(dǎo)致在結(jié)果的開始或結(jié)束位置拆分字符。此行為與 READTEXT 處理 DBCS 的方式一致。然而,由于偶而會(huì)出現(xiàn)奇怪的結(jié)果,建議對(duì) DBCS 字符使用 ntext 而非 text。


返回類型
如果 expression 是支持的字符數(shù)據(jù)類型,則返回字符數(shù)據(jù)。如果 expression 是支持的 binary 數(shù)據(jù)類型,則返回二進(jìn)制數(shù)據(jù)。

返回字符串的類型與給定表達(dá)式的類型相同(表中顯示的除外)。

給定的表達(dá)式 返回類型 
text varchar 
image varbinary 
ntext nvarchar 


注釋
在字符數(shù)中必須指定使用 ntext、char 或 varchar 數(shù)據(jù)類型的偏移量(start 和 length)。在字節(jié)數(shù)中必須指定使用 text、image、binary 或 varbinary 數(shù)據(jù)類型的偏移量。 



說明  兼容級(jí)別可能影響返回值。有關(guān)兼容級(jí)別的更多信息,請(qǐng)參見 sp_dbcmptlevel。 


示例
A. 在字符串上使用 SUBSTRING
下例顯示如何只返回字符串的一部分。該查詢?cè)谝涣兄蟹祷?nbsp;authors 表中的姓氏,在另一列中返回 authors 表中的名字首字母。

USE pubs
SELECT au_lname, SUBSTRING(au_fname, 1, 1)
FROM authors
ORDER BY au_lname

下面是結(jié)果集:

au_lname                                   
---------------------------------------- - 
Bennet                                   A 
Blotchet-Halls                           R 
Carson                                   C 
DeFrance                                 M 
del Castillo                             I 
...
Yokomoto                                 A 

(23 row(s) affected)

下例顯示如何顯示字符串常量 abcdef 中的第二個(gè)、第三個(gè)和第四個(gè)字符。

SELECT x = SUBSTRING('abcdef', 2, 3)

下面是結(jié)果集:

x
----------
bcd

(1 row(s) affected)

B. 在 text、ntext 和 image 數(shù)據(jù)上使用 SUBSTRING 
下例顯示如何從 pubs 數(shù)據(jù)庫的 publishers 表內(nèi)的每個(gè) text 和 image 數(shù)據(jù)列中返回前 200 個(gè)字符。text 數(shù)據(jù)以 varchar 的形式返回,image 數(shù)據(jù)則以 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'

下面是結(jié)果集:

pub_id logo                   pr_info    
------ ---------------------- ---------- 
1756   0x474946383961E3002500 This is sa

(1 row(s) affected)

下例顯示 SUBSTRING 在 text 和 ntext 數(shù)據(jù)上的效果。首先,下例在 pubs 數(shù)據(jù)庫內(nèi)創(chuàng)建一個(gè)名為 npr_info 的新表。然后,在 npr_info 表中用 pub_info.pr_info 列的前 80 個(gè)字符創(chuàng)建 pr_info 列,并添加ü作為首字符。最后,INNER JOIN 檢索所有出版商標(biāo)識(shí)號(hào)以及 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


請(qǐng)參見

字符串函數(shù)

分享
文章分類
聯(lián)系我們
聯(lián)系人: 王先生
Email: 18449932@qq.com
QQ: 18449932
微博: officecn01
移動(dòng)訪問