Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using replace function with NULL strings

Status
Not open for further replies.

cawthor

Programmer
May 31, 2001
89
US
I've got an SQL Insert statement that inserts about 100 fields (1 record) into a table. Most of these fields are text, so it's possible they may contain single quotes. These quotes need to be escaped in the insert statement so I was hoping to just use the replace function...eg:

INSERT INTO Tab1 (Field1, Field2...etc) VALUES ('" & replace(field1, "'", "''") & "', '" & replace(field2, "'", "''") & "')"

The problem with this is that some of these fields may be NULL and the replace function blows up on a NULL value (Invalid use of Null: 'replace'). Do I have to do a condition on every field before my statement like:

if not ISNULL(field1) then
field1 = replace(field1, "'", "''")
end if

It just seems a hassle to do this 100 times for each field.

Thanks.
 
I dunno if this will work but you might try this:
replace((field1 & ""), "'", "''")

I don't think string concat will fail on a null, and if it doesnt, you'll have a proper empty string going into the Replace() function.
 
try this function

Code:
function fixsq(sfield)
  tmp=trim("" & sfield)
  tmp=replace(tmp,"'","''")
  fixsq=tmp
end function

INSERT INTO Tab1 (Field1, Field2...etc) VALUES ('" & fixsq(field1) & "', '" & fixsq(field2) & "')"
this will cover both the nulls and the escape quotes
 
Side note: this also replaces NULLs with ''. The difference is meaningless in some 99% of cases, but anyway...

------
heisenbug: A bug that disappears or alters its behavior when one attempts to probe or isolate it
schroedinbug: A bug that doesn't appear until someone reads source code and realizes it never should have worked, at which point the program promptly stops working for everybody until fixed.

[ba
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top