Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...This forum is the most helpful site I've ever used. I used to use Deja.com; but, this site is better - hands down!..."

Geography

Where in the world do Tek-Tips members come from?

ANSI_SQL FAQ

SQL String queries

How to Query Strings
Posted: 6 Jul 00

I have answered many questions about syntax errors when trying to query for strings in SQL and VB.
The following is my suggestion if you are having problems:

1) The string you are searching for must always be enclosed in single quotes :

SQL:

SELECT Name from MyTable where Surname = 'Fred'

VB:

mystr = "SELECT Name from MyTable where Surname = 'Fred'"

2) If the string you are searching for Contains a single quote then the single quote must be doubled

SQL:

SELECT Name from MyTable where Surname = 'Fred''s'

VB:

mystr = "SELECT Name from MyTable where Surname = 'Fred''s'"


3) If you are using variables in VB and the string you are searching for Contains a single quote then the single quote must STILL be doubled. This can be accomplished by writting a VB function that replaces any single quote with two single quotes. The SQL string must still be enclosed in single quotes:

VB:
mySearchstring = "Fred's"
NewString = ConvertQuotes(mySearchstring)

' Converts NewString into "Fred''s"

mystr = "SELECT Name from MyTable where Surname = '" & NewString & "'"

4) The above is also true when calling SQL stored procedures:

SQL:

CREATE PROC GetName( @SearchString  VARCHAR(50) )
AS
SELECT Name from MyTable where Surname = @SearchString

GO

CALL GetName ('Fred')
CALL GetName ('Fred''s')


Hope this is usefull,


Chris Dukes

Back to ANSI_SQL FAQ Index
Back to ANSI_SQL Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close