Need help with postsql query field matching a certain domain names in the end as per below in a particular FIELD.
1234.abc.xyz.com;
0971.abc.xyz.com
WHERE CAST (domain_name AS text) LIKE '%d{4}.abc.xyz.com%'
#where domain_name is the FIELD name
2
Answers
use ~ followed by your search pattern as regular expression:
playground:
https://dbfiddle.uk/O0Q_Ctmo
fiddle
~ is used for regular expression matching, LIKE for simple matching. Read more about them here: https://www.postgresql.org/docs/current/functions-matching.html#FUNCTIONS-SIMILARTO-REGEXP
If you just want to find domain_name that end in a particular text, the simple matching works fine (don’t know if you really need the cast):
This will not work correctly:
The dot (.) is "any character" in a regular expression so this domain would be selected: abcd.abcxxyzdcom. You need to escape the dot in the string for it to be treated literally like this: ‘dddd.abc.xyz.com’
Underscore is a wildcard for "any character" in the simple LIKE.