Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Single quotes in a value...

Author  Topic 

VincentFrandsen
Starting Member

39 Posts

Posted - 2007-03-22 : 05:36:10
i have the following code @ value is my problem... i have to place it in single quotes but how do i get aroun the problem i have when my data has single quotes in its values...

i.e. @value = "O'Brian"

DECLARE @SQL as Varchar(max)
set @sql = 'UPDATE schBank.TblLeads' + convert(varchar(10), @CampaignId) +
' SET [' + convert(varchar(10), @FieldId) + '] = ''' + @Value +
''' WHERE [TransactionId] = ' + convert(varchar(10), @TransactionId)
PRINT @SQL
EXECUTE ('' + @sql + '')


Vincent Fradnsen

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-03-22 : 05:44:03
quote:
Originally posted by VincentFrandsen

i have the following code @ value is my problem... i have to place it in single quotes but how do i get aroun the problem i have when my data has single quotes in its values...

i.e. @value = "O'Brian"

DECLARE @SQL as Varchar(max)
set @sql = 'UPDATE schBank.TblLeads' + convert(varchar(10), @CampaignId) +
' SET [' + convert(varchar(10), @FieldId) + '] = ''' + Replace(@Value, '''', '''''') +
''' WHERE [TransactionId] = ' + convert(varchar(10), @TransactionId)
PRINT @SQL
EXECUTE ('' + @sql + '')


Vincent Fradnsen



Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-22 : 05:48:16
Or use QUOTENAME function.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

VincentFrandsen
Starting Member

39 Posts

Posted - 2007-03-22 : 05:50:22
Thanks Harsh that works like a BOMB...

Vincent Fradnsen
Go to Top of Page

VincentFrandsen
Starting Member

39 Posts

Posted - 2007-03-22 : 05:53:54
Hi Peso Looked it up on BOL but i dont really understand how it works... please could you give me a better example...

Vincent Fradnsen
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-22 : 06:02:16
try this

select QUOTENAME('test')



KH

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-22 : 06:17:10
If you want it as [].
If you want single quote ' use QUOTENAME( value , CHAR(39))


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

VincentFrandsen
Starting Member

39 Posts

Posted - 2007-03-22 : 09:00:09
thanks

Vincent Fradnsen
Go to Top of Page
   

- Advertisement -