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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Missing operand issue with single speechmark names

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_Agents
WHERE SWSurname IN ('D''Costa','O''Donnell');
Go to Top of Page

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
Go to Top of Page

mole999
Starting Member

49 Posts

Posted - 2014-10-13 : 11:37:08
maybe

Replace(SWSurname,'''',''), and just loose them totally

Mole
Go to Top of Page

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.
Go to Top of Page

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 LASWSurname
FROM 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.ChildrenID
WHERE (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.ChildsSurname

then my additional parameters that produce the dropdown box is visible in the attached pic.



Cheers for your help with this :)

Jim
Go to Top of Page

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.
Go to Top of Page

mole999
Starting Member

49 Posts

Posted - 2014-10-14 : 11:43:41
I think its here

dbo.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 readability

dbo.Tbll_SocialWorker as TSW which would then force changes

replace(TSW.SWSurname,'''',''),

I am only an amateur though :)


Mole
Go to Top of Page

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 again
J

Jim
Go to Top of Page

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 LASWSurname
FROM dbo.Tbl_Families
INNER JOIN dbo.Tbl_Placements As TbP
ON dbo.Tbl_Families.FamiliesID = TbP.FamiliesID
FULL OUTER JOIN dbo.Tbll_SocialWorker As TSW
ON dbo.Tbl_Families.SocialWorkerID = TSW.SocialWorkerID
FULL OUTER JOIN dbo.Tbl_Children As Tbc
FULL OUTER JOIN dbo.tbll_FamilyPlacementWorker
ON TbC.FpwID = dbo.tbll_FamilyPlacementWorker.FpwID
FULL OUTER JOIN dbo.Tbll_Agents AS TbA
ON TbC.AgencyCode = TbA.AgencyCode
ON TbP.ChildrenID = TbC.ChildrenID
FULL OUTER JOIN dbo.Vw_MaxChildLacPlan
ON TbC.ChildrenID = dbo.Vw_MaxChildLacPlan.ChildrenID
FULL OUTER JOIN dbo.Vw_JW_01
ON TbC.ChildrenID = dbo.Vw_JW_01.ChildrenID
FULL OUTER JOIN dbo.Vw_JW_02
ON TbC.ChildrenID = dbo.Vw_JW_02.ChildrenID
WHERE (
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
Go to Top of Page

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 11
Incorrect syntax near '.'.
Msg 102, Level 15, State 1, Line 43
Incorrect syntax near '>'.
Msg 156, Level 15, State 1, Line 51
Incorrect syntax near the keyword 'ELSE'.
Msg 156, Level 15, State 1, Line 59
Incorrect syntax near the keyword 'AS'.
Msg 156, Level 15, State 1, Line 66
Incorrect syntax near the keyword 'AS'.
Msg 102, Level 15, State 1, Line 78
Incorrect 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
Go to Top of Page

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 out

Server Management Studio should show where the issue is, just use the simple substitute first and see if that fixes the issue

I use Flyspeed SQL at times when building and http://www.dpriver.com/pp/sqlformat.htm for formatting layout

Mole
Go to Top of Page
   

- Advertisement -