How to inject a variable in a string in Oracle SQL Developer? - sql

Here is my one of my tries without sucess:
var myVar VARCHAR;
exec :myVar:= 'm';
select * from users where lower(name) like lower(':myVar%')
myVar is the variable i want to inject in the string
Hope to have explained my question well. Thanks in advance.

Don't put the variable inside the quotes:
select * from users where lower(name) like lower(:myVar) || '%'

A few things.
var myVar VARCHAR2(2); --varchar2, not varchar
exec :myVar:= 's%'; -- put the wildcard here, it's just easier
select * from employees where lower(first_name) like lower(:myVar)
run everything via F5

Related

MsSql: How to pass a parameter in query in the 'LIKE' Operator instead of a static value

How can I pass a parameter in query instead of static value in the LIKE operator?
My table would be like this
The query i am currently using is:
Select Url from UrlTable where Url LIKE '%customer%' ;
You can use variables to assign values to them. Passing then into a stored procedure is a common process and the syntax is below.
CREATE PROCEDURE Test(#Customer NVARCHAR(150))
AS
Select Url from UrlTable where Url LIKE '%'+#Customer+'%'
Can you try the following;
DECLARE #Customer nvarchar(50) = [value_to_filter];
Select Url from UrlTable where Url LIKE '%' + #customer + '%';

Using Like '%#mail%' in stored procedure

If I try to call a stored procedure to check for mail addresses containing a #mail it fails because it sees the #mail as a variable instead of a string literal
Is this what you want?
where email like '%' || #mail || '%'
|| here is the standard operator for string concatenation. This can very depending on the database.
You can use
Concat function
SELECT * FROM TableA WHERE Email LIKE CONCAT('%',#Email,'%')
I ended up creating a variable which holds the like #domainname placed it in the exec statement and it started working. So I guess the variable gets parced, the value is added to the query and its accepted as text in stead of searching for a variable

Appending character to variable in execute query

I want to execute a dynamic SQL statement, which searches for names whose last name is always a constant and first name is a variable.
Here is a query I have written for selecting a row with name='Test lastname'.
EXECUTE 'SELECT name FROM users
WHERE name=$1 lastname'
USING ('Test');
This generates a syntax error. Is it possible to do this?
I think you need something like this:
EXECUTE 'SELECT user_id FROM users
WHERE name=$1'
USING Test||' lastname' ;
Here Test is variable and 'lastname' is hard coded value
Also another way is as #JorgeCampos mentioned:
...WHERE name=$1 || '' lastname''' USING 'Test';

Apostrophe(') in SQL data,oracle 11g

I need to insert data into a sql table using a csv file with apostrophe(') and ('') in few rows.
I was able to handle it using the below method.
Get open_quote and close_quote and put the username and email_id between these two variable.
SELECT CHR(113)||CHR(39)||CHR(91) INTO OPEN_QUOTE FROM dual;
SELECT CHR(93)||CHR(39) INTO CLOSE_QUOTE FROM dual;
enter image description here
It looks ugly.I could have used replace but i opted for this method.
Could you please let me know of any other method so that my code looks good?
Attached is the screenshot of the dynamic sql.
You can have a single quote in a string by doubling it. For instance:
select 'It''s Bob''s book'
from dual;
As of Oracle 10g in PL/SQL you can have:
V_SQL_STATEMENT := q'[It's Bob's book]';
See Oracle SQL Reference for details on text literals.
Use the alternative-quoting mechanism and several REPLACEs instead of concatenation. It's a little extra work but it makes it clearer what the final SQL statement will look like.
v_sql_statement := replace(replace(replace(replace(
q'[
insert into login
(USER_ID,CLIENT_ID,EMAIL,PSWD_HASH,NETWORK_ID,FAILED_LOGIN_ATTEMPTS,NAME)
VALUES
(
LOGIN_SEQ.nextval,
#P_CLIENT_ID#,
'#PARSE_EMAIL#',
#V_PSWD_HASH#,
NULL,
0,
'#USER_NAME#'
)
]'
, '#P_CLIENT_ID#', p_client_id)
, '#PARSE_EMAIL#', parse_email(lower(c1.email)))
, '#V_PSWD_HASH#', v_pswd_hash)
, '#USER_NAME#', nvl(c1.name, generate_user_name(parse_email(c1.email))))
;

Declaring a Temporary Variable in Oracle Database

i have worked previously with SQL Server and was able to learn how to create temporary variables and use them in SQL
i used to write something like this:
declare #Student nvarchar(255)
select #Student = studentname from sometable where somecondition = 1
declare #teacher nvarchar(255)
select #teacher = teachername from sometable2 where somecondition >2
and then
select #student, #teacher, other columns from sometable where some condition
i want to do the same thing in ORACLE Database.
Please Help!
If you want to do this in SQL*Plus without using PL/SQL, you can use substitution variables:
column studentname new_value l_studentname
select studentname from sometable where somecondition = 1;
column teachername new_value l_teachername
select teachername from sometable2 where somecondition >2;
select '&l_studentname', '&l_teachername,' other columns
from sometable where somecondition;
The new_value clause in the column directive automatically assigns the value from any following select to a local variable, which I've prepended with l_ but you can call anything. You then reference that local variable in future queries with the & variable substitution syntax.
You can use them pretty much anywhere you'd normally have a value. e.g. in the where clause. Note that text values have to be enclosed in quotes, hence '&l_studentname'; without the quotes the value would be interpreted as a column name in this case, which wouldn't work.
You can declare a variable say
SOME_VAR VARCHAR2(255);
Then use it in your query directly
SELECT DISTINCT YT.TEACHER_NAME
INTO SOME_VAR
FROM YOUR_TABLE YT
WHERE YT.TEACHER_ID = 1;
Then you are free to use this variable, SOME_VAR, for further use
Of course, this will not work in a simple SQL statement, but in case you use it in a programming block, like a procedure.
Hope it helps