Postgresql – Can’t find names with criteria that has special symbols in PostgreSQL

postgresql

I am running the following query which gives syntax error:

select distinct e.ahrims_id,e.card_num,e.name_related,e.d_name,e.father_name,
e.d_father_name from hr_employee e left join unit_identification_code uic
on e.unit_identificatin_code_id = uic.id where uic.code in (
'ZRMAAA', 'ZRMA10', 'ZRMA66', 'ZRMA11', 'ZRMA12', 'ZRMA13', 'ZRMA14', 'ZRMA15', 'ZRMA27',
'ZRMA53', 'ZRMB11', 'ZRMA20', 'ZRMA65', 'ZRMA21', 'ZRMA22', 'ZRMA23', 'ZRMA24', 'ZRMA25',
'ZRMA26', 'ZRMA52', 'ZRMA30', 'ZRMA60', 'ZRMA31', 'ZRMA32', 'ZRMA33', 'ZRMA34', 'ZRMA35',
'ZRMA28', 'ZRMA80', 'ZRMB13', 'ZRMA40', 'ZCCA39', 'ZRMA29', 'ZRMA43', 'ZRMA44', 'ZRMA45',
'ZRMA71', 'ZRMA09', 'ZRMA99', 'Z1200N', 'ZTEC14', 'ZRMA81', 'ZRMA82', 'ZRMA83', 'ZRMA85',
'ZRMA86', 'ZRMA87', 'ZRMA88', 'ZRMA89', 'ZRMA90', 'ZRMA92')
and 
(e.name_related = '' || e.name_related like '.' || e.name_related like ',' 
|| e.name_related like '%x%' || e.d_name='' || e.d_name like '.' || e.d_name like ','
|| e.d_name like '%x%' || e.father_name='' || e.father_name like '.' 
|| e.father_name like ',' || e.father_name like '%x%' || e.d_father_name=''
|| e.d_father_name like '.' || e.d_father_name like ',' || e.d_father_name like '%x%'
)   

Following is the error message:

ERROR: syntax error at or near "like"
LINE 3: …= '' || e.name_related like '.' || e.name_related like ',' |…

I googled a lot, but did not find a good solution for this.
Please help if you can.
Thanks in advance.

Best Answer

|| is the operator to concatenate strings in SQL. You probably want OR.

Additionally: LIKE without a wildcard does not make sense. e.name_related like ',' is the same as e.name_related = ','. Multiple ORs are the same as IN so you can simplify your condition to:

(
  e.name_related IN ('', ',', '.') OR e.name_related like '%x%' OR
  e.d_name in ('', '.', ',') OR e.d_name like '%x%' OR 
  e.father_name IN ('', '.', ',') OR e.father_name like '%x%' OR
  e.d_father_name IN ('', '.', ',') OR e.d_father_name like '%x%'
)