SQL 中的 substring 函数是用来抓出一个栏位资料中的其中一部分。这个函数的名称在不同的资料库中不完全一样:
MySQL: SUBSTR(), SUBSTRING()
Oracle: SUBSTR()
SQL Server: SUBSTRING()
最常用到的方式如下(在这里我们用SUBSTR()为例):
SUBSTR(str,pos): 由<str>中,选出所有从第<pos>位置开始的字元。请注意,这个语法不适用于SQL Server上。
SUBSTR(str,pos,len): 由<str>中的第<pos>位置开始,选出接下去的个字元。
例如在数据库中有一张Geography表:
region_name | store_name |
East | Boston |
East | New York |
West | Los Angeles |
West | San Diego |
来看这两个示例:
例1:
[sourcecode language="sql"]
SELECT SUBSTR(store_name, 3) FROM Geography WHERE store_name = 'Los Angeles';
[/sourcecode]
结果:s Angeles
例2:
[sourcecode language="sql"]
SELECT SUBSTR(store_name,2,4) FROM Geography WHERE store_name = 'San Diego';
[/sourcecode]
结果:an D
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。