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
Related
Is it posible that an ms sql query to return only a portion of what is stored in a field?
For example, I got this data stored in the field:
row NGV1="" NGV10="*" NGV6=" " NGV5=" " NGV4=" " NGV3=" " NGV2=" " _tipprodus="NGV" lotuloptim="20" /
I only need to display the value that is between the quotation marks from this part lotuloptim="20". The result should be 20.
Thank you in advance.
Best regards
Yes, you can use SUBSTRING, but using substring you need to know the start index
SELECT SUBSTRING(column_name,start,length) AS some_name FROM table_name;
and then you can use CHARINDEX to find the index for example to the lotuloptim word
DECLARE #document varchar(64);
SELECT #document = 'Reflectors are vital safety' +
' components of your bicycle.';
SELECT CHARINDEX('bicycle', #document);
GO
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!
I know there are a few about similar topics on here but I can't find one related to my issue, this is it:
I have a table with an ID Column and a QRCode column. each time an item is added the primary key auto increments. The QRCode will scan in to be like the following:
"http://somewebsite.com/12345/987654321"
i want to be able to remove the "http://somewebsite.com/" from the string, I know how to do this in C# however I am unsure of how to do this in Sql Server. any guidance would be great, thanks
Regular formats are like the following, and used in the example below.
"http://somewebsite.com/12345/456564654"
"http://somewebsite.com/12345/989886765"
"http://somewebsite.com/12346/987654321"
the query returns the following results:
SELECT
REPLACE
(
REPLACE(QRCode, 'http://somewebsite.com/', '')
,'/', ' '
) AS QRCode
FROM
QRTable
WHERE
QRCode LIKE '%http://somewebsite.com/%'
"12345 456564654"
"12345 989886765"
"12346 987654321"
Now i need to update the table with those new results however as there's 3 results, i get the error message "Subquery returned more than 1 value". is there a way to replace the selected values in the table with the ones that exist based on the primary key field?
**Removed previous example
A more complete answer based on your updated question. This removes the first portion of the URL as well as the trailing / so that you get your desired output.
DECLARE #Variable VARCHAR(50)
SET #Variable = 'http://somewebsite.com/12345/456564654'
SET #Variable =
REPLACE
(
REPLACE(#Variable, 'http://somewebsite.com/', '')
,'/', ' '
)
PRINT #Variable
Output = 12345 456564654
Looking at your SQL statement you want this:
SELECT
REPLACE
(
REPLACE(QRCode, 'http://somewebsite.com/', '')
,'/', ' '
) AS QRCode
FROM
QRTable
WHERE
QRCode LIKE '%http://somewebsite.com/%'
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');
I've a query that works OK against a MySQL database, but we've had to migrate the database and applications to SQL Server. Now I'm in trouble getting this query to work.
SELECT
complist.CompName,
complist.CompID,
componenttrace.Remark,
complist.McID,
complist.Station,
complist.Slot,
complist.Amount - complist.Used - ISNULL(complist.Correction,0)
FROM
complist, componenttrace
WHERE
complist.McID = 1004
AND complist.CompID = componenttrace.CompID
AND UPPER(complist.Station + '.' + complist.Slot) LIKE '%1.1%'
ORDER BY
complist.CompName, complist.CompID
On the application, the part that goes
AND UPPER(complist.Station + '.' + complist.Slot) LIKE '%1.1%'
is added automatically if there's any value on a given field (i.e. 1.1).
I have a SQL syntax problem, but don't know how to solve it. Can anyone shed some light here?
thanks,
Gustavo
Without error message is very hard to say what is wrong, but my bet is on the implicit join. Try
SELECT
complist.CompName,
complist.CompID,
componenttrace.Remark,
complist.McID,
complist.Station,
complist.Slot,
complist.Amount - complist.Used - ISNULL(complist.Correction,0)
FROM
complist
INNER JOIN componenttrace ON complist.CompID = componenttrace.CompID
WHERE
complist.McID = 1004
AND UPPER(complist.Station + '.' + complist.Slot) LIKE '%1.1%'
ORDER BY
complist.CompName, complist.CompID