Link to home
Start Free TrialLog in
Avatar of PMP2009
PMP2009Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Dynamic SQL using "LIKE" ---ERROR: "SQL COMMAND NOT PROPERLY ENDED"

I'm gettting an error with the code at " AND SUBSTR (c.map, 1, 3) LIKE ''%'' '''||p12||''' ''%'' " 
saying as "SQL COMMAND NOT PROPERLY ENDED"
Avatar of Raja Jegan R
Raja Jegan R
Flag of India image

Try this one..

AND SUBSTR (c.map, 1, 3) LIKE '%' || p12 || '%'
SQL Server T-SQL does not have a SUBSTR function, it's called SUBSTRING instead.

If you are trying to select records that contain "p12" in the c.map field, no matter where, try the first code snippet.  The % stands for any bunch of characters.

If your search string "p12" should really be the first 3 characters of the c.map field then try snippet 2.  No need to use LIKE because you always have 3 characters returned from the SUBSTRING().

--snippet 1
AND c.map LIKE '%p12%'
 
--snippet 2
AND SUBSTRING(c.map, 1, 3) = 'p12'

Open in new window

dynamic sql, and within oracle:
 AND SUBSTR (c.map, 1, 3) LIKE ''%'' ||p12|| ''%'' 

Open in new window

Avatar of lilian-arnaud
lilian-arnaud

same result sa rrjegan17 :-)
AND SUBSTR (c.map, 1, 3) LIKE '%' ||p12|| '%'

Open in new window

as the sql is part of dynamic sql, for example:

EXECUTE IMMEDIATE ' select ... where ... '

the suggestions with single quotes won't work...
If its dynmaic sql then try the following...
v_sql := q'[SELECT * FROM yourtable WHERE .... SUBSTR (c.map, 1, 3) LIKE '%' || p12 || '%']';
Avatar of PMP2009

ASKER

No....I have tried all the above ways and still can't get the correct solution...Plz help its very urgent
Avatar of PMP2009

ASKER

Also please not that p12  is my input parameter passed to the Stored procedure
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Angel,
   Since you are caring about the first three characters only, why it cant be like this:

AND c.map LIKE p12 || '%'
<< Also please not that p12  is my input parameter passed to the Stored procedure >>

What is the parameter data type declared for this p12?
If it is varchar2() then you wont be facing any issue, I guess..
>Angel,
>   Since you are caring about the first three characters only, why it cant be like this:

that depends on the needs.

say c.map contains "ABC000000"

and p12 passed contains "C"

it might still require a match...
Avatar of PMP2009

ASKER

Hello...i think i got the answer
Avatar of PMP2009

ASKER

Thank you very much...in solving this....
Angel, Got it..
I assumed that the asker will be passing three characters as input and hence the confusion.