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ù)
- office課程播放地址及課程明細(xì)
- Excel Word PPT Access VBA等Office技巧學(xué)習(xí)平臺(tái)
- 將( .accdb) 文件格式數(shù)據(jù)庫轉(zhuǎn)換為早期版本(.mdb)的文件格式
- 將早期的數(shù)據(jù)庫文件格式(.mdb)轉(zhuǎn)換為 (.accdb) 文件格式
- KB5002984:配置 Jet Red Database Engine 數(shù)據(jù)庫引擎和訪問連接引擎以阻止對(duì)遠(yuǎn)程數(shù)據(jù)庫的訪問(remote table)
- Access 365 /Access 2019 數(shù)據(jù)庫中哪些函數(shù)功能和屬性被沙箱模式阻止(如未啟動(dòng)宏時(shí))
- Access Runtime(運(yùn)行時(shí))最全的下載(2007 2010 2013 2016 2019 Access 365)
- Activex控件或Dll 在某些電腦無法正常注冊(cè)的解決辦法(regsvr32注冊(cè)時(shí)卡?。?/a>
- office使用部分控件時(shí)提示“您沒有使用該ActiveX控件許可的問題”的解決方法
- RTF文件(富文本格式)的一些解析
- Access樹控件(treeview) 64位Office下出現(xiàn)橫向滾動(dòng)條不會(huì)自動(dòng)定位的解決辦法
- Access中國樹控件 在win10電腦 節(jié)點(diǎn)行間距太小的解決辦法
- EXCEL 2019 64位版(Office 2019 64位)早就支持64位Treeview 樹控件 ListView列表等64位MSCOMMCTL.OCX控件下載
- VBA或VB6調(diào)用WebService(直接Post方式)并解析返回的XML
- 早期PB程序連接Sqlserver出現(xiàn)錯(cuò)誤
- MMC 不能打開文件C:/Program Files/Microsoft SQL Server/80/Tools/Binn/SQL Server Enterprise Manager.MSC 可能是由于文件不存在,不是一個(gè)MMC控制臺(tái),或者用后來的MMC版
- sql server連接不了的解決辦法
- localhost與127.0.0.1區(qū)別
- Roych的淺談數(shù)據(jù)庫開發(fā)系列(Sql Server)
- sqlserver 自動(dòng)備份對(duì)備份目錄沒有存取權(quán)限的解決辦法
- 安裝Sql server 2005 express 和SQLServer2005 Express版企業(yè)管理器 SQLServer2005_SSMSEE
聯(lián)系人: | 王先生 |
---|---|
Email: | 18449932@qq.com |
QQ: | 18449932 |
微博: | officecn01 |