Fuzzy (Like) Join not working - sql

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 + '%')

Related

SUBSTR inside Like statement

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) || '%'

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.

SELECT DISTINCT with COUNT

I want to count the number of patients that are not currently admitted (CurrentClinicalInfo = 'False'). However, there are multiple ClinicalInfos per patient. Therefore I need to select distinctively.
I have tried this code below but it comes with too many column leading to errors further on.
SELECT DISTINCT COUNT(*) AS TableLength
FROM PatientDemographics AS p LEFT OUTER JOIN
PatientClinicalinformation AS pc ON p.PatientID = pc.PatientID
WHERE (pc.PatientID IS NULL) AND (p.FirstName LIKE '%' + + '%') OR
(pc.PatientID IS NULL) AND (p.Surname LIKE '%' + + '%') OR
(p.FirstName LIKE '%' + + '%') AND (pc.CurrentClinicalInfo = 'False') OR
(p.Surname LIKE '%' + + '%
') AND (pc.CurrentClinicalInfo = 'False')
You want distinct Patients, correct?
SELECT COUNT(DISTINCT p.PatientID) AS TableLength
FROM PatientDemographics AS p
LEFT JOIN PatientClinicalinformation AS pc ON p.PatientID = pc.PatientID
WHERE pc.PatientID IS NULL AND p.FirstName LIKE '%' + #input + '%'
OR
pc.PatientID IS NULL AND p.Surname LIKE '%' + #input + '%'
OR
p.FirstName LIKE '%' + #input + '%' AND pc.CurrentClinicalInfo = 'False'
OR
p.Surname LIKE '%' + #input + '%' AND pc.CurrentClinicalInfo = 'False'

Finding a line break in a text string in a SQL table?

I was trying to find line breaks and carriage returns in a column in a SQL table and I am not sure about the syntax.
I tried:
SELECT foo FROM test
WHERE foo LIKE CHAR(10)
I didn't get any results even though I know that the table should return results. What am I doing wrong?
SELECT foo FROM test WHERE foo LIKE '%' + CHAR(10) + '%'
Edit: to find all various types of line endings you should probably just check both:
SELECT foo FROM test WHERE foo LIKE '%' + CHAR(10) + '%'
OR foo LIKE '%' + CHAR(13) + '%'
SELECT foo FROM Table WHERE foo LIKE '%' + CHAR(13)+CHAR(10) + '%'
Try
SELECT foo FROM test WHERE foo LIKE '%'+ CHAR(10)+'%'
Try
SELECT foo FROM test WHERE foo LIKE CONCAT('%', CHAR(10), '%')

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, ...).