Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
jimtimber
Yak Posting Veteran
60 Posts |
Posted - 2014-10-13 : 09:48:07
|
Hi,I have set a parameter for a query (SELECT DISTINCT SWSurname from Tbl_Agents ORDER BY SWSurname) which works. However, we have 2 surnames that have an ' in the name (e.g. D'Costa, O'Donnell) which brings up the error message "SyntaxErrorException: Syntax error: Missing operand after 'Costa' operator.".Any ideas how to fix this would be great! :)Thanks.J Jim |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2014-10-13 : 10:24:59
|
quote: Originally posted by jimtimber Hi,I have set a parameter for a query (SELECT DISTINCT SWSurname from Tbl_Agents ORDER BY SWSurname) which works. However, we have 2 surnames that have an ' in the name (e.g. D'Costa, O'Donnell) which brings up the error message "SyntaxErrorException: Syntax error: Missing operand after 'Costa' operator.".Any ideas how to fix this would be great! :)Thanks.J Jim
The select statement you have shown should not cause any problems even if there are single quotes in the data.In general, if you want to use string literals that have single quotes in them, escape using another quote. For example:SELECT SWSurname FROM Tbl_AgentsWHERE SWSurname IN ('D''Costa','O''Donnell'); |
|
|
jimtimber
Yak Posting Veteran
60 Posts |
Posted - 2014-10-13 : 11:15:18
|
Hi James K, thanks for your response. What you did worked but it only brought through those 2 names. We have a list of about 300 SWs that this parameter turns into a drop down box. So I need to include what you suggested, alongside all the other names that don't have an ' in the name. Thanks again!Jim |
|
|
mole999
Starting Member
49 Posts |
Posted - 2014-10-13 : 11:37:08
|
maybeReplace(SWSurname,'''',''), and just loose them totallyMole |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2014-10-13 : 12:21:09
|
quote: Originally posted by jimtimber Hi James K, thanks for your response. What you did worked but it only brought through those 2 names. We have a list of about 300 SWs that this parameter turns into a drop down box. So I need to include what you suggested, alongside all the other names that don't have an ' in the name. Thanks again!Jim
As I had indicated in my original reply, the problem is not with the SELECT statement itself, but where you are using the results of the query; i.e., the code that is consuming the results of the select query. So you can try what mole999 suggested - which would remove the embedded single quotes, or you can try to double up the single quotes like this: SELECT REPLACE(SWSurname,'''','''''') AS SWSurname FROM Tbl_Agents Hard to say what you need without seeing how you are using the results of the query. |
|
|
jimtimber
Yak Posting Veteran
60 Posts |
Posted - 2014-10-14 : 04:57:27
|
Morning Mole and James, would I put that code in the main query or would that go in the parameter SQL? Our data is stored on a 3rd party system. I write the main queries, paste the SQL into their system and then can add extra parameters separately. My full code for what I am doing is this:SELECT TOP (100) PERCENT dbo.Tbl_Placements.FamiliesAgency, dbo.Tbl_Children.ChildrenID, dbo.Tbl_Children.AgencyCode, dbo.Tbll_Agents.AgencyName, dbo.Tbll_SocialWorker.SocialWorkerID, dbo.Tbll_SocialWorker.SWFirstname, dbo.Tbll_SocialWorker.SWSurname, dbo.Tbl_Children.ChildsSurname, dbo.Tbl_Children.ChildsFirstName, dbo.Tbl_Children.ChildsDateOfBirth AS DOB, dbo.Tbl_Children.ChildsAgeYears, dbo.Tbl_Families.ApplicantCode AS [FC(s)], dbo.Tbl_Children.TypeOfPlacement, dbo.Tbl_Placements.DateOfPlacement, dbo.Tbl_Placements.DatePlacementFinished, CASE WHEN ChildsAgeYears >= '16' THEN 'N/A' ELSE CASE WHEN ChildsAgeYears < '16' AND Vw_MaxChildLacPlan.Expr1 > Tbl_Placements.DateOfPlacement THEN CONVERT(varchar(10), Vw_MaxChildLacPlan.Expr1, 103) ELSE NULL END END AS [LAC Care Plan], CASE WHEN ChildsAgeYears < '16' THEN 'N/A' ELSE CASE WHEN ChildsAgeYears >= '16' AND Vw_JW_01.Expr1 > Tbl_Placements.DateOfPlacement THEN CONVERT(varchar(10), Vw_JW_01.Expr1, 103) ELSE NULL END END AS [Pathway Plan], CASE WHEN (SELECT MAX(ChildrenActionDate) AS Expr1 FROM dbo.Tbl_ChildrenProgress WHERE (ChildrenAction = N'Placement Plan') AND (ChildrenID = dbo.Tbl_Children.ChildrenID)) >= dbo.Tbl_Placements.DateOfPlacement THEN (SELECT MAX(ChildrenActionDate) AS Expr1 FROM dbo.Tbl_ChildrenProgress WHERE (ChildrenAction = N'Placement Plan') AND (ChildrenID = dbo.Tbl_Children.ChildrenID)) ELSE NULL END AS [Placement Plan], (SELECT MAX(ChildrenActionDate) AS Expr1 FROM dbo.Tbl_ChildrenProgress WHERE (ChildrenAction = N'Review') AND (ChildrenID = dbo.Tbl_Children.ChildrenID)) AS [Last Review], (SELECT MAX(ChildrenActionDate) AS Expr1 FROM dbo.Tbl_ChildrenProgress AS Tbl_ChildrenProgress_1 WHERE (ChildrenAction = N'Initial Health Assessment received') AND (ChildrenID = dbo.Tbl_Children.ChildrenID)) AS [Health Assessment], CASE WHEN ChildsAgeYears < '4' THEN 'N/A' ELSE CASE WHEN ChildsAgeYears >= '4' THEN CONVERT(varchar(10), (SELECT MAX(ChildrenActionDate) AS Expr1 FROM dbo.Tbl_ChildrenProgress WHERE (ChildrenAction = N'Personal Education Plan (PEP) / Individual Education Plan (IEP)') AND (ChildrenID = dbo.Tbl_Children.ChildrenID)), 103) ELSE NULL END END AS IndivPeps, dbo.tbll_FamilyPlacementWorker.FpwFirstname AS LASWFirstName, dbo.tbll_FamilyPlacementWorker.FpwSurname AS LASWSurnameFROM dbo.Tbl_Families INNER JOIN dbo.Tbl_Placements ON dbo.Tbl_Families.FamiliesID = dbo.Tbl_Placements.FamiliesID FULL OUTER JOIN dbo.Tbll_SocialWorker ON dbo.Tbl_Families.SocialWorkerID = dbo.Tbll_SocialWorker.SocialWorkerID FULL OUTER JOIN dbo.Tbl_Children FULL OUTER JOIN dbo.tbll_FamilyPlacementWorker ON dbo.Tbl_Children.FpwID = dbo.tbll_FamilyPlacementWorker.FpwID FULL OUTER JOIN dbo.Tbll_Agents ON dbo.Tbl_Children.AgencyCode = dbo.Tbll_Agents.AgencyCode ON dbo.Tbl_Placements.ChildrenID = dbo.Tbl_Children.ChildrenID FULL OUTER JOIN dbo.Vw_MaxChildLacPlan ON dbo.Tbl_Children.ChildrenID = dbo.Vw_MaxChildLacPlan.ChildrenID FULL OUTER JOIN dbo.Vw_JW_01 ON dbo.Tbl_Children.ChildrenID = dbo.Vw_JW_01.ChildrenID FULL OUTER JOIN dbo.Vw_JW_02 ON dbo.Tbl_Children.ChildrenID = dbo.Vw_JW_02.ChildrenIDWHERE (dbo.Tbl_Placements.DateOfPlacement IS NOT NULL) AND (dbo.Tbl_Placements.DatePlacementFinished IS NULL) AND (NOT (dbo.Tbl_Placements.TypeOfPlacement = 'respite house')) AND (NOT (dbo.Tbl_Placements.TypeOfPlacement LIKE '%Respite House - INTERNAL'))ORDER BY dbo.Tbl_Children.ChildsSurnamethen my additional parameters that produce the dropdown box is visible in the attached pic.Cheers for your help with this :)Jim |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2014-10-14 : 08:27:50
|
The code I posted earlier is all for SQL Server with no intervening third party software. What the third party system would do to the data you provide is known only to them. So you should ask the vendor of the software for advice on how to handle the embedded single quotes in strings. |
|
|
mole999
Starting Member
49 Posts |
Posted - 2014-10-14 : 11:43:41
|
I think its heredbo.Tbll_SocialWorker.SocialWorkerID, dbo.Tbll_SocialWorker.SWFirstname, dbo.Tbll_SocialWorker.SWSurname, so Replace(SWSurname,'''','')becomes replace(dbo.Tbll_SocialWorker.SWSurname,'''',''),I would be extremely tempted to make a change to the From Statement to lift readabilitydbo.Tbll_SocialWorker as TSW which would then force changes replace(TSW.SWSurname,'''',''),I am only an amateur though :)Mole |
|
|
jimtimber
Yak Posting Veteran
60 Posts |
Posted - 2014-10-14 : 11:47:25
|
Thanks James and Mole! James, I spoke with the 3rd party people, they didn't have a solution beyond adding "CONVERT (VARCHAR(10), dbo.Tbll_SocialWorker.SWSurname, 100)" to the query and the parameter query, but this didn't work.Mole, i'll give that a try in the morning and let you know the result :)Thanks againJJim |
|
|
mole999
Starting Member
49 Posts |
Posted - 2014-10-14 : 12:00:31
|
You could tidy and make it easier to read (if I have typed this properly in WORD SELECT TOP (100) PERCENT TbP.FamiliesAgency , TbC.ChildrenID , TbC.AgencyCode , TbA.AgencyName , TSW.SocialWorkerID , TSW.SWFirstname , TSW.SWSurname , TbC.ChildsSurname , TbC.ChildsFirstName TbC.ChildsDateOfBirth AS DOB , TbC.ChildsAgeYears , dbo.Tbl_Families.ApplicantCode AS [FC(s)] , TbC.TypeOfPlacement , TbP.DateOfPlacement , TbP.DatePlacementFinished, CASE WHEN ChildsAgeYears >= '16' THEN 'N/A' ELSE CASE WHEN ChildsAgeYears < '16' AND Vw_MaxChildLacPlan.Expr1 > Tbl_Placements.DateOfPlacement THEN CONVERT(VARCHAR(10), Vw_MaxChildLacPlan.Expr1, 103) ELSE NULL END END AS [LAC Care Plan], CASE WHEN ChildsAgeYears < '16' THEN 'N/A' ELSE CASE WHEN ChildsAgeYears >= '16' AND Vw_JW_01.Expr1 > Tbl_Placements.DateOfPlacement THEN CONVERT(VARCHAR(10), Vw_JW_01.Expr1, 103) ELSE NULL END END AS [Pathway Plan], CASE WHEN ( SELECT MAX(ChildrenActionDate) AS Expr1 FROM TbCProgress WHERE ( ChildrenAction = N'Placement Plan') AND ( ChildrenID = TbC.ChildrenID)) >= TbP.DateOfPlacement THEN ( SELECT MAX(ChildrenActionDate) AS Expr1 FROM TbCProgress WHERE ( ChildrenAction = N'Placement Plan') AND ( ChildrenID = TbC.ChildrenID)) ELSE NULL END AS [Placement Plan], ( SELECT MAX(ChildrenActionDate) AS Expr1 FROM TbCProgress WHERE ( ChildrenAction = N'Review') AND ( ChildrenID = TbC.ChildrenID)) AS [Last Review], ( SELECT MAX(ChildrenActionDate) AS Expr1 FROM TbCProgress AS Tbl_ChildrenProgress_1 WHERE ( ChildrenAction = N'Initial Health Assessment received') AND ( ChildrenID = TbC.ChildrenID)) AS [Health Assessment], CASE WHEN ChildsAgeYears < '4' THEN 'N/A' ELSE CASE WHEN ChildsAgeYears >= '4' THEN CONVERT(VARCHAR(10), ( SELECT MAX(ChildrenActionDate) AS Expr1 FROM TbCProgress WHERE ( ChildrenAction = N'Personal Education Plan (PEP) / Individual Education Plan (IEP)') AND ( ChildrenID = TbC.ChildrenID)), 103) ELSE NULL END END AS IndivPeps, dbo.tbll_FamilyPlacementWorker.FpwFirstname AS LASWFirstName, dbo.tbll_FamilyPlacementWorker.FpwSurname AS LASWSurnameFROM dbo.Tbl_FamiliesINNER JOIN dbo.Tbl_Placements As TbPON dbo.Tbl_Families.FamiliesID = TbP.FamiliesIDFULL OUTER JOIN dbo.Tbll_SocialWorker As TSWON dbo.Tbl_Families.SocialWorkerID = TSW.SocialWorkerIDFULL OUTER JOIN dbo.Tbl_Children As TbcFULL OUTER JOIN dbo.tbll_FamilyPlacementWorkerON TbC.FpwID = dbo.tbll_FamilyPlacementWorker.FpwIDFULL OUTER JOIN dbo.Tbll_Agents AS TbAON TbC.AgencyCode = TbA.AgencyCodeON TbP.ChildrenID = TbC.ChildrenIDFULL OUTER JOIN dbo.Vw_MaxChildLacPlanON TbC.ChildrenID = dbo.Vw_MaxChildLacPlan.ChildrenIDFULL OUTER JOIN dbo.Vw_JW_01ON TbC.ChildrenID = dbo.Vw_JW_01.ChildrenIDFULL OUTER JOIN dbo.Vw_JW_02ON TbC.ChildrenID = dbo.Vw_JW_02.ChildrenIDWHERE ( TbP.DateOfPlacement IS NOT NULL)AND ( TbP.DatePlacementFinished IS NULL)AND ( NOT ( TbP.TypeOfPlacement = 'respite house'))AND ( NOT ( TbP.TypeOfPlacement LIKE '%Respite House - INTERNAL'))ORDER BY TbC.ChildsSurname Mole |
|
|
jimtimber
Yak Posting Veteran
60 Posts |
Posted - 2014-10-15 : 04:14:53
|
Hi Mole, The above brought back the following errors:Msg 102, Level 15, State 1, Line 11Incorrect syntax near '.'.Msg 102, Level 15, State 1, Line 43Incorrect syntax near '>'.Msg 156, Level 15, State 1, Line 51Incorrect syntax near the keyword 'ELSE'.Msg 156, Level 15, State 1, Line 59Incorrect syntax near the keyword 'AS'.Msg 156, Level 15, State 1, Line 66Incorrect syntax near the keyword 'AS'.Msg 102, Level 15, State 1, Line 78Incorrect syntax near ','.I need to learn to tidy up my SQL. I've only been doing it for 3 months so I've a lot to learn! :)Jim |
|
|
mole999
Starting Member
49 Posts |
Posted - 2014-10-15 : 11:29:34
|
I guess i was wrong in WORD, maybe a comma or such thats out ( TbC.ChildsFirstName ) is missing a comma, which pushes the rest outServer Management Studio should show where the issue is, just use the simple substitute first and see if that fixes the issueI use Flyspeed SQL at times when building and http://www.dpriver.com/pp/sqlformat.htm for formatting layoutMole |
|
|
|
|
|
|
|