Question : SQL Single quote messing up query results

In the query below there is a record in the database and the string value of the field called
TRXDSCRN has the value 'WON'T BE invoiced'.

As soon as the query hits that field on the record my query crashes. I tried inserting a replace command to get rid of the single quote but that did not work. How do I adjust this to get the query to execute successfully?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
SELECT  ltrim(jl.jobnumber) as jobnumber,jl.transnmbr,jl.mastertype,jl.masterid,pm.vendname,jl.debitamt,jl.crdtamnt,
VCHRNMBR = case isnull(pw.vchrnmbr,0) when 0 then case isnull(po.vchrnmbr,0) when 0 then ph.vchrnmbr else po.vchrnmbr end else pw.vchrnmbr end,
DOCTYPE  = case isnull(pw.doctype,0) when 0 then case isnull(po.doctype,0) when 0 then ph.doctype else po.doctype end else pw.doctype end,
DOCDATE  = case isnull(pw.docdate,0) when 0 then case isnull(po.docdate,0) when 0 then ph.docdate else po.docdate end else pw.docdate end,
POSTDATE = case isnull(pw.pstgdate,0) when 0 then case isnull(po.posteddt,0) when 0 then ph.posteddt else po.posteddt end else pw.pstgdate end,
--DOCNUMBR = case isnull(pw.docnumbr,0) when 0 then case isnull(po.docnumbr,0) when 0 then ph.docnumbr else po.docnumbr end else pw.docnumbr end,
DOCAMNT  = case isnull(pw.docamnt,0) when 0 then case isnull(po.docamnt,0) when 0 then ph.docamnt else po.docamnt end else pw.docamnt end,
TRXDSCRN = case isnull(pw.trxdscrn,0) when 0 then case isnull(po.trxdscrn,0) when 0 then replace(ph.trxdscrn,CHAR(39),'') else replace(po.trxdscrn,CHAR(39),'') end else replace(pw.trxdscrn,CHAR(39),'') end,
jl.docsource,jl.actnumst,gl.accatnum
FROM gl00100 gl, gl00102 glc, pm00200 pm, job_linker as jl
LEFT OUTER JOIN pm10000 as pw 
 ON jl.transnmbr = pw.vchrnmbr
LEFT OUTER JOIN pm20000 as po 
 ON jl.transnmbr = po.vchrnmbr
LEFT OUTER JOIN pm30200 as ph 
 ON jl.transnmbr = ph.vchrnmbr
where jl.mastertype='VEN' and len(rtrim(jl.jobnumber))>0 and jl.masterid = pm.vendorid and gl.actnumbr_1=jl.actnumst and gl.accatnum=glc.accatnum and gl.accatnum>=31
order by jl.transnmbr

Answer : SQL Single quote messing up query results

Random Solutions  
 
programming4us programming4us