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)
 [Resolved] String Values

Author  Topic 

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2009-06-02 : 09:52:33
If I have a string with value like "I'm the string" and I am trying to do an insert into a table I get error. Is there a way to insert this string without having to replace the "'" character? Could I use
'''I'm the string''', is this correct? Thank you.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-02 : 10:09:26
You can use a second single quote to mask it.
I''m the string


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2009-06-02 : 10:17:29
I was thinking doing the whole string as I never know what user might enter, this why I will be covered. So is my approach correct? Thank you.
Go to Top of Page

a.rameshk
Starting Member

19 Posts

Posted - 2009-06-02 : 10:18:57

Hi,
You can insert the value by SELECT.So that you can exactly get value like I'am the string.
CREATE TABLE STRINGS
(StValue VARCHAR(50))
GO
insert STRINGS
SELECT SUBSTRING(('''I''am the string'''),1,16)
go
select * from STRINGS
Go to Top of Page

vision99
Starting Member

14 Posts

Posted - 2009-06-02 : 10:22:51
Hi,

you can insert, In sqlserver Four single apostrophes equals one apostrophe output in the string.
Eg. Carlito's look like this:'Carlito'+''''+'s Way'

-Vision
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2009-06-02 : 10:43:41
In fact I am reading hundreds of records from one table and programatically inserting into another table (insert into ... select from ...). In the string (character field) there might be any invalid character or any number of invalid characters (no way to anticipate), so I need to look at the whole string and not each position withing the string, this is what I had in mind.

Exec('insert into mydataBase.dbo.JTABLE(
report_state, report_year, name,
Select zstate, ztyear, zname,
from AS400SRV_MSDASQL.VGSYS400


In this case "zname" could possible have any characters embedded.
Go to Top of Page

kira
Starting Member

17 Posts

Posted - 2009-06-02 : 10:53:04
SET QUOTED_IDENTIFIER OFF
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-02 : 11:39:53
If the data already are inserted with single quote, you don't have to worry!




E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2009-06-02 : 12:01:13
Here is a real example:

Sql code:
insert into .....
values values(" ..... "," & "'" & zname & "'" & ","......

Value of zname:
Don't

Renders error:
System.Data.OleDb.OleDbException: SQL0104: Token S was not valid. Valid tokens: ) ,.
Cause . . . . . : A syntax error was detected at token S. Token S is not a valid token.

Present work around:
zname = zname.Replace("'", " ")
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-02 : 12:05:47
use QUOTENAME()
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-06-03 : 02:15:06
Also refer
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/02/19/understanding-single-quotes.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -