Select a table by value inside BLOB column - sql-server

We use ADO and SQL Server.
Our table has a blob column XMLFIELD, inside this blob we store a complete XML file with information.
<XML>
.... much much text and fields ....
<\XML>
Reading and writing this XML field is no problem
How can I build a query to select only datasets whose XMLFIELD contains a certain Text / Word
select * from MyTable where XMLFIELD contains a special word
or
select * from MyTable where XMLFIELD contains a special field
and has a special value

Related

convert one column to json

Using Azure SQLServer I'm trying to convert only one column to json format. The data is in a nvarchar field. Using For Json Auto will convert all fields and all rows. What I need is just to convert only one column.
By converting to json what I mean is to be clickable to see it's data a new window inside SSMS.
Let's say the table (Logs) has 2 columns: LogId and LogData. LogData is nvarchar(max) and contains json data.
What I need is to query the table and have a clickable logData column.
You can try like following to get only one column as JSON
select o.*,
(select YourColumnForJson
from YourTable i
where o.Identifer=i.Identifer for json auto) as JsonColumn
from YourTable o

select query to retrieve exact word matching from a list of word in mssql

[403 image link by OP]
Above image is of my database table in Mssql server. I want to select the data which only contains arrTransactionColumns from report_keys column.
My query is
select * from table where report_keys like '%arrTransactionColumns%'
but it gives the result which has arrTransactionColumnsHRI too.
try use CHARINDEX
select *
from TABLE
WHERE CHARINDEX('arrTransactionColumns',report_keys )>0

find data size of xml type column in db2

I have a table which has a column of data type xml. I want to find the size of data inserted by user in that column. DB2 shows the length of that column is 0.
How can i find the size of data that is present in column of type xml in a table in DB2 database?
Had the same problem.
As XML in DB2 is a transient datatype it is stored in its hierarchical form. So what you have to do is to serialize the xml-content (because it's stored in it's "parsed" form) and output it into a serialized data type (e.g. a CLOB).
Take for example the table xmltable with the column content which is of type XML:
SELECT xmltable.*, XMLSERIALIZE(xmltable.content AS CLOB(20M))
FROM xmltable
Be sure to initialize the CLOB large enough, otherwise you'll get an error 22001.

How to extract SQL Server image column data into readable format and store into another column?

I have a SQL Server database in which there are around 5 lac records in a table called message_mst, here is the table structure
Table name : message_mst
Columns:
message_id int
message_body image
I am not the person who built this database but whoever built this used image column to store all the message text which is simply plain text. But if we select the records, message_body prints all the text in HEX format. I want to convert it into readable format and then store into new field called message_body_readable.
How can I do that?
You can do it converting the field first to varbinary and than to varchar.
declare #t table (i image)
insert into #t values('some text')
select i, CAST(cast(i as varbinary(max)) as varchar(max))
from #t
Can you try this once..?
SELECT CONVERT(VARCHAR(1000), message_body, 0) FROM message_mst

Sql Server - Capture an XML query and save it to a table?

I would like to run a (extensive) query that produces one line of XML. This XML represents about 12 tables worth of relational data. When I "delete" a row from the top level table I would like to "capture" the state of the data at that moment (in XML), save it to an archive table, then delete all the child table data and finally mark the top level table/row as "isDeleted=1". We have other data hanging off of the parent table that CANNOT be deleted and cannot lose the relation to the top table.
I have most of the XML worked out - see my post here on that can of worms.
Now, how can I capture that into an archive table?
CREATE TABLE CampaignArchive(CampaignID int, XmlData XML)
INSERT INTO CampaignArchive(CampaignID, XmlData)
SELECT CampaignID, (really long list of columns) FROM (all my tables) FOR XML PATH ...
This just does not work. :)
Any suggestions?
TIA
You need a subquery to wrap all that XML creation into one single scalar that goes into column XmlData. Again, use TYPE to create a scalar of type XML not a string:
INSERT INTO CampaignArchive(CampaignID, XmlData)
SELECT CampaignID, (
select (really long list of columns)
FROM (all my tables) FOR XML PATH ..., type);

Resources