From 930b074ff588fe11cc76f215accf5d4d1822d36f Mon Sep 17 00:00:00 2001 From: Pradeep Kumar Voorukonda Date: Tue, 30 Apr 2024 09:43:05 +0100 Subject: [PATCH] Fix invalid chars on Course Titles --- .../Scripts/Script.PostDeploy.sql | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/Dfc.CourseDirectory.Database/Scripts/Script.PostDeploy.sql b/src/Dfc.CourseDirectory.Database/Scripts/Script.PostDeploy.sql index 00e05c332..1cff02b41 100644 --- a/src/Dfc.CourseDirectory.Database/Scripts/Script.PostDeploy.sql +++ b/src/Dfc.CourseDirectory.Database/Scripts/Script.PostDeploy.sql @@ -8621,3 +8621,60 @@ WHEN NOT MATCHED ) WHEN NOT MATCHED BY SOURCE THEN DELETE; + +--- FIND INVALID CHARS AND REPLACE WITH VALID CHARS ---------------- +WHILE (EXISTS( Select CourseId,CourseRunId + from Pttcd.CourseRuns + where CourseName like '%&#%' and CourseRunStatus = 1)) +BEGIN + With cte as ( + Select coursename,courseid,courserunid, + patindex('%&#%',CourseName) as [FirstPosition], + substring(CourseName,patindex('%&#%',CourseName),1) as [FirstInvalidCharacter], + ascii(substring(CourseName,patindex('%&#%',CourseName),1)) as [FirstASCIICode] + from Pttcd.CourseRuns + where CourseName like '%&#%' and CourseRunStatus = 1), + + cte2 as( + Select *, + substring(CourseName,FirstPosition,len(CourseName)-1) as PartialCourseName + from cte), + + DataWithPos as ( + Select *, patindex('%;%',PartialCourseName) as [LastPosition], + substring(PartialCourseName,patindex('%;%',PartialCourseName),1) as [LastInvalidCharacter], + ascii(substring(PartialCourseName,patindex('%;%',PartialCourseName),1)) as [LastASCIICode] + from cte2), + + DataWithIs as ( + Select *,substring(CourseName,FirstPosition,LastPosition) AS [InvalidString] from DataWithPos), + + DatawithChar as (Select *, + CASE WHEN InvalidString = '–' OR InvalidString = '–' THEN + '-' + WHEN InvalidString = '’' OR InvalidString = ''' OR InvalidString = '’' OR InvalidString = ''' THEN + '''' + WHEN InvalidString = '&' THEN + '&' + WHEN InvalidString = '+' THEN + '+' + WHEN InvalidString = ' ' THEN + '' + WHEN InvalidString = ' ' THEN + ' ' + END AS CharToReplace + from DataWithIs) + UPDATE CR + SET CR.COURSENAME = REPLACE(CR.CourseName,DC.InvalidString,DC.CharToReplace) + FROM Pttcd.CourseRuns as CR + JOIN DataWithChar as DC ON CR.CourseId = DC.CourseId AND CR.CourseRunId = DC.CourseRunId +END + + +UPDATE FC + SET FC.COURSENAME = CR.COURSENAME + FROM Pttcd.FindACourseIndex AS FC + JOIN Pttcd.CourseRuns as CR ON CR.CourseId = FC.CourseId AND CR.CourseRunId = FC.CourseRunId + WHERE FC.CourseName != CR.CourseName + +