Need to execute the SQL query with IF statement in Where clause.
SELECT
CASE
WHEN Isnull(CustGuarFlag ,'') = '' THEN DocName
ELSE DocName + ' - '+
CASE
WHEN CustGuarFlag= 'C' THEN 'Customer'
ELSE 'Guarantor'
END
END DocName,
ISNULL(A.DOCSTAGE, '') DOCSTAGE,
ISNULL(A.DOCREFNO,'') DOCREFNO,
ISNULL(CONVERT(VARCHAR, A.DOCREFDT, 103), '') DOCREFDT,
ISNULL(CONVERT(VARCHAR, A.RECDDT, 103), '') RECDDT ,
ISNULL(A.DOCVALUE, 0) DOCVALUE ,
ISNULL(CONVERT(VARCHAR, A.DOCVLDUPTO, 103), '') DOCVLDUPTO,
a.pk_id AS AppDocpk,
b.pk_id AS Docpk,
CASE
WHEN Isnull(a.DocVerifiedStatus, '') = 'null'
THEN ''
ELSE Isnull(a.DocVerifiedStatus,'')
END AS DocVerifiedStatus,
ISNULL(RejRemarks, '') AS RejRemarks,
ISNULL(KYCDocHdr_Fk, 0) AS KYCPk,
ISNULL(DocShrtDescr, '') AS DocShrtDescr
FROM
loln_apprvldoc A WITH(NOLOCK)
JOIN
LGEN_DOCUMENTS B WITH(NOLOCK) ON A.Doc_FK = B.PK_ID
LEFT OUTER JOIN
Lgen_KYCApprvlDoc c ON a.KYCDocHdr_Fk = c.PK_Id
WHERE
A.PRPSLNO = 'KATHU1602080002'
AND ISNULL(A.DocStat, '') = 'R'
AND ISNULL(A.DocVerifiedStatus, '') IN ('Rejection', '', 'null')
AND CASE
WHEN Isnull(c.DocVerifiedStatus, '') = 'A'
THEN c.Pk_id IS NOT NULL
ELSE c.Pk_id IS NULL
END
CASE doesn't belong in a WHERE clause. Replace that CASE clause with
(
(Isnull(c.DocVerifiedStatus,'')='A' AND c.Pk_id IS NOT NULL)
OR
(Isnull(c.DocVerifiedStatus,'')<>'A' AND c.Pk_id IS NULL)
)
Related
I am trying to get an if, then clause working in cloudera - along the lines:
if (appointment_purpose = '') and
appointment_purpose2 = '') and
appointment_type2 = '') and
appointment_type3 = '') and
appointment_type4 = '') and
appointment_type5 = '') and
discuss_other ' ') then
discuss_other = 0
else discuss_other = 1,
How do I get this to work?
If all appointment types and appointment purposes were empty, then discuss_other should also be empty, that is 0 - otherwise discuss_other should be 1
Try this:
SELECT (CASE WHEN appointment_purpose = ''
AND appointment_purpose2 = ''
AND appointment_type2 = ''
AND appointment_type3 = ''
AND appointment_type4 = ''
AND appointment_type5 = ''
AND discuss_other ' '
THEN 0
ELSE 1
) as discuss_other
FROM tableName
In SQL, "empty" typically means NULL. So, I wonder if you really intend one of these constructs:
select (case when appointment_purpose is null and
appointment_purpose2 is null and
appointment_type2 is null and
appointment_type3 is null and
appointment_type4 is null and
appointment_type5 is null and
discuss_other is null
then 0 else 1
end) as discuss_other
Because one of the values has a space, I wonder if you want to handle both spaces and null values:
select (case when (appointment_purpose is null or replace(appointment_purpose, ' ', '') = '') and
(appointment_purpose2 is null or replace(appointment_purpose2, ' ', '') = '') and
(appointment_purpose3 is null or replace(appointment_purpose3, ' ', '') = '') and
(appointment_purpose4 is null or replace(appointment_purpose4, ' ', '') = '') and
(appointment_purpose5 is null or replace(appointment_purpose5, ' ', '') = '') and
(discuss_other is null or replace(discuss_other, ' ', '') = '')
then 0 else 1
end) as discuss_other
Finally, multiple columns in the same table that are acting like an array is usually a bad sign. Normally, this suggests that you want a junction table.
hi i am a noob programmer.
I am learning Hibernate now. And I am frustrated by
SELECT COUNT(*) FROM zipcode a
WHERE (ISNULL( :sido , '') = '' OR a.sido = :sido) AND
(ISNULL( :sigungu , '') = '' OR a.sigungu = :sigungu) AND
(ISNULL( :eupmyun , '') = '' OR a.eupmyun = :eupmyun) AND
(ISNULL( :roadName , '') = '' OR a.road_name = :roadName) AND
(ISNULL( :ri , '') = '' OR a.ri = :ri)
I can't make this sql query to hibernate....
Number num = (Number) session()
.createQuery(
"SELECT COUNT(*) FROM ZipCode a WHERE (ISNULL( :sido , '') = '' OR a.sido = :sido) AND (ISNULL( :sigungu , '') = '' OR a.sigungu = :sigungu) AND (ISNULL( :eupmyun , '') = '' OR a.eupmyun = :eupmyun) AND(ISNULL( :roadName , '') = '' OR a.roadName = :roadName) AND(ISNULL( :ri , '') = '' OR a.ri = :ri) ")
.setParameter("sido", sido).setParameter("sigungu", sigungu).setParameter("eupmyun", eupmyun).setParameter("roadName", roadName).setParameter("ri", ri)
.list();
I try this sql...
I am using Postgresql 9.4 and hibernate 5.0.*
Please help me
1) Instead of createQuery use createSQLQuery as below:
Query query = session.createSQLQuery("SELECT COUNT(*) FROM zipcode a
WHERE (ISNULL( :sido , '') = '' OR a.sido = :sido) AND
(ISNULL( :sigungu , '') = '' OR a.sigungu = :sigungu) AND
(ISNULL( :eupmyun , '') = '' OR a.eupmyun = :eupmyun) AND
(ISNULL( :roadName , '') = '' OR a.road_name = :roadName) AND
(ISNULL( :ri , '') = '' OR a.ri = :ri) ");
Number number =(Number)query.list().get(0);
createQuery - is a HQL query whereas createSQLQuery is a native db query.
Note: make sure the query is in one line else you need to add ('+') concat operator.
I have a pretty complex query that uses the 'XML for path' to return an XML. Now I need this query as a regular T-SQL query, but I have great difficulty converting this. I have searched for a tool or another topic that can convert this or give more information how to easily transform this query to a regular t-sql query. Any help would be greatly appreciated!
SELECT
UPPER([dataareaid]) AS Company,
accountnum AS Code,
name + ' [' +
CASE ENTERPRISENUMBER
WHEN 'BTW BE 0000.000.097'
THEN TMMONATIONALREGISTRATIONNUMBER
ELSE
CASE countryregionid
WHEN 'BE'
THEN ENTERPRISENUMBER
ELSE countryregionid + VATNUM
END
END + ']' AS Name,
'false' AS PaymentDenied,
PAYMTERMID AS PaymentTermCode,
currency AS Currency,
AX_TMM.dbo.IS_ACCOUNT_ACTIVE(dataareaid,accountnum) AS Active,
--recid AS Id,
'email' as OrderFormat,
'efinance#test.be' as OrderEmail,
'nl-NL' as [Language],
CASE ENTERPRISENUMBER
WHEN 'BTW BE 0000.000.097'
THEN REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TMMONATIONALREGISTRATIONNUMBER, '.', ''), ',', ''),'-',''),' ', ''),'BTW', '')
ELSE
CASE countryregionid
WHEN 'BE'
THEN REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(ENTERPRISENUMBER, '.', ''), ',', ''),'-',''),' ', ''),'BTW', '')
ELSE REPLACE(REPLACE(REPLACE(REPLACE(REPLACE((countryregionid + VATNUM), '.', ''), ',', ''),'-',''),' ', ''),'BTW', '')
END
END AS [SupplierIdentifiers/SupplierIdentifier/value],
'PartyId' AS [SupplierIdentifiers/SupplierIdentifier/key],
CASE ENTERPRISENUMBER
WHEN 'BTW BE 0000.000.097'
THEN REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TMMONATIONALREGISTRATIONNUMBER, '.', ''), ',', ''),'-',''),' ', ''),'BTW', '')
ELSE
CASE countryregionid
WHEN 'BE'
THEN REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(ENTERPRISENUMBER, '.', ''), ',', ''),'-',''),' ', ''),'BTW', '')
ELSE REPLACE(REPLACE(REPLACE(REPLACE(REPLACE((countryregionid + VATNUM), '.', ''), ',', ''),'-',''),' ', ''),'BTW', '')
END
END AS [AdditionalDatas/AdditionalData/value],
'PartyId' AS [AdditionalDatas/AdditionalData/name],
(SELECT
accountnum AS [Bban],
ACCOUNTID AS [Name],
name AS [AdditionalData1],
ENTERPRISENUMBER AS [AdditionalData2],
bankiban AS [Iban],
swiftno AS [Swift],
AX_TMM.dbo.IS_BANKACCOUNT_DEFAULT(dataareaid,vendaccount,accountid) AS IsDefault
--vendtable.recid AS Supplierid
FROM
vendbankaccount
WHERE
vendaccount = vendtable.accountnum
and DATAAREAID = vendtable.DATAAREAID
and (expirydate = '1900-01-01 00:00:00.000' or expirydate > getdate())
FOR xml path('SupplierBankAccount'), TYPE) AS [BankAccounts],
(SELECT (SELECT
'' AS [ExternalCode],
'PostalAddress' AS [Type],
'Maatschappelijke zetel ' + SMARTMDCMASTERACCOUNTTABLE.NAME AS [Name],
'true' AS [IsDefault],
maaccount + vendtable.DATAAREAID AS [ExternalIdentifier],
(SELECT (SELECT
'STREET_NAME' AS [AddressPartKey],
street AS [AddressPartText]
FROM
SMARTMDCMASTERACCOUNTTABLE
LEFT OUTER JOIN ADDRESSCOUNTRYREGION
ON ADDRESSCOUNTRYREGION.ISOCODE = SMARTMDCMASTERACCOUNTTABLE.country
AND ADDRESSCOUNTRYREGION.DATAAREAID = SMARTMDCMASTERACCOUNTTABLE.DATAAREAID
WHERE
maaccount = vendtable.accountnum
FOR xml path('SupplierAddressPart'), TYPE),
(SELECT
'POSTAL_ZONE' AS [AddressPartKey],
zipcode AS [AddressPartText]
FROM
SMARTMDCMASTERACCOUNTTABLE
WHERE
maaccount = vendtable.accountnum
FOR xml path('SupplierAddressPart'), TYPE
),
(SELECT
'CITY_NAME' AS [AddressPartKey],
city AS [AddressPartText]
FROM
SMARTMDCMASTERACCOUNTTABLE
WHERE
maaccount = vendtable.accountnum
FOR xml path('SupplierAddressPart'), TYPE
),
(SELECT
'COUNTRY' AS [AddressPartKey],
ADDRESSCOUNTRYREGION.NAME AS [AddressPartText]
FROM
SMARTMDCMASTERACCOUNTTABLE
LEFT OUTER JOIN ADDRESSCOUNTRYREGION
ON ADDRESSCOUNTRYREGION.ISOCODE = SMARTMDCMASTERACCOUNTTABLE.country
AND ADDRESSCOUNTRYREGION.DATAAREAID = SMARTMDCMASTERACCOUNTTABLE.DATAAREAID
WHERE
maaccount = vendtable.accountnum
FOR xml path('SupplierAddressPart'), TYPE
)
FOR xml path('AddressParts'), TYPE)
FROM
SMARTMDCMASTERACCOUNTTABLE
LEFT OUTER JOIN ADDRESSCOUNTRYREGION
ON ADDRESSCOUNTRYREGION.ISOCODE = SMARTMDCMASTERACCOUNTTABLE.country
AND ADDRESSCOUNTRYREGION.DATAAREAID = SMARTMDCMASTERACCOUNTTABLE.DATAAREAID
WHERE
maaccount = vendtable.accountnum
FOR xml path('SupplierAddress'), TYPE) FOR xml path('SupplierAddresses'), TYPE)
FROM vendtable
WHERE DATAAREAID = 'TMM'
FOR xml path('Supplier'), ROOT ('DocumentElement')
I have a select that fetches some data for me.
I'm trying to make a query that shows me that data but if it doesn't exist it will show me the first 5 columns with data that I provided. Here is the query
DECLARE #miesiac INT = 1
DECLARE #opis VARCHAR(30) = '';
WITH cte
AS ( SELECT DISTINCT
ROW_NUMBER() OVER ( PARTITION BY emp_name,
doc_documentdate ORDER BY doc_documentdate ) s
,emp_name Kod
,emp_id id
,Emp_Surname Nazwisko
,Emp_FirstName Imie
,ISNULL(CONVERT(VARCHAR(10), Doc_DocumentDate, 120), '') Data
,CASE WHEN doc_documentdate = ttc_date
AND ttc_isholiday = 0 THEN 'S'
WHEN doc_documentdate = ttc_date
AND ttc_isholiday = 6
OR ttc_isholiday = 7
OR ELM_DecimalColumn1 = 0 THEN 'W'
WHEN ELM_DecimalColumn1 IS NULL THEN 'w'
ELSE 'P'
END Typdnia
,ISNULL(CONVERT(VARCHAR(8), elm_datecolumn1, 108),
'00:00:00') GodzOd
,ISNULL(CONVERT(VARCHAR(8), elm_datecolumn2, 108),
'00:00:00') GodzDo
,CASE WHEN ELM_decimalcolumn1 > 0
THEN 'praca podstawowa'
ELSE 'brak'
END Strefa
,#opis Opis
,ISNULL(emp_shortstringcolumn2, 'brak') Wydzial
,ISNULL(ELM_ShortStringColumn5, '0') Wydzial_adres_wezla
,ISNULL(a.dic_svalue, ' brak') Projekt
,ISNULL(b.DIC_ShortStringColumn1, '1') Projekt_adres_wezla
FROM Employees
LEFT JOIN elements ON emp_id = ELM_EmpID
AND ELM_FflID = 200
LEFT JOIN documents ON elm_objectid = doc_id
AND Doc_DdfID = 2
LEFT JOIN dictionaries a ON ELM_IntColumn2 = a.DIC_ID
LEFT JOIN Dictionaries b ON elm_intcolumn2 = b.DIC_ID
LEFT JOIN ttscalendar ON ttc_date = Doc_DocumentDate
RIGHT JOIN dim_timeday ON doc_documentdate = tmd_fulldatekey
)
SELECT ISNULL(cte1.kod, '') Kod
,ISNULL(cte1.nazwisko, '') Nazwisko
,ISNULL(cte1.imie, '') Imie
,ISNULL(cte1.data, '') Data
,ISNULL(cte1.TypDnia, '') Typdnia
,ISNULL(cte1.GodzOd, '00:00') W1_GodzOd
,ISNULL(cte1.godzdo, '00:00') W1_GodzDo
,ISNULL(cte1.strefa, 'brak') W1_Strefa
,ISNULL(cte1.opis, '') W1_Opis
,ISNULL(cte1.wydzial, 'brak') W1_Wydzial
,ISNULL(cte1.wydzial_adres_wezla, '0') W1_Wydzial_adres_wezla
,ISNULL(cte1.Projekt, 'brak') W1_Projekt
,ISNULL(cte1.Projekt_adres_wezla, '1') W1_Projekt_adres_wezla
,ISNULL(cte2.GodzOd, '') W2_GodzOd
,ISNULL(cte2.godzdo, '') W2_GodzDo
,ISNULL(cte2.strefa, '') W2_Strefa
,ISNULL(cte2.opis, '') W2_Opis
,ISNULL(cte2.wydzial, '') W2_Wydzial
,ISNULL(cte2.wydzial_adres_wezla, '') W2_Wydzial_adres_wezla
,ISNULL(cte2.Projekt, '') W2_Projekt
,ISNULL(cte2.Projekt_adres_wezla, '') W2_Projekt_adres_wezla
,ISNULL(cte3.GodzOd, '') W3_GodzOd
,ISNULL(cte3.godzdo, '') W3_GodzDo
,ISNULL(cte3.strefa, '') W3_Strefa
,ISNULL(cte3.opis, '') W3_Opis
,ISNULL(cte3.wydzial, '') W3_Wydzial
,ISNULL(cte3.wydzial_adres_wezla, '') W3_Wydzial_adres_wezla
,ISNULL(cte3.Projekt, '') W3_Projekt
,ISNULL(cte3.Projekt_adres_wezla, '') W3_Projekt_adres_wezla
,ISNULL(cte4.GodzOd, '') W4_GodzOd
,ISNULL(cte4.godzdo, '') W4_GodzDo
,ISNULL(cte4.strefa, '') W4_Strefa
,ISNULL(cte4.opis, '') W4_Opis
,ISNULL(cte4.wydzial, '') W4_Wydzial
,ISNULL(cte4.wydzial_adres_wezla, '') W4_Wydzial_adres_wezla
,ISNULL(cte4.Projekt, '') W4_Projekt
,ISNULL(cte4.Projekt_adres_wezla, '') W4_Projekt_adres_wezla
,ISNULL(cte5.GodzOd, '') W5_GodzOd
,ISNULL(cte5.godzdo, '') W5_GodzDo
,ISNULL(cte5.strefa, '') W5_Strefa
,ISNULL(cte5.opis, '') W5_Opis
,ISNULL(cte5.wydzial, '') W5_Wydzial
,ISNULL(cte5.wydzial_adres_wezla, '') W5_Wydzial_adres_wezla
,ISNULL(cte5.Projekt, '') W5_Projekt
,ISNULL(cte5.Projekt_adres_wezla, '') W5_Projekt_adres_wezla
,ISNULL(cte6.GodzOd, '') W6_GodzOd
,ISNULL(cte6.godzdo, '') W6_GodzDo
,ISNULL(cte6.strefa, '') W6_Strefa
,ISNULL(cte6.opis, '') W6_Opis
,ISNULL(cte6.wydzial, '') W6_Wydzial
,ISNULL(cte6.wydzial_adres_wezla, '') W6_Wydzial_adres_wezla
,ISNULL(cte6.Projekt, '') W6_Projekt
,ISNULL(cte6.Projekt_adres_wezla, '') W6_Projekt_adres_wezla
FROM cte cte1
LEFT JOIN cte cte2 ON cte1.Kod = cte2.kod
AND cte1.data = cte2.data
AND cte2.s = 2
LEFT JOIN cte cte3 ON cte2.Kod = cte3.kod
AND cte2.data = cte3.data
AND cte3.s = 3
LEFT JOIN cte cte4 ON cte3.Kod = cte4.kod
AND cte3.data = cte4.data
AND cte4.s = 4
LEFT JOIN cte cte5 ON cte4.Kod = cte5.kod
AND cte4.data = cte5.data
AND cte5.s = 5
LEFT JOIN cte cte6 ON cte5.Kod = cte6.kod
AND cte5.data = cte6.data
AND cte6.s = 6
WHERE cte1.s = 1
AND MONTH(cte1.data) = #Miesiac
AND cte1.id = 7
ORDER BY cte1.data
,cte1.kod
Here is the data
ANJO_141 ANTONKIEWICZ JOLANTA 2016-01-22 S 00:00:00 00:00:00 brak NF 1.8 brak 1
ANJO_141 ANTONKIEWICZ JOLANTA 2016-01-23 W 07:00:00 12:00:00 praca podstawowa NF 1.8 brak 1
ANJO_141 ANTONKIEWICZ JOLANTA 2016-01-27 P 07:00:00 15:00:00 praca podstawowa NF 1.8 brak 1
ANJO_141 ANTONKIEWICZ JOLANTA 2016-01-28 W 00:00:00 00:00:00 brak NF 1.8 brak 1
I have the following query. I need Description to be set to the value of the long statement in the middle - the part beginning with CONVERT and ending with [Description]. The query I have now runs but the value of Description in the result is always NULL. Any ideas?
select s.SectionId,
s.Academic_Year [AcademicYear],
s.Academic_Term [AcademicTerm],
s.Academic_Session [AcademicSession],
s.Event_Id [EventId],
s.Event_Sub_Type [EventSubType],
s.Section [Section],
s.Event_Long_Name [EventLongName],
e.Description [EventDescription],
CONVERT(varchar(MAX), S.DESCRIPTION)
+ '<br /><br /><a href="http://www.bkstr.com/webapp/wcs/stores/servlet/booklookServlet?bookstore_id-1=044&term_id-1='
+ CASE S.ACADEMIC_TERM
WHEN 'SPRING' THEN 'SPRING'
WHEN 'SUMMER' THEN 'SUM'
WHEN 'FALL' THEN 'FALL'
WHEN 'J TERM' THEN 'J TERM'
ELSE 'ADV'
END
+ S.ACADEMIC_YEAR + '/' +
CASE S.ACADEMIC_SESSION
WHEN '01' THEN '1'
WHEN '02' THEN '2'
WHEN '03' THEN '3'
END
+ '&div-1=HILB&dept-1=' + SUBSTRING(S.EVENT_ID, 1, CHARINDEX(' ', S.EVENT_ID) - 1) + '&course-1=' + SUBSTRING(S.EVENT_ID, CHARINDEX(' ', S.EVENT_ID) + 1,
LEN(S.EVENT_ID)) + '§ion-1=' + CONVERT(varchar(2), S.SECTION) + '" target=' + '"_blank">View Book Information</a>' [Description],
cest.Medium_Desc [SubTypeDescription],
s.Credits [Credits],
cge.Medium_Desc [GeneralEd],
s.Start_Date [StartDate],
s.End_Date [EndDate],
s.Max_Participant [MaximumParticipants],
s.Adds [AddCount],
s.Wait_List [WaitlistCount],
s.Sec_Enroll_Status [EnrollmentStatus],
o.Org_Name_1 [CampusName],
cp.Medium_Desc [ProgramDescription],
cc.Medium_Desc [CollegeDescription],
cd.Medium_Desc [DepartmentDescription],
ccm.Medium_Desc [CurriculumDescription],
ccl.Medium_Desc [ClassLevelDescription],
nt.Nontrad_Med_Name [NonTraditionalDescription],
cpn.Medium_Desc [PopulationDescription],
case s.Other_Org when 'N' then 0 else s.Other_Org_Part end as [CampusOtherLimit],
case s.Other_Program when 'N' then 0 else s.Other_Program_Part end as [ProgramOtherLimit],
case s.Other_College when 'N' then 0 else s.Other_College_Part end as [CollegeOtherLimit],
case s.Other_Department when 'N' then 0 else s.Other_Dept_Part end as [DepartmentOtherLimit],
case s.Other_Curriculum when 'N' then 0 else s.Other_Curric_Part end as [CurriculumOtherLimit],
case s.Other_Class_Level when 'N' then 0 else s.Other_CLevel_Part end as [ClassLevelOtherLimit],
case s.Other_NonTrad when 'N' then 0 else s.Other_NonTrad_Part end as [NonTraditionalOtherLimit],
case s.Other_Population when 'N' then 0 else s.Other_Pop_Part end as [PopulationOtherLimit],
s.Other_Org_Add as [CampusOtherRegistered],
s.Other_Program_Add as [ProgramOtherRegistered],
s.Other_College_Add as [CollegeOtherRegistered],
s.Other_Dept_Add as [DepartmentOtherRegistered],
s.Other_Curr_Add as [CurriculumOtherRegistered],
s.Other_CLevel_Add as [ClassLevelOtherRegistered],
s.Other_Nontrad_Add as [NonTraditionalOtherRegistered],
s.Other_Pop_Add as [PopulationOtherRegistered],
case s.Registration_Type when 'TRAD' then 0 else 1 end as [IsConEd],
cat.Medium_Desc [TermDescription],
cas.Medium_Desc [SessionDescription],
s.Credit_Type [DefaultCreditType],
cct.Medium_Desc [CreditTypeDescription]
from sections s
left join code_eventsubtype cest on cest.code_value_key = s.event_sub_type
left join code_generaled cge on cge.code_value_key = s.general_ed
left join event e on e.event_id = s.event_id
left join organization o on o.org_code_id = s.org_code_id
left join code_program cp on cp.code_value_key = s.program
left join code_college cc on cc.code_value_key = s.college
left join code_department cd on cd.code_value_key = s.department
left join code_curriculum ccm on ccm.code_value_key = s.curriculum
left join code_classlevel ccl on ccl.code_value_key = s.class_level
left join nontraditional nt on nt.nontrad_program = s.nontrad_program
left join code_population cpn on cpn.code_value_key = s.population
left join code_acaterm cat on cat.code_value_key = s.academic_term
left join code_acasession cas on cas.code_value_key = s.academic_session
left join code_acacredittype cct on cct.code_value_key = s.credit_type
One of your inputs must be NULL.
You can probably solve this by identifying which it is and using COALESCE or ISNULL:
COALESCE(x, '')
ISNULL(x, '')
COALESCE is the standard ANSI way, ISNULL gives slightly better performance (although the difference is probably insignificant in most cases).