SUBSTR inside Like statement - sql

need to do something like this but i get a CONCAT ERROR, any idea how to do this SUBSTR inside the like?
thanks
select (Select description from BD3 where description like CONCAT('%', SUBSTR(BD3.nomebalcao,locate(BD3.nomebalcao, 'AG. ')+4,5), '%')) as ToGroup
from BD1
left join BD2 on BD2.group_id =BD1.group_id
left join BD3 on BD1.BPD_INSTANCE_ID = BD3.BPD_INSTANCE_ID
where BD2.GROUP_NAME= 'ADM_SIS'
and BD1.status in (select status_value from stat where name = 'Received')

The CONCAT function in DB2 can only concatenate exactly two strings (CONCAT(a,b), not CONCAT(a,b,c)).
Use the CONCAT operator instead:
'%' CONCAT SUBSTR(BD3.nomebalcao,locate(BD3.nomebalcao, 'AG. ') + 4, 5) CONCAT '%'
or better even the standard SQL concatenation operator ||:
'%' || SUBSTR(BD3.nomebalcao,locate(BD3.nomebalcao, 'AG. ') + 4, 5) || '%'

Related

How can I check if string value in two columns also exists in another column in SQL

So there are 3 columns, first name, last name, and description.
What I want to do is to check if first name or last name, or both, exist in description.
I believe using Like operator might help, but not sure how to write it.
Any suggestion is appreciated!
You can use a case expression and like. In Standard SQL, this would look like:
select t.*,
(case when description like '%' || firstname || '%' or
description like '%' || lastname || '%'
then 1 else 0
end) as in_description_flag
from t;
This uses the SQL standard operator for string concatenation. Some databases have bespoke operators (such as +) or bespoke functions (such as concat()).
EDIT:
In SQL Server:
select t.*,
(case when description like '%' + firstname + '%' or
description like '%' + lastname + '%'
then 1 else 0
end) as in_description_flag
from t;

Fuzzy (Like) Join not working

I have the below code to match on similar characters where possible and it only brings through results from subquery A. Please can someone assist? Thanks
select
*
from
(
Select 'Test' T
)a
left join
(
Select 'Test1' T
)b
on
'%' + a.t + '%'
like
'%' + b.t + '%'
The like pattern only goes on the right side of the operator. I think you intend:
on (a.t like '%' + b.t + '%') or
(b.t like '%' + a.t + '%')

SQL variable length numeric string

This query converts bc_int_phone to just numeric characters eliminating - and '' characters. Say the numbers obtained need not have the same starting numeric. Say I'm searching for 123-456-7890 but this could be of the form 999 123-456-7890.. How do I incorporate the like %(bc_phone_number)% in this code to incorporate this case?
select
ca.callingpartynumber, ca.originalcalledpartynumber, ca.duration,
ca.duration_text, ca.finalcalledpartynumber,
case
when calledpartylastname is not null
then ca.calledpartylastname + ',' + calledpartyfirstname
else p1.name
end as calledpartyname,
p1.location, p1.dept, p1.title,
case
when callingpartylastname is not null
then ca.callingpartylastname + ',' + callingpartyfirstname
else p3.name
end as callingpartyname
from
calldata.calldetailreport ca
join
ps_bc_peoplesource_base p1 on ca.originalcalledpartynumber like replace(p1.bc_int_phone, '-', '')
left outer join
ps_bc_peoplesource_base p3 on ca.callingpartynumber like replace(p3.bc_int_phone, '-', '')
where
callingpartynumber in (select replace(bc_int_phone, '-', '') internal_modified
from ps_bc_peoplesource_base
where bc_lan_id like 'f7c')
try like ca.originalcalledpartynumber like '%'||replace(p1.bc_int_phone, '-', '')||'%'
|| is the concatenation operator for strings in oracle.
USe + if you are using SQL Server.

convert Microsoft SQL Server specific query to ORACLE specific query

Hi all I have the following query written for Microsoft SQL Server.
I need to convert this query to Oracle syntax. I do not have any knowledge in Oracle syntax.
I need your help to make this query work on Oracle database.
I tried many ways but no data returned.
SELECT SISPREV_student.Stu_Id, SISPREV_student.CwId,
SISPREV_student.Sex, SISPREV_student.Nok_Name, SISPREV_student.Nok2_Name,
SISPREV_student.Birth_Dt,
SISPREV_student.Currently_Enrolled, SISPREV_student.Stu_Athlete, SISPREV_student.LivingOnCampus,
SISPREV_student.ImmigrationStatus,
SISPREV_student.ScholarStatus, SISPREV_student.Stu_FirstName, SISPREV_student.Stu_MiddleName,
SISPREV_student.Stu_LastName, SISPREV_student.Ethnicity
FROM SISPREV_address RIGHT OUTER JOIN
SISPREV_student ON SISPREV_address.StudentID =
SISPREV_student.Stu_Id LEFT OUTER JOIN
SISPREV_email ON SISPREV_student.Stu_Id =
SISPREV_email.StudentID LEFT OUTER JOIN
SISPREV_phone ON SISPREV_student.Stu_Id =
SISPREV_phone.StudentID
WHERE ((SISPREV_student.Stu_Id LIKE '%' + '#StudentId' + '%') OR
('#StudentId' = '')) AND
((UPPER(SISPREV_student.Stu_FirstName) LIKE '%' +
UPPER('#StudentFirstName') + '%') OR ('#StudentFirstName' = '')) AND
((UPPER(SISPREV_student.Stu_MiddleName) =
UPPER('#StudentMiddleName')) OR ('#StudentMiddleName' = '')) AND
((UPPER(SISPREV_student.Stu_LastName) LIKE '%' +
UPPER('#StudentLastName') + '%') OR ('#StudentLastName' = '')) AND
((UPPER(SISPREV_student.Nok_Name) LIKE '%' +
UPPER('#StudentNextOfKinName') + '%') OR ('#StudentNextOfKinName' = '')) AND
((UPPER(SISPREV_address.Street) LIKE '%' + UPPER('#StudentStreet') +
'%') OR ('#StudentStreet' = '')) AND
((UPPER(SISPREV_address.City) LIKE '%' + UPPER('#StudentCity') +
'%') OR ('#StudentCity' = '')) AND
((UPPER(SISPREV_address.State) LIKE '%' + UPPER('#StudentState') +
'%') OR ('#StudentState' = '')) AND
((UPPER(SISPREV_address.ZipCode) LIKE '%' + '#StudentZipCode' + '%'
) OR ('#StudentZipCode' = '')) AND
((SISPREV_student.Birth_Dt = CONVERT(SMALLDATETIME,'#StudentBirthDay')) OR ('#StudentBirthDay' = '')) AND
((SISPREV_phone.Phone = '#StudentPhoneNumber') OR
('#StudentPhoneNumber' = '')) AND
((UPPER(SISPREV_student.Sex) = UPPER('#StudentGender')) OR
('#StudentGender' = ''))
GROUP BY SISPREV_student.Stu_Id, SISPREV_student.CwId,
SISPREV_student.Sex, SISPREV_student.Nok_Name, SISPREV_student.Nok2_Name,
SISPREV_student.Birth_Dt,
SISPREV_student.Currently_Enrolled, SISPREV_student.Stu_Athlete, SISPREV_student.LivingOnCampus,
SISPREV_student.ImmigrationStatus,
SISPREV_student.ScholarStatus, SISPREV_student.Stu_FirstName, SISPREV_student.Stu_MiddleName,
SISPREV_student.Stu_LastName, SISPREV_student.Ethnicity
The following needs to change:
((upper(sisprev_address.street) like '%' + upper('#StudentStreet') + '%')
In Oracle || is concatenation not +
((upper(sisprev_address.street) like '%' || upper('#StudentStreet') || '%') or ('#StudentStreet' = ''))
If #StudentStreet comes from user input and can be empty you can check that in Oracle as
or ('#StudentStreet' is null)
Date:
convert(smalldatetime, '#StudentBirthDay')
Dependig on the way you receive the date string
to_date('#StudentBirthDay','some date format')
'some date format' ='DD-MM-YYYY' or whatever format you handle. Look up Oracle data format strings.

Using LIKE within CASE in TSQL

I would like to select the records that contain the content of the #selctDescription parameter but only when #selctDescription is not empty.
I have the following, which does not work:
(t.[description] LIKE
(
CASE
WHEN #selctDescription = '' THEN t.[description]
ELSE ('%' #selctDescription '%')
END
)
)
Can anyone point me in the right direction?
SELECT *
FROM Table
WHERE
((#selctDescription IS NULL OR #selctDescription = '')
OR
(t.[description] LIKE '%' + #selctDescription +'%'))
#selctDescription = '' OR t.[description] LIKE '%' + #selctDescription + '%'
I think your code is equivalent to:
t.[description] LIKE '%' + #selctDescription + '%'
Your description on the other hand, suggests you want this:
#selctDescription <> ''
AND t.[description] LIKE '%' + #selctDescription + '%'
A couple of thoughts for you...
Where you have '%' #selctDescription '%', you just need + between the strings to concatenate them. Your code will then work as is.
'%' + #selctDescription + '%'
Also, it's useful to note that you don't even need the CASE statement, as when the parameter is blank, you get '%%', which will still match everything.
This is useful to know because at present you have table fields on both sides of the LIKE statement. This will really hurt the optimiser. If I were to use CASE I'd be more tempted to stick to...
t.[description] LIKE (CASE WHEN #selctDescription = '' THEN '%' ELSE ('%' + #selctDescription + '%') END)
This has the benefit that the result of the CASE statement can be performed once, as a scalar value, prior to execution of the query. As opposed to being recalculated ever row.
And that said, it then becomes functionally identical to...
t.[description] LIKE ('%' + #selctDescription + '%')
Based on your first line and comments, you need to make the following select:
Select * from table
where field <> ''
and field like '%' + #selctDescription + '%'
But you must put the correct table and field and #selctDesciption must be text (char, nchar, varchar, nvarchar, ...).