This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing the use of SIMILAR operator which uses for pattern matching in PostgreSQL.
We know about the LIKE operator, the use of SIMILAR is also very similar to LIKE.
The only difference is, it interprets the pattern using the SQL standard’s definition of a regular expression.
Below are few examples of SIMILAR:
Create sample table with data:
1 2 3 4 5 |
CREATE TABLE tbl_Stud (Rno int, Name character varying); INSERT INTO tbl_Stud VALUES (1,'Anvesh'), (2,'Anil') ,(3,'Roy'), (4,'Reyan'),(5,'Rohit'); |
Test few example of SIMILAR:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
SELECT *FROM tbl_Stud WHERE Name SIMILAR TO 'Anvesh'; rno | name -----+-------- 1 | Anvesh SELECT *FROM tbl_Stud WHERE Name SIMILAR TO 'An%'; rno | name -----+-------- 1 | Anvesh 2 | Anil SELECT *FROM tbl_Stud WHERE Name SIMILAR TO '%n%'; rno | name -----+-------- 1 | Anvesh 2 | Anil 4 | Reyan SELECT *FROM tbl_Stud WHERE Name SIMILAR TO '%(An|Ro)%'; rno | name -----+-------- 1 | Anvesh 2 | Anil 3 | Roy 5 | Rohit |
Leave a Reply