This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Replace single quote in sql server
So I'm trying to use the following code:
UPDATE TableName
SET FieldName = REPLACE(FieldName, 'SearchFor', 'ReplaceWith');
It works wonderfully for what I need to do, except for the fact that what I need to search for is "valid driver's license". That apostrophe doesn't seem to agree with the code. I tried doing this:
'valid driver''s license'
...but that doesn't seem to work either. I'm getting the following error:
Argument data type text is invalid for argument 1 of replace function.
If anyone has dealt with this before, I would love some help! This would save me so much time, rather than updating each record by hand. -__-
Ellie
The error tells you exactly what the problem is. FieldName is a column of type text, which doesn't work with the REPLACE function. Try casting the first param as VARCHAR(MAX) first, and it should work. IE:
UPDATE TableName
SET FieldName = REPLACE(CAST(FieldName AS VARCHAR(MAX)), 'SearchFor', 'ReplaceWith');
That's funny. I just ran into this exact thing a few minutes ago. It turns out I had to change this:
UPDATE TableName
SET FieldName = REPLACE(FieldName, 'SearchFor', 'ReplaceWith');
to this:
UPDATE TableName
SET FieldName = REPLACE(cast(FieldName as varchar(5000)), 'SearchFor', 'ReplaceWith');
You must escape the single quote with a slash.
UPDATE TableName
SET FieldName = REPLACE(FieldName, 'Isn\'t', 'Is not');
Related
I have an nvarchar field in my database called CatCustom which contains comma-separated 5-character codes. It can contain as little as one code, or as many as 20 codes, separated by commas.
Right now, I use this query to add a new 5-character code to the field in given records (in this case the new code is LRR01):
UPDATE dbo.Sources
SET CatCustom = CONCAT_WS(', ', RTRIM(CatCustom), 'LRR01')
WHERE SourceID IN (1,2,3,4,5,8,9,44,63,45,101,102,222,344)
I need to add to this though: I need the record to be updated only if that 5-character code doesn't already exist somewhere in the CatCustom field, to ensure that code is not in there more than once.
How would I accomplish this?
EDIT: I really don't understand how this can be considered a duplicate of the suggested thread. This is a VERY specific case and has nothing to do with creating stored procedures and or variables. The alleged duplicated thread does not really help me - sorry.
Use STRING_SPLIT function to split the comma separated list and then add Not Exist condition in the WHERE clause like below
UPDATE dbo.Sources
SET CatCustom = CONCAT_WS(', ', RTRIM(CatCustom), 'LRR01')
WHERE SourceID IN (1,2,3,4,5,8,9,44,63,45,101,102,222,344)
AND NOT EXISTS (SELECT 1 FROM STRING_SPLIT(CatCustom, ',') where value = 'LRR01')
UPDATE dbo.Sources
SET
CatCustom = CONCAT_WS(', ', RTRIM(CatCustom), 'LRR01')
WHERE
SourceID IN (1,2,3,4,5,8,9,44,63,45,101,102,222,344)
AND CatCustom NOT LIKE '%LRR01%';
Is there a (supposedly) simple way to get an exact match when using the replace function in SQL Server 2012? (I'm open to other searching possibilities as well, of course)
For example, I'm using the following code to grab all the objects in a DB containing 'texter' in it at some point:
select OBJECT_NAME(object_id) name,
OBJECT_DEFINITION(object_id) code,
type
into #tester
from sys.objects
where OBJECT_DEFINITION(object_id) LIKE '%texter%'
This doesn't seem to differentiate between .texter, #texter or stupidtexter.
and so if I use:
update #tester
set code = REPLACE(code, 'texter', 'bluenose')
where code LIKE '%texter%'
It's going to replace any variant of 'texter' with 'bluenose'
Let's assume I only want to replace the ' texter' and '.texter' versions of this and nothing else (noting that within each object it is possible that #texter or stupidtexter may also be present in the object code).
Is there a way I can differentiate between the variants of 'texter', #texter and stupidtexter?
Thanks
The solution was:
replace(REPLACE(code, '.texter', 'bluenose'), ' texter', 'bluenose')
Thanks to Sean Lange for the answer!
Dapper 1.34 (on earlier Dapper ver like 1.1x this worked fine).
db.Query(#"Select [whatever] from #TableName Where [PREFIX]='#Prefix' order by [something] desc",
new { TableName = tableName, Prefix = prefix })
Error: System.Data.SqlClient.SqlException (0x80131904): Must declare the table variable "#TableName".
I get same error trying to define DynamicParameters and passing those.
=======
I am currently doing string substitution {%1} .. but that does not seem acceptable ...
Can I please get a sample, also looking at test class for dapper I cant see it running, maybe something wrong with my project setup ?
No, that has never worked fine, due to how SQL works:
table names cannot be parameterized, and dapper has never offered this; that could work if you pass in a DataTable as a table-valued-parameter, though - which dapper does support - but I don't think this is what you are after
'#Prefix' is a string literal; you just mean #Prefix (no single quotes)
I need to fetch Table's TOP_PK, IDENT_CURRENT, IDENT_INCR, IDENT_SEED for which i am building dynamic query as below:
sGetSchemaCommand = String.Format("SELECT (SELECT TOP 1 [{0}] FROM [{1}]) AS TOP_PK, IDENT_CURRENT('[{1}]') AS CURRENT_IDENT, IDENT_INCR('[{1}]') AS IDENT_ICREMENT, IDENT_SEED('[{1}]') AS IDENT_SEED", pPrimaryKey, pTableName)
Here pPrimaryKey is name of Table's primary key column and pTableName is name of Table.
Now, i am facing problem when Table_Name contains " ' " character.(For Ex. KIN'1)
When i am using above logic and building query it would be as below:
SELECT (SELECT TOP 1 [ID] FROM [KIL'1]) AS TOP_PK, IDENT_CURRENT('[KIL'1]') AS CURRENT_IDENT, IDENT_INCR('[KIL'1]') AS IDENT_ICREMENT, IDENT_SEED('[KIL'1]') AS IDENT_SEED
Here, by executing above query i am getting error as below:
Incorrect syntax near '1'.
Unclosed quotation mark after the character string ') AS IDENT_SEED'.
So, can anyone please show me the best way to solve this problem?
Escape a single quote by doubling it: KIL'1 becomes KIL''1.
If a string already has adjacent single quotes, two becomes four, or four becomes eight... it can get a little hard to read, but it works :)
Using string methods from .NET, your statement could be:
sGetSchemaCommand = String.Format("SELECT (SELECT TOP 1 [{0}] FROM [{1}]) AS TOP_PK, IDENT_CURRENT('[{2}]') AS CURRENT_IDENT, IDENT_INCR('[{2}]') AS IDENT_ICREMENT, IDENT_SEED('[{2}]') AS IDENT_SEED", pPrimaryKey, pTableName, pTableName.Replace("'","''"))
EDIT:
Note that the string replace is now only on a new, third substitution string. (I've taken out the string replace for pPrimaryKey, and for the first occurrence of pTableName.) So now, single quotes are only doubled, when they will be within other single quotes.
You need to replace every single quote into two single quotes http://beyondrelational.com/modules/2/blogs/70/posts/10827/understanding-single-quotes.aspx
I already have a table with 3 column (Id bigint, Title nvarchar(450), Description nvarchar(MAX)) in sql 2008
I decide convert Title and Description column into one XML column. but when trying to update get many error like "illegal name character" or "illegal qualified name character" and etc.
to solve this problem i just create windows application with subsonic 2.1 with below code
MyTable tbl = new MyTable(1111);
tbl.myXMLcol = "<Person><Title><![CDATA[ " + HttpUtility.HtmlEncode(tbl.Title) + " ]]></Title><Description><![CDATA[ " + HttpUtility.HtmlEncode(tbl.Description) + " ]]></Description></Person>";
tbl.Save();
then try to add "<?xml version=\"1.0\" encoding=\"utf-16\"?>" into first above string that get error "unable to switch the encoding".
Note: Also i using this method to remove illegal character, but not solve my problem
Note2: I trying to update Japanese record that get this error, but for English work properly.
Could you please help me.
Thanks.
I think you should be using UTF-8 encoding.
You can find out more about the encoding here:
http://www.ibm.com/developerworks/xml/library/x-utf8/
Also, you will find some more information here:
http://msdn.microsoft.com/en-us/library/ms131375.aspx
Why not do all of this directly in SQL Server ?? Could you try this:
UPDATE YourTable
SET XmlCol = CAST(N'<Person><Title>' + Title +
N'</Title><Description>' + Description +
N'</Description></Person>' AS XML)
WHERE ID = 12345
Does that work? If not, then maybe this one here?
UPDATE YourTable
SET XmlCol = CONVERT(XML, N'<Person><Title>' + Title +
N'</Title><Description>' + Description +
N'</Description></Person>', 2)
WHERE ID = 12345
The "2" for style will strip out things SQL Server doesn't like about XML - things like DTD and so forth. Maybe that'll help in your case, too?
I've had various troubles with SQL Server's XML support when importing XML from outside. Usually, one way or another, it ends up working, though :-)
Marc
After many research, I found article about my problem.
Please see Tarun Ghosh post