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
Related
I'm using Microsoft Query to pull data from MS SQL Server to Excel. Many of my tables have the same column names, for example:
[task].[active]
[user].[active]
[task].[name]
[user].[name]
When I pivot in Excel, only the column names are shown. A pivot filter might have multiple fields called "active" which is very confusing.
I'd like to alias every column with the table name it's from, so that in the filter it would say "task_active" and "user_active". My Excel SELECT statement would be:
SELECT active AS task_active, name AS task_name FROM task...
Is there a quick way to prepend the table name to an alias using a formatting tool? I have Apex SQL Refactor, and Notepad++ but I haven't found a way to do this without having to manually type all of the column names again.
If you populate resultset to datatable then datatable to excel then it will automatically change duplicate column name to col1,col2 etc.
This is not your requirement.you want it to be specific.
Method 1 . Create temp table with desire column name
Insert the result in #temp table
Return #temp table result set
Method 2 : Use dynamic query.
Wht your real query look like ?
I want to select the numeric part of data that saved in nvarchar() datatype column in sqlserver.
The size of a character in rows doesn't same and maybe some of the rows don't have the numeric part on the column, for example, the data format like
/TablePhoneHome>
or
<TablePhoneHome></TablePhoneHome>
or
<TablePhoneHome><Number Num="9123159834"/></TablePhoneHome>
or
<TablePhoneHome><Number Num="somthing"/></TablePhoneHome>
I want to select the phone number from that like :
09151826166-09151150374
null
9123159834
Assuming the data type is actually xml (and if it isn't, then you should fix your data type to be xml) you can easily use XQUERY to get the value:
SELECT YT.YourColumn.value('(/TablePhoneHome/Number/#Num)[1]','varchar(50)') AS Num
FROM YourTable YT;
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
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
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);