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:
Select description from articles where id='29'; Then how do I get the substring from description? thanks
The SQLite substr function returns a substring from a string starting at a specified position with a predefined length.
The Oracle INSTR function is used to search string for substring and find the location of the substring in the string. If a substring that is equal to substring is found, then the function returns an integer indicating the position of the first character of this substring.
Use a SUBSTR() function. The first argument is the string or the column name. The second argument is the index of the character at which the substring should begin. The third argument is the length of the substring.
SQLite substr() returns the specified number of characters from a particular position of a given string. A string from which a substring is to be returned. An integer indicating a string position within the string X. An integer indicating a number of characters to be returned.
Use the substr function.
From the list of core functions:
substr(X,Y,Z) substr(X,Y) substring(X,Y,Z) substring(X,Y)The
substr(X,Y,Z)function returns a substring of input stringXthat begins with theY-thcharacter and which isZcharacters long. IfZis omitted thensubstr(X,Y)returns all characters through the end of the stringXbeginning with theY-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-thcharacter are returned. IfXis a string then characters indices refer to actual UTF-8 characters. IfXis a BLOB then the indices refer to bytes.
substring()is an alias forsubstr()beginning with SQLite version 3.34.
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 field represents the starting point using an index, and the z field represents the substring length.
=============================== |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: Lore instead of Lorem ipsum domit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With