Written by

Independent Consultant
Question Stella Ticker · Apr 29, 2017

How to use variable in a sql statement with the like operator

What is the correct way to write embedded SQL that will use a Like operator and local variable. Ex How can a sql query return the IDs of all rows where LastName has the substring "Doe"?

Set nameToFind="Doe". The below does not work.

&sql(Select ID from myTable where LastName like '%:nameToFind%')

Simple enough but this syntax is tricky!!

Comments

Anil Mathew · Apr 29, 2017

Try

Set nameToFind="%Doe%". The below does not work.

&sql(Select ID from myTable where LastName like :nameToFind)

0
Stella Ticker · Apr 29, 2017

It works !! Thanks Anil . So single quotes are not needed !!

0
Anil Mathew · Apr 30, 2017

Within an SQL statement, the variable starts with a (:) colon.  

If you put the quote it becomes a fixed value.

                                        ':nameToFind'   is interperted as    ":nameToFind"

Set nameToFind="abc"

                                        :nameToFind is interpreted as    "abc"

Regards

Anil

0