I want to link the word Click with URL in the email. This URL change for every row of SELECT.
I write this:
td = CAST('Click' AS XML), ''
But SQL server return:
Análisis de XML: línea 1, carácter 73; se esperaba punto y coma
Which translates to
XML parsing : line 1 , character 73; expected semicolon
The & symbol should be encoded as &. The parser sees &orig and expects a semicolon after it (or at least at some point in the string).
Related
I have column A with value hello.
I need to migrate it into new column AJson with value ["hello"].
I have to do this with Sql Server command.
There are different commands FOR JSON etc. but they serialize value with column name.
This is the same value that C# method JsonConvert.SerializeObject(new List<string>(){"hello"} serialization result would be.
I can't simply attach [" in the beginning and end because the string value may contain characters which without proper serialization will break the json string.
My advice is you just make a lot of nested replaces and then do it yourself.
FOR JSON is intended for entire JSON, and therefore not valid without keys.
Here is a simple example that replaces the endline with \n
print replace('ab
c','
','\n')
Backspace to be replaced with \b.
Form feed to be replaced with \f.
Newline to be replaced with \n.
Carriage return to be replaced with \r.
Tab to be replaced with \t.
Double quote to be replaced with "
Backslash to be replaced with \
My approach was to use these 3 commands:
UPDATE Offers
SET [DetailsJson] =
(SELECT TOP 1 [Details] AS A
FROM Offers AS B
WHERE B.Id = Offers.Id
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER)
UPDATE Offers
SET [DetailsJson] = Substring([DetailsJson], 6, LEN([DetailsJson]) - 6)
UPDATE Offers
SET [DetailsJson] = '[' + [DetailsJson] + ']'
..for op's answer/table..
UPDATE Offers
SET [DetailsJson] = concat(N'["', string_escape([Details], 'json'), N'"]');
declare #col nvarchar(100) = N'
a b c " : [ ] ]
x
y
z'
select concat(N'["', string_escape(#col, 'json'), N'"]'), isjson(concat(N'["', string_escape(#col, 'json'), N'"]'));
Context:
till now i uses to use regexp in sql to extract variable urls. I find it very slow and want to optimize it using substr and instr commands. That's important for me cause as i'm new in sql it serves me to be more familiar with such commands.
database:
my db is made by posts extracted from social platforms. text are called "titre". It contains variables url in different formats: www, http, https. I want to create a table or table view (i m not fixed) containing those url and the related id_post.
My work:
I have noticed that url always ends with a blank space, sthg like: "toto want to share with you this www.example.com in his post"
here stands what i ve done so far:
---longueur de la chaîne de caractère depuis https
select LENGTH(substr(titre, INSTR(titre,'https:'))) from post_categorised_pages where id_post = '280853248721200_697941320345722';
---longueur de la chaîne de caractère depuis le blanc
select LENGTH(substr(titre, INSTR(titre,' ', 171))) from post_categorised_pages where id_post = '280853248721200_697941320345722';
--- différence pour obtenir la longueur de chaîne de caractères de l'url
select LENGTH(substr(titre, INSTR(titre,'https:'))) - LENGTH(substr(titre, INSTR(titre,' ', 171))) as longueur_url from post_categorised_pages where id_post = '280853248721200_697941320345722';
---url
select substr(titre, 171, 54)from post_categorised_pages where id_post = '280853248721200_697941320345722';
Question:
How can i automotasize that over the whole table "post_categorised_page"?
Can i introduce case when statements to take into account https or http of www. and how can i do that?
Thanks a lot!!!!
Maybe, instead of the "HTTP", HTTPS" or "WWW" string you would need to have the name of a column.
In this case, probably, it would be helpful to have a definition table where to define all possible sources. This tabel to have 2 columns (ID and source_name).
Then, in your post_categorised_pages table, to insert also the source of the message (the ID value).
Then, into the query, to join with this definition table by ID and, instead of
select substr(titre, INSTR(titre,'https:'), (LENGTH(substr(titre, INSTR(titre,'https:'))) - LENGTH(substr(titre, INSTR(titre,' ', (INSTR(titre,'https:')))))))from post_categorised_pages where id_post = '280853248721200_697941320345722';
to have
select substr(titre, INSTR(titre,"definition table".source_name), (LENGTH(substr(titre, INSTR(titre,"definition table".source_name))) - LENGTH(substr(titre, INSTR(titre,' ', (INSTR(titre,"definition table".source_name)))))))from post_categorised_pages where id_post = '280853248721200_697941320345722';
Ok guys, here is the solution i have found (there is stil one mistake, see at end of post).
I use two views to finally extract my strings.
First view is create by a connect by request:
--- create intermediate table view with targeted pattern position
create or replace view Start_Position_Index as
with "post" as
(select id, text from "your_table" where id= 'xyz')
select id, instr(text,'#', 1, level) as position, text
from post
connect by level <= regexp_count(titre, '#');
then
--- create working table view with full references and blank position for each pattern match and string_lenght for each one
create or replace view _#_index as
select id, position as hashtag_pos, INSTR(text,' ', position) as blank_position, INSTR(text,' ', position) - position as string_length, text
from Start_Position_Index;
At the end you will be able to retrieve the hashtags (in that case) you were looking for in your string.
Ok so the mistakes:
- if the pattern you are looking for is at the end of your string it will retrieve a null value cause there will be no blank space (as it is at end of the string).
- it is not well optimized cause here i am working with views and not tables. I think using tables will be faster.
But i m pretty sure there is lots of things to do in order to optimize this code... any idea? The challenge were how to extract specific pattern recursively among strings whithout using costy regex and without using pl/sql stuff. What do you think of that?
How about using Oracle Full Text search?
This will index all the words from the column and will provide the hashtags or web addresses, as both are written in one word, without space in between.
Image I have a currency table, containing e.g. this record :
- Id = 1
- Code = EUR
- Symbol = €
Important to notice :
The input in our database is already property HTML-encoded!
Now, when I use this SQL statement :
SELECT '#id' = Currency.Id
, '#code' = Currency.Code
, '#symbol' = Currency.Symbol
FROM Currency
FOR XML PATH('currency')
, ROOT('list')
, TYPE
;
...it unfortunately results into the following XML :
<list><currency id="1" code="EUR" symbol="€" /></list>
Notice that the Euro-symbol has been re-encoded, rendering it invalid.
How can I avoid that? How can I obtain the following XML output :
<list><currency id="1" code="EUR" symbol="€" /></list>
The result you get unfortunately and is re-encoded and invalid is perfectly correct - but not what you expect. You pass in € which is a string. Within XML this is escaped as € and will be re-coded to €.
You must stop to think of XML as kind of formalized string. This is a technical issue. XML will handle this implicitly.
There are two ways:
Go the string-way and cast your XML to NVARCHAR, do any string manipulation you might want (e.g. REPLACE(myXML,'€','€') and cast back to XML or
(I'd prefer this!) hand in the € as actual symbol and let the XML engine do the encoding.
EDIT
One more thing: SQL Server doesn't know the € entity. Try with € or €:
SELECT '€' AS [#EuroSign] --works
,'€' AS [#NamedEscapedEuro] --will be encoded
,'€' AS [#EscapedEuro] --will be encoded
FOR XML PATH('TestEuro'),ROOT('root')
SELECT --CAST('<x>'+'€'+'</x>' AS XML).value('/x[1]','nvarchar(10)') AS [#EuroSign] --not allowed!!!
--CAST('<x>'+'€'+'</x>' AS XML).value('/x[1]','nvarchar(10)') AS [#NamedEscapedEuro] --not allowed, exists, but not known in SQL Server!
CAST('<x>'+'€'+'</x>' AS XML).value('/x[1]','nvarchar(10)') AS [#EscapedEuro] --works
FOR XML PATH('TestEuro'),ROOT('root')
There is column named FileImage that contains XML data.
<root>
<Image>
<File_image>02Jan12- Mfg--7.jpg</File_image>
<Page_no>7</Page_no>
<Logo>N</Logo>
</Image>
</root>
I want to search whether text between tags contains space character.
This is for checking invalid file name.
SELECT *
FROM mytable
where fileimage.exist('
/Image:file_image//text()
[contains(., " ")]') = 1
But error shown
XQuery [mytable.FileImage.exist()]: The name "Image" does not denote a namespace.
How to do this ? I am using SQL Server 2005.
SELECT *
FROM mytable
where fileimage.exist('
/root/Image/File_image//text()
[contains(., " ")]') = 1
This request works as intended:
select dit_in.id data_item_in, dit_out.id data_item_out, alg.id algo_id
from algorithm_run arun
join algorithm_run_of arof on
arof.algorithm_run_id = arun.id
join algorithm_run_input arin on
arin.algorithm_run_id = arun.id
join data_item dit_in on
dit_in.id = arin.data_item_id
join algorithm alg on
alg.id = arof.algorithm_id
join algorithm_run_output arout on
arout.algorithm_run_id = arun.id
join data_item dit_out on
dit_out.id = arout.data_item_id
where alg.id in (182,183,143,162,125,222)
Unfortunately I get an error when I add at the end:
and arun.start_time >= to_date(’01/jun/2011’,’dd/mm/yyyy’)
and arun.start_time < to_date(’01/jul/2011’,’dd/mm/yyyy')
I'm using a web interface, the error message is:
warning: oci_execute() [function.oci-execute]: ORA-00911: invalid character in /opt/csw/apache2/share/htdocs/DAE/sites/default/modules/data_repository/data_repository.inc on line 117.
warning: oci_fetch_row() [function.oci-fetch-row]: ORA-24374: define not done before fetch or execute and fetch in /opt/csw/apache2/share/htdocs/DAE/sites/default/modules/daedatabase/daedatabase_db.inc on line 852.
and arun.start_time < to_date(’01/jul/2011’,’dd/mm/yyyy')
Do I see two different types of quote characters around that last bit? A single quote and a backquote? Or is that just a cut/paste or translation problem?
Try this:
and arun.start_time >= to_date(’01/06/2011’,’dd/mm/yyyy’)
and arun.start_time < to_date(’01/07/2011’,’dd/mm/yyyy’)
or
and arun.start_time >= to_date(’01/jun/2011’,’dd/mon/yyyy’)
and arun.start_time < to_date(’01/jul/2011’,’dd/mon/yyyy’)
The problem is that your date string (01/jun/2011) doesn't match the format specifier (dd/mm/yyyy). You need to either change your date or the specifier, as the above examples show.
As Phil points out, your strings are wrapped in two different quotes marks. It looks like you're mainly use fancy quote marks, from a word processor. This is a problem, because Oracle is expecting plain ASCII apostrophes (ASCII 39).
It would certainly explain why you're getting an ORA-00911 error.
To fix this, you simply need to replace all the ’ with ' .
To avoid it in the future you should use a text editor or IDE when writing code.