convert Microsoft SQL Server specific query to ORACLE specific query - sql

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.

Related

How to use Case statement in SQL correctly

The below Select statement does NOT return a record for me:
Select 1:
SELECT
Case When rcdr.PolicyNumber like '' + bmpd.PaymentReferenceNumber + '%'
Then 'Adc' else 'Exceed' end as System
FROM RcDetailRecords rcdr
join Bil_ManualPaymentDetails bmpd
on rcdr.PolicyNumber like '' + bmpd.PaymentReferenceNumber + '%' + ''
Output:
System =
The below Select statement DOES return a record for me:
Select 2:
SELECT
Case When rcdr.PolicyNumber like '1234567890%'
Then 'Adc' else 'Exceed' end as System
FROM RcDetailRecords rcdr
join BilManualPaymentDetails bmpd
on rcdr.PolicyNumber like '1234567890%'
Output:
System = Adc
Am I missing a tick '' mark somewhere in the first Select ?
BilManualPaymentDetails Table:
PaymentReferenceNumber
123456789020161013025120
RcDetailRecords Table:
PolicyNumber
1234567890
1234567890 is not like 123456789020161013025120%, but 123456789020161013025120 is like 1234567890%
So it appears you have the fields switched, try:
SELECT
CASE WHEN bmpd.PaymentReferenceNumber LIKE '' + rcdr.PolicyNumber + '%'
THEN 'Adc'
ELSE 'Exceed'
END as System
FROM RcDetailRecords rcdr
JOIN Bil_ManualPaymentDetails bmpd
ON bmpd.PaymentReferenceNumber like '' + rcdr.PolicyNumber + '%' + ''

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

Adding a WHERE statement to an SQL query based on an IF statement

Issue: I only want to filter the PropertySearch value if there is one
I want to be able to have a dynamic SQL statement based on this.
I have added If #PropertySearch which filters from a textbox on the webform.
The search works up to -- If #PropertySearch <> '' -- and will work if I comment the code
--
If #PropertySearch <> ''
BEGIN
TblA.PropertyID LIKE '%' + #PropertySearch + '%' OR TblA.Propertyname LIKE '%' + #PropertySearch +' %'
END
--
I want to only filter the PropertyID/PropertySearch when there is a #PropertySearch.
I have looked at having 'AND' after 'BEGIN' as well as Nested tables but am struggling
If #RegionID = 1 --then -- Head office users
BEGIN
SELECT TblA.PropertyID as PId, TblA.Propertyname as PNa, TblB.FireSafetyDisplay as FireSafety1, TblB.SlipsandTripsDisplay as SaT
FROM TbPropertyDetails as TblA inner join TbPropertyDetailsSafeguarding as TblB on TblA.PropertyID = TblB.PropertyID
WHERE TblA.RegionID > 0
If #PropertySearch <> ''
BEGIN
TblA.PropertyID LIKE '%' + #PropertySearch + '%' OR TblA.Propertyname LIKE '%' + #PropertySearch +' %'
END
END
WHERE TblA.RegionID > 0 AND (#PropertySearch = ''
OR TblA.PropertyID LIKE '%' + #PropertySearch + '%' OR TblA.Propertyname LIKE '%' + #PropertySearch +' %')

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'

T-SQL: CASE statement in WHERE?

I have the following WHERE clause in my SQL query currently:
WHERE c.Town LIKE '%' + #Town + '%'
What I want do is make the first '%' optional - so if the user has selected to just filter by records that just start with the given text, the WHERE would be
WHERE c.Town LIKE #Town + '%'
I assumed the simplest way to do this would be CASE statements, but I'm having no joy as it seems the syntax is incorrect. Can I get any pointers? Here's the CASE statement I was trying:
WHERE c.Town LIKE (CASE WHEN #FilterOptions = 1 THEN '%' ELSE '') + #Town + '%'
You missed the END:
WHERE c.Town LIKE (CASE WHEN #FilterOptions = 1 THEN '%' ELSE '' END) + #Town + '%'
A CASE must end with an END. Try
WHERE c.Town LIKE (CASE WHEN #FilterOptions = 1 THEN '%' ELSE '' END) + #Town + '%'
Case should have end.
I think the below statement can help you.
WHERE c.Town LIKE (CASE WHEN #FilterOptions = 1 THEN '%' ELSE '' END) + #Town + '%