SQLite Trim() Function

  Total Views : 359
  Comments : 0
  Keywords : SQLite trim() function to remove whitespaces with example, SQLite trim example to remove spaces, SQLite trim function to trim data with example

Here we will learn what is SQLite Trim() function and how to use SQLite Trim() function to remove specified characters from both leading and trailing ends of string.

SQLite Trim() Function

Generally in SQLite Trim() function is used to remove specified characters from both starting and ending of the string.

 

In case if we didn’t specify any characters to remove for Trim() function, then it will remove only space characters from both ends of a string. 

Syntax of SQLite Trim() Function

Following is the syntax of SQLite Trim() function to remove leading and trailing characters from string.

 

TRIM( string, trim_character )

In above SQLite trim() function syntax we defined multiple parameters those are

 

string – Its source string which used to trim characters.

 

trim_character – The trim_character is a character which we need to remove from both ends of string. It’s Optional.

 

In case we didn’t mentioned trim_character then trim() function will remove the spaces from starting and ending of the source string.

SQLite Trim() Function Examples

Now we will see how to use SQLite trim() function to remove starting and ending of characters with examples.

 

Following is the SQLite trim() example to remove leading and trailing spaces of string.

 

sqliteSELECT TRIM('   Welcome to Tutlane.com   ');

 

TRIM('   Welcome to Tutlane.com   ')

-----------------------------------

Welcome to Tutlane.com

If you observe above example we didn’t specified any trim character that’s the reason trim() function removed starting and ending spaces of string.

SQLite Trim() Function with Character

Following is the SQLite trim() example with trim character to remove starting and ending of characters from string.

 

sqliteSELECT TRIM('##Welcome to Tutlane.com####','#');

 

TRIM('##Welcome to Tutlane.com####')

-----------------------------------

Welcome to Tutlane.com

If you observe above SQLite trim() function example we defined trim character “#” to remove from string. So it removed special character “#” from both sides of string.

 

Following is the another SQLite Trim() function statement to remove leading and trailing character(s) 'ma' from the given string 'madam'.

 

sqlite> SELECT 'madam'trim('madam','ma');

 

'madam'     trim('madam','ma')

----------  ------------------

madam       d

If you observe above SQLite Trim() function statement we are removing starting and ending characters “ma” from string “madam”.

 

Following is the another example of SQLite Trim() function to remove characters “123” from string “123321tutlane321123”.

 

sqliteSELECT TRIM('123321tutlane.com321123','123');

 

TRIM('123321tutlane.com321123')

-----------------------------------

tutlane.com

If you observe above result Trim() function removed starting and ending characters “123” from given string.

SQLite Trim() Function with Tables

Now we will see how to use Trim() function in SQLite tables with examples for that we need to create table called emp_master using following query.

 

To create and insert some data in emp_master table execute following query.

 

CREATE TABLE emp_master

(emp_id INTEGER PRIMARY KEY AUTOINCREMENT,

first_name TEXT,

last_name TEXT,

salary NUMERIC,

dept_id INTEGER);

 

INSERT INTO emp_master

values (1,'Honey','Patel', 10100,1),

(2,'Shweta','Jariwala', 19300,2),

(3,'Vinay','Jariwala', 35100,3),

(4,'Jagruti','Viras', 9500,2),

(5,'Shweta','Rana',12000,3),

(6,'sonal','Menpara', 13000,1),

(7,'Yamini','Patel', 10000,2),

(8,'Khyati','Shah', 50000,3),

(9,'Shwets','Jariwala',19400,2);

Now run the following query to check the records of emp_master table.

 

sqliteSELECT FROM emp_master;

 

emp_id      first_name  last_name   salary      dept_id

----------  ----------  ----------  ----------  ----------

1           Honey       Patel       10100       1

2           Shweta      Jariwala    19300       2

3           Vinay       Jariwala    35100       3

4           Jagruti     Viras       9500        2

5           Shweta      Rana        12000       3

6           Sonal       Menpara     13000       1

7           Yamini      Patel       10000       2

8           Khyati      Shah        50000       3

9           Shwets      Jariwala    19400       2

Now write the query like as shown following to remove starting and ending characters “sh” from first name of employees whose first name starts with “sh” from emp_master table.

 

sqliteSELECT trim(first_name,'Sh')

FROM emp_master

WHERE first_name LIKE 'Sh%';

 

trim(first_name,'Sh')

---------------------

weta

weta

wets

If you observe above result the Trim() function removed leading and trailing characters “sh” from first_name column in emp_master table.

 

This is how we can use SQLite Trim() function to remove starting and ending characters from string based on our requirements. 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User