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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

' as a varchar 3

Status
Not open for further replies.

boligoma

IS-IT--Management
Feb 11, 2003
31
MX
Hi, I have a real dumm question:

I'm trying tu put a value ' inside a varchar, like this example:

declare @hola varchar(15)

set @hola= "'hola"

so thay the value of @hola most be 'hola (with the apostrophe)

the Query analizer returns me the next error:

Invalid column name ''hola'.

how I can use it?

Thanks!
 
Don't use double quotes, use two single quotes instead.

declare @hola varchar(15)

set @hola= '''hola'

print @hola

Tim
 
Tim gives you the solution, but here's an explanation.

To make a single quote (') be recognized as that, you need to put a single quote with it. So this is what you want:
'hola
you need this to make it a VARCHAR (string):
''hola'
and this to make the single quote be returned:
'''hola'


As for the error message you got. You are probably SELECTing on 'hola and not on the variable @hola.

SELECT @hola
or as Tim uses:
PRINT @hola

-SQLBill
 
Thank you for the explanation, I saw it but I never understood it until know...
 
I have always just remembered it as you need 2 ' to make 1 ' when you are building a string (as opposed to one that's already in a string variable) that you intend to pass to the SQL engine.
Damn, no matter how you explain it, it sounds weird. :)
-Karl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top