Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 15th, 2006, 08:03 PM
Authorized User
 
Join Date: Jun 2003
Posts: 90
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to tdaustin Send a message via Yahoo to tdaustin
Default Apostraphe's in SQL statement

Hi all,

I recon this is really simple but its a problem that keeps coming up and i use a replace function to remove them, but this time i need apostrophes in my data field

When you insert via SQL if a ' is contained withing a text value while inserting into a database it will break the sql statement as it thinks its the end of the data for that data field.

Is there a way i can wrap my fields to not kill the sql statement if a ' is contained within a variable.

eg. say i was inserting a company name
Code:
'local variable
CompanyName = "Tim's Company"

strAdd = "INSERT INTO tblCompany SET CompanyName = '"& CompanyName &"'"
objConn.Execute(strAdd)
FYI: im using mySQL database

Cheers

Tim

TDA
__________________
TDA
 
Old January 15th, 2006, 08:16 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

I run this function around ALL text fields on insert or update, it will fix your problem:

Function StoreText(theText)
    StoreText = ""
    on error resume next
    StoreText = CStr(theText)
    if (len(StoreText) > 0) Then
     StoreText = Replace(StoreText, """", """, 1, -1, 1)
     StoreText = Replace(StoreText, "'", "''", 1, -1, 1)
     StoreText = Replace(StoreText, vbCrLf, "<BR>", 1, -1, 1)
    end if
End Function

Use it like so:
strAdd = "INSERT INTO tblCompany SET CompanyName = '"& trim(storeText(CompanyName)) &"';"
objConn.Execute(strAdd)

FYI: running the trim function and using trailing semi colons are also very good habbits to get into

Wind is your friend
Matt
 
Old January 15th, 2006, 09:04 PM
Authorized User
 
Join Date: Jun 2003
Posts: 90
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to tdaustin Send a message via Yahoo to tdaustin
Default

Thanks matt, thats definately a consistant and good way of doing it!

Also from your example learnt about the Start, Count, Compare parameters of the replace function (had no idea).

thanks again

Tim :)

TDA
 
Old January 15th, 2006, 09:50 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

You are in Melbourne, what company do you work for? I am in Sydney working for the Framegroup

Wind is your friend
Matt
 
Old January 16th, 2006, 12:46 AM
Authorized User
 
Join Date: Jun 2003
Posts: 90
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to tdaustin Send a message via Yahoo to tdaustin
Default

Hi Matt,

I freelance and sub contract to The Aussie Internet.

Tim :)

TDA





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with SQL Statement forumsUID SQL Server 2000 1 August 23rd, 2007 08:08 AM
convert a SQL Statement from MS Access to a SQL Corey Access 6 March 28th, 2007 12:33 PM
SQL Statement??? carswelljr SQL Server 2000 2 August 25th, 2006 12:40 PM
SQL Statement help johnman Access 1 October 22nd, 2004 10:20 AM
sql statement [email protected] Beginning VB 6 2 June 23rd, 2003 04:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.