database 如何在 SQLIte 中获取子字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10413055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get substring in SQLIte?
提问by Vladimir Stazhilov
I retrieve quite a lot of data from SQLite database. When retrieving I map it to different views in my application. There is a text field in my table from which I don't want to get the full text, just first n chars. So if my query for example is:
我从 SQLite 数据库中检索了大量数据。检索时,我将其映射到应用程序中的不同视图。我的表中有一个文本字段,我不想从中获取全文,只是前 n 个字符。因此,如果我的查询例如是:
Select description from articles where id='29';
Then how do I get the substring from description? thanks
那么如何从描述中获取子字符串呢?谢谢
回答by Dan D.
Use the substrfunction.
使用该substr功能。
From the list of core functions:
从核心功能列表:
substr(X,Y,Z)substr(X,Y)The
substr(X,Y,Z)function returns a substring of input stringXthat begins with theY-th character and which isZcharacters long. IfZis omitted thensubstr(X,Y)returns all characters through the end of the stringXbeginning with the Y-th. The left-most character ofXis number 1. IfYis negative then the first character of the substring is found by counting from the right rather than the left. IfZis negative then theabs(Z)characters preceding theY-th character are returned. IfXis a string then characters indices refer to actual UTF-8 characters. IfXis a BLOB then the indices refer to bytes.
substr(X,Y,Z)substr(X,Y)该
substr(X,Y,Z)函数返回输入字符串的一个子字符串X,该子字符串以第Y-th 个字符开头,长度为Z字符。如果Z被省略,则substr(X,Y)返回X从第 Y 个开始的字符串末尾的所有字符。的最左边的字符X是数字 1。如果Y是负数,则通过从右侧而不是左侧开始计数来找到子字符串的第一个字符。如果Z为负,则返回第 -th 个字符abs(Z)之前的Y字符。如果X是字符串,则字符索引指的是实际的 UTF-8 字符。如果X是 BLOB,则索引指的是字节。
回答by Enzokie
To get the substring in SQLite
在 SQLite 中获取子字符串
You can use the builtin function in SQLite which is substr(X,Y,Z). The x field represents the string input to be sliced, the y and z field represents the starting point and ending point respectively using an index.
您可以使用 SQLite 中的内置函数,即substr(X,Y,Z). x 字段表示要切片的字符串输入,y 和 z 字段分别使用索引表示起点和终点。
===============================
|Database Table : **articles**|
===============================
|id | description |
-------------------------------
|29 | Lorem ipsum domit |
===============================
Now we will try to make a select query for our description
现在我们将尝试为我们的描述进行选择查询
SELECT substr(description,1,4) FROM articles where id='29';
Output would be: Loreinstead of Lorem ipsum domit
输出将是:Lore而不是 Lorem ipsum domit

