Replace all URLs in SQL Server table column which contains html data - sql

I have a SQL Server table column which contains html contents with links in format href="/page/random_id1/random_id2".
Here random_id1 is a guid and random_id2 is a slug. Now i would like to run a sql query which can replace all href values by converting them to href="/page/random_id2".
Notice here, i'm removing random_id1 part from every herf.
Here's a sample query:
select top 1 id, htmlcode from tenantpage;
03781776-5f64-4c59-a6f7-5873e0b9f128
<div>test</div>Link<div>something else <span>test Link</span></div>Link
I don't know if this is possible, if possible please help here.

Related

SQL Error 'ORA-00904' When Retrieving Data From an XML File Via XMLQuery

I've been trying for days now to retrieve data from an XML file with a SELECT statement in SQL Developer but I constantly get the 'ORA-00904' when trying to execute the statement. So far, these are the steps I've been following:
Create the table and directory where I want the XML data to be stored in:
CREATE TABLE PLAYER OF XMLTYPE; / CREATE DIRECTORY PLDIR AS 'C:\Users\marta\OneDrive\Escritorio\UOC\Sem3\ABD\PR2'; /
Insert into my PLAYER table said data:
INSERT INTO PLAYER VALUES (XMLTYPE(bfilename('PLDIR', 'InfoPlayersWPT.xml'),nls_charset_id('AL32UTF8')))
/
So far so good. The issue appears when I try to execute the SELECT statement
What could it be? I've changed the $Name parameter a million times as well as the Name field but nothing changes. The thing is that in the XML file, these are the fields:
--Update--
I've modified a little bit the structure and this is the new error I get:
enter image description here
I've reached a point where I don't get if there could be a problem with my database connection or if the variable are incorrect.
Any form of help would be much appreciated.
Your table doesn't have a name or id column. Your query is trying to get those, while also transforming the XML to an info node making the id a node rather than an attribute, but you still don't extract the values from that new XML. You don't need to though.
If the document only has one player (which seems unlikely with the outer node 'Players') then you can get the data with an XMLQuery call for each value:
select XMLQuery('/Players/Player/#id' passing p.object_value returning content) as id,
XMLQuery('/Players/Player/Name/text()' passing p.object_value returning content) as name
from player p
ID
NAME
1
Francinsco Navarro Compán
But it's a bit simpler, particularly if you add more data items, to use a single XMLTable instead:
select x.id, x.name
from player p
cross apply XMLTable(
'/Players/Player'
passing p.object_value
columns id number path '#id',
name varchar2(30) path 'Name'
) x
ID
NAME
1
Francinsco Navarro Compán
fiddle
... which would also handle multiple players in each XML document.

T-SQL: Exclude Columns from a SELECT statement based on string

My overall goal is to create new tables by selecting columns in existing tables with certain patterns/tags in their column names. This is in SQL Server.
For example, I have a common need to get all contact information out of a company table, and into its own contact table.
I haven't been able to find a programmatic approach in SQL to express excluding columns from a SELECT statement based on string.
When looking to options like the COL_NAME function, those require an ID arg, which kills that option for me.
Wishing there was something built in that could work like the following:
SELECT *
FROM TABLE
WHERE COL_NAME() LIKE 'FLAG%'
Any ideas? Open to anything! Thanks!!
One trick could be to firstly using the following code to get the column names you desire:
select * from information_schema.columns
where table_name='tbl' and column_name like 'FLAG%'
Then concatenate them into a comma-delimited string and finally create a dynamic sql query using the created string as the column names.

Read XML string in SQL and Dynamically select value from XMl string

I have One table "tblXMLData" with One column "coreData".
I am inserting XML string in this table.
I want to read this xml and insert record into another table.
When i write bellow code it is working fine,
Declare #xmlRec xml
Select TOP 1 #xmlRec = coreData FROM tblXMLData
SELECT
tmpTable.col.query ('FName').value('.','varchar(50)') AS FName,
tmpTable.col.query ('LName').value('.','varchar(50)') AS LName,
tmpTable.col.query ('Mobile').value('.','varchar(50)') AS Mobile
FROM #xmlRec.nodes('data/data') AS tmpTable(col)
But I want to create select statement Dynamically. Like I am read column name from another table and creating dynamic select statement.
When i execute that statement it will not work because "#xmlRec.nodes" is giving error.
I want to create dynamic Query and insert selected record into another table.
Please help....

SQL Query: Modify records based on a secondary table

I have two tables in a PostgreSQL database.
The first table contains an ID and a text field with up to 200 characters and the second table contains a data definition table which has a column that contains smileys or acronyms and a second column which converts them to plain readable English.
The number of records in table 1 is about 1200 and the number in table two is about 300.
I wish to write a SQL statement which will convert any text speak in column 1 in table one into normal readable language based on the definitions in Table 2.
So for example if the value in table 1 reads as: Finally Finished :)
The transformed SQL would be something like: Finally Finished Smiles or smiling,
where the definition is pulled from the second table.
Note the smiley could be anywhere in the text in column one and could one of three hundred characters.
Does anyone know if this is possible?
Yes. Do you want to do it entirely in SQL, or are you writing a brief bit of code to do this? I'm not entirely sure of how to do it all in SQL but I would consider something like what is below:
SELECT row.textToTranslate FROM Table_1
oldText = row.textToTranslate
Split row.textToTranslate by some delimeter
For each word in row.textToTranslate:
queryResult = SELECT FROM Table_2 WHERE pretranslate=word
if(queryResult!=Null)
modifiedText = textToTranslate.replace(word, queryResult)
UPDATE Table_1 SET translatedText=modifiedText WHERE textToTranslate=oldText

How do I use case statement to aid altering another field?

I have a hopefully pretty simple question here. I'm converting some Access SQL script into Server Management Studio 2008.
Currently the Access script shows the following line of code:
IIf([rsosourceID]) IN (254,360,446),"MoneySavingExpert" as SourceName
Basically it's creating a temporary table with four columns, if the fields match say those 3 numbers on the left then it will populate a fourth column with their new name. (To be used for grouping in a later report)
How can I do something simillar in SQL? I've tried using a Case statement but to my knowledge that will only alter the field you're looking against.
In this case I need to look at one field then using it's results alter another, any ideas?
A case statement can return a new column using the value of any other column(s):
SELECT rsoSourceID,
rsoDescription,
rsoCategory,
case when rsoSourceID in (254,360,446)
then 'MoneySavingExpert'
else null end as SourceName
FROM TableName