×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • 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!

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

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

ANSI_SQL FAQ

SQL String queries

How to Query Strings by cdukes
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