T-SQL to modify XML Data
I will like to modify the value for TransactionAmount where TransactionAmount > 15000 using TSQL. Table has over 50K rows.
Could someone pls help me? I am having trouble finding any examples anywhere.
<BillingTransactionInfo xmlns="http://xed.com/bc/gx.billsactioninfomodel">
<AccountingDate>2018-12-07T13:40:44</AccountingDate>
<AccountingDay>7</AccountingDay>
<AccountingMonth>12</AccountingMonth>
<AccountingYear>2018</AccountingYear>
<AccountNumber>PC:0049207</AccountNumber>
<AccountType>insured</AccountType>
<BillingReferenceNumber>50000018100</BillingReferenceNumber>
<CustomerName>JOHN MCGEE</CustomerName>
<GLMonth>12</GLMonth>
<GLYear>2018</GLYear>
<TransactionSubtypeCode>DirectBillMoneyReceivedTxn</TransactionSubtypeCode>
<TransactionSubtypeDesc>Direct Bill Money Received</TransactionSubtypeDesc>
<IssueDate>2018-12-07T13:40:37</IssueDate>
<PaymentMethod>cash</PaymentMethod>
<PolicyRiskState>AL</PolicyRiskState>
<ReasonCode>Direct Bill Money Received</ReasonCode>
<RecordCreationDate>2018-12-07T13:40:37</RecordCreationDate>
<Source>BILLING</Source>
<TransactionAmount>2570.77</TransactionAmount>
<TransactionCreateDate>2018-12-07T13:40:37</TransactionCreateDate>
</BillingTransactionInfo>
xml tsql sql-server-2012
New contributor
add a comment |
I will like to modify the value for TransactionAmount where TransactionAmount > 15000 using TSQL. Table has over 50K rows.
Could someone pls help me? I am having trouble finding any examples anywhere.
<BillingTransactionInfo xmlns="http://xed.com/bc/gx.billsactioninfomodel">
<AccountingDate>2018-12-07T13:40:44</AccountingDate>
<AccountingDay>7</AccountingDay>
<AccountingMonth>12</AccountingMonth>
<AccountingYear>2018</AccountingYear>
<AccountNumber>PC:0049207</AccountNumber>
<AccountType>insured</AccountType>
<BillingReferenceNumber>50000018100</BillingReferenceNumber>
<CustomerName>JOHN MCGEE</CustomerName>
<GLMonth>12</GLMonth>
<GLYear>2018</GLYear>
<TransactionSubtypeCode>DirectBillMoneyReceivedTxn</TransactionSubtypeCode>
<TransactionSubtypeDesc>Direct Bill Money Received</TransactionSubtypeDesc>
<IssueDate>2018-12-07T13:40:37</IssueDate>
<PaymentMethod>cash</PaymentMethod>
<PolicyRiskState>AL</PolicyRiskState>
<ReasonCode>Direct Bill Money Received</ReasonCode>
<RecordCreationDate>2018-12-07T13:40:37</RecordCreationDate>
<Source>BILLING</Source>
<TransactionAmount>2570.77</TransactionAmount>
<TransactionCreateDate>2018-12-07T13:40:37</TransactionCreateDate>
</BillingTransactionInfo>
xml tsql sql-server-2012
New contributor
How do you want to modify the values? It is easy to find all XMLs where the amount exceeds a given border, but you did not tell us, what else you want to place there...
– Shnugo
12 hours ago
Yes, I will like to modify the values to 10000.
– Dan Kalio
5 hours ago
add a comment |
I will like to modify the value for TransactionAmount where TransactionAmount > 15000 using TSQL. Table has over 50K rows.
Could someone pls help me? I am having trouble finding any examples anywhere.
<BillingTransactionInfo xmlns="http://xed.com/bc/gx.billsactioninfomodel">
<AccountingDate>2018-12-07T13:40:44</AccountingDate>
<AccountingDay>7</AccountingDay>
<AccountingMonth>12</AccountingMonth>
<AccountingYear>2018</AccountingYear>
<AccountNumber>PC:0049207</AccountNumber>
<AccountType>insured</AccountType>
<BillingReferenceNumber>50000018100</BillingReferenceNumber>
<CustomerName>JOHN MCGEE</CustomerName>
<GLMonth>12</GLMonth>
<GLYear>2018</GLYear>
<TransactionSubtypeCode>DirectBillMoneyReceivedTxn</TransactionSubtypeCode>
<TransactionSubtypeDesc>Direct Bill Money Received</TransactionSubtypeDesc>
<IssueDate>2018-12-07T13:40:37</IssueDate>
<PaymentMethod>cash</PaymentMethod>
<PolicyRiskState>AL</PolicyRiskState>
<ReasonCode>Direct Bill Money Received</ReasonCode>
<RecordCreationDate>2018-12-07T13:40:37</RecordCreationDate>
<Source>BILLING</Source>
<TransactionAmount>2570.77</TransactionAmount>
<TransactionCreateDate>2018-12-07T13:40:37</TransactionCreateDate>
</BillingTransactionInfo>
xml tsql sql-server-2012
New contributor
I will like to modify the value for TransactionAmount where TransactionAmount > 15000 using TSQL. Table has over 50K rows.
Could someone pls help me? I am having trouble finding any examples anywhere.
<BillingTransactionInfo xmlns="http://xed.com/bc/gx.billsactioninfomodel">
<AccountingDate>2018-12-07T13:40:44</AccountingDate>
<AccountingDay>7</AccountingDay>
<AccountingMonth>12</AccountingMonth>
<AccountingYear>2018</AccountingYear>
<AccountNumber>PC:0049207</AccountNumber>
<AccountType>insured</AccountType>
<BillingReferenceNumber>50000018100</BillingReferenceNumber>
<CustomerName>JOHN MCGEE</CustomerName>
<GLMonth>12</GLMonth>
<GLYear>2018</GLYear>
<TransactionSubtypeCode>DirectBillMoneyReceivedTxn</TransactionSubtypeCode>
<TransactionSubtypeDesc>Direct Bill Money Received</TransactionSubtypeDesc>
<IssueDate>2018-12-07T13:40:37</IssueDate>
<PaymentMethod>cash</PaymentMethod>
<PolicyRiskState>AL</PolicyRiskState>
<ReasonCode>Direct Bill Money Received</ReasonCode>
<RecordCreationDate>2018-12-07T13:40:37</RecordCreationDate>
<Source>BILLING</Source>
<TransactionAmount>2570.77</TransactionAmount>
<TransactionCreateDate>2018-12-07T13:40:37</TransactionCreateDate>
</BillingTransactionInfo>
<BillingTransactionInfo xmlns="http://xed.com/bc/gx.billsactioninfomodel">
<AccountingDate>2018-12-07T13:40:44</AccountingDate>
<AccountingDay>7</AccountingDay>
<AccountingMonth>12</AccountingMonth>
<AccountingYear>2018</AccountingYear>
<AccountNumber>PC:0049207</AccountNumber>
<AccountType>insured</AccountType>
<BillingReferenceNumber>50000018100</BillingReferenceNumber>
<CustomerName>JOHN MCGEE</CustomerName>
<GLMonth>12</GLMonth>
<GLYear>2018</GLYear>
<TransactionSubtypeCode>DirectBillMoneyReceivedTxn</TransactionSubtypeCode>
<TransactionSubtypeDesc>Direct Bill Money Received</TransactionSubtypeDesc>
<IssueDate>2018-12-07T13:40:37</IssueDate>
<PaymentMethod>cash</PaymentMethod>
<PolicyRiskState>AL</PolicyRiskState>
<ReasonCode>Direct Bill Money Received</ReasonCode>
<RecordCreationDate>2018-12-07T13:40:37</RecordCreationDate>
<Source>BILLING</Source>
<TransactionAmount>2570.77</TransactionAmount>
<TransactionCreateDate>2018-12-07T13:40:37</TransactionCreateDate>
</BillingTransactionInfo>
<BillingTransactionInfo xmlns="http://xed.com/bc/gx.billsactioninfomodel">
<AccountingDate>2018-12-07T13:40:44</AccountingDate>
<AccountingDay>7</AccountingDay>
<AccountingMonth>12</AccountingMonth>
<AccountingYear>2018</AccountingYear>
<AccountNumber>PC:0049207</AccountNumber>
<AccountType>insured</AccountType>
<BillingReferenceNumber>50000018100</BillingReferenceNumber>
<CustomerName>JOHN MCGEE</CustomerName>
<GLMonth>12</GLMonth>
<GLYear>2018</GLYear>
<TransactionSubtypeCode>DirectBillMoneyReceivedTxn</TransactionSubtypeCode>
<TransactionSubtypeDesc>Direct Bill Money Received</TransactionSubtypeDesc>
<IssueDate>2018-12-07T13:40:37</IssueDate>
<PaymentMethod>cash</PaymentMethod>
<PolicyRiskState>AL</PolicyRiskState>
<ReasonCode>Direct Bill Money Received</ReasonCode>
<RecordCreationDate>2018-12-07T13:40:37</RecordCreationDate>
<Source>BILLING</Source>
<TransactionAmount>2570.77</TransactionAmount>
<TransactionCreateDate>2018-12-07T13:40:37</TransactionCreateDate>
</BillingTransactionInfo>
xml tsql sql-server-2012
xml tsql sql-server-2012
New contributor
New contributor
New contributor
asked 22 hours ago
Dan KalioDan Kalio
81
81
New contributor
New contributor
How do you want to modify the values? It is easy to find all XMLs where the amount exceeds a given border, but you did not tell us, what else you want to place there...
– Shnugo
12 hours ago
Yes, I will like to modify the values to 10000.
– Dan Kalio
5 hours ago
add a comment |
How do you want to modify the values? It is easy to find all XMLs where the amount exceeds a given border, but you did not tell us, what else you want to place there...
– Shnugo
12 hours ago
Yes, I will like to modify the values to 10000.
– Dan Kalio
5 hours ago
How do you want to modify the values? It is easy to find all XMLs where the amount exceeds a given border, but you did not tell us, what else you want to place there...
– Shnugo
12 hours ago
How do you want to modify the values? It is easy to find all XMLs where the amount exceeds a given border, but you did not tell us, what else you want to place there...
– Shnugo
12 hours ago
Yes, I will like to modify the values to 10000.
– Dan Kalio
5 hours ago
Yes, I will like to modify the values to 10000.
– Dan Kalio
5 hours ago
add a comment |
3 Answers
3
active
oldest
votes
Welcome to Stack Overflow. This should be rather simple. First lets generate 50K rows of sample data.
IF OBJECT_ID('tempdb..#things') IS NOT NULL DROP TABLE #things;
DECLARE @xml XML =
'<BillingTransactionInfo xmlns="http://xed.com/bc/gx.billsactioninfomodel">
<AccountingDate>2018-12-07T13:40:44</AccountingDate>
<AccountingDay>7</AccountingDay>
<AccountingMonth>12</AccountingMonth>
<AccountingYear>2018</AccountingYear>
<AccountNumber>PC:0049207</AccountNumber>
<AccountType>insured</AccountType>
<BillingReferenceNumber>50000018100</BillingReferenceNumber>
<CustomerName>JOHN MCGEE</CustomerName>
<GLMonth>12</GLMonth>
<GLYear>2018</GLYear>
<TransactionSubtypeCode>DirectBillMoneyReceivedTxn</TransactionSubtypeCode>
<TransactionSubtypeDesc>Direct Bill Money Received</TransactionSubtypeDesc>
<IssueDate>2018-12-07T13:40:37</IssueDate>
<PaymentMethod>cash</PaymentMethod>
<PolicyRiskState>AL</PolicyRiskState>
<ReasonCode>Direct Bill Money Received</ReasonCode>
<RecordCreationDate>2018-12-07T13:40:37</RecordCreationDate>
<Source>BILLING</Source>
<TransactionAmount>2570.77</TransactionAmount>
<TransactionCreateDate>2018-12-07T13:40:37</TransactionCreateDate>
</BillingTransactionInfo>';
SELECT TOP (50000)
id = IDENTITY(INT,1,1),
X = CAST(REPLACE(CAST(@xml AS VARCHAR(8000)),
'<TransactionAmount>2570.77</TransactionAmount>',
CONCAT('<TransactionAmount>',ABS(CHECKSUM(NEWID())%2250000)*.01,'</TransactionAmount>')) AS XML)
INTO #things
FROM sys.all_columns a, sys.all_columns b;
Solution:
SELECT id, tr.amt
FROM #things AS t
CROSS APPLY (VALUES(t.X.value(
'(/*:BillingTransactionInfo/*:TransactionAmount/text())[1]','DECIMAL(10,2)'))) AS tr(amt)
WHERE tr.amt > 15000;
Returns:
id amt
----------- ------------
201 21876.97
202 21229.64
204 19188.62
209 21680.17
212 18603.47
213 20507.21
216 19536.31
218 19490.95
...
Updated to demonstrate how to modify the values to 10000 as per the OP's request.
UPDATE t
SET X.modify('
replace value of (/*:BillingTransactionInfo/*:TransactionAmount/text())[1]
with "10000"')
FROM #things AS t
CROSS APPLY (VALUES(t.X.value(
'(/*:BillingTransactionInfo/*:TransactionAmount/text())[1]','DECIMAL(10,2)'))) AS tr(amt)
WHERE tr.amt > 15000;
Thank you so much. I was able to select amounts greater than 15000. Please how could I update them?
– Dan Kalio
20 hours ago
@DanKalio I updated my answer to show how to modify the value. Let me know if you have other questions
– Alan Burstein
4 hours ago
add a comment |
Your XML has got a default namespace. It is possible to use a wildcard (like *:ElementName
), but it is recommended to declare namespaces.
Furthermore, the best way to filter for a value in cases, where you do not need that value is the native XML method .exist()
.
Try this:
WITH XMLNAMESPACES(DEFAULT('http://xed.com/bc/gx.billsactioninfomodel'))
UPDATE YourTable
SET TheXmlColumn.modify('replace value of (/BillingTransactionInfo/TransactionAmount/text())[1] with "12345"')
WHERE TheXmlColumn.exist('/BillingTransactionInfo[TransactionAmount > 15000]')=1;
You can read this as
- Use the given default namespace, when there is no explicit namespace
- Update your table and modify the XML in the stated way
- But hit only rows where there exists a
<BillingTransactionInfo>
with a<TransactionAmount>
higher then 15000.
Attention: Your sample does not look like this, but you must be sure, that your XML column will keep just one single <BillingTransactionInfo>
as statet in your question!
add a comment |
To find the rows which have TransactionAmount of more than 15000, something like this should work:
SELECT *
FROM TableName
WHERE ColumnWithXml.value('(/BillingTransactionInfo/TransactionAmount)[1]','int') > 15000
Where TableName is the name of the table and ColumnWithXml is the name of the XML column (needs to be of type XML)
To update those rows, something like this should work:
UPDATE TableName
SET ColumnWithXml.modify('replace value of
(/BillingTransactionInfo/TransactionAmount)[1] with ("25000")')
WHERE ColumnWithXml.value('(/BillingTransactionInfo/TransactionAmount)[1]','int') > 15000
See here (Where clauses on XML columns):
https://www.sqlservercentral.com/Forums/Topic1545273-392-1.aspx
and here (updating data):
https://www.mssqltips.com/sqlservertip/2738/examples-of-using-xquery-to-update-xml-data-in-sql-server/
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Dan Kalio is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54245172%2ft-sql-to-modify-xml-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Welcome to Stack Overflow. This should be rather simple. First lets generate 50K rows of sample data.
IF OBJECT_ID('tempdb..#things') IS NOT NULL DROP TABLE #things;
DECLARE @xml XML =
'<BillingTransactionInfo xmlns="http://xed.com/bc/gx.billsactioninfomodel">
<AccountingDate>2018-12-07T13:40:44</AccountingDate>
<AccountingDay>7</AccountingDay>
<AccountingMonth>12</AccountingMonth>
<AccountingYear>2018</AccountingYear>
<AccountNumber>PC:0049207</AccountNumber>
<AccountType>insured</AccountType>
<BillingReferenceNumber>50000018100</BillingReferenceNumber>
<CustomerName>JOHN MCGEE</CustomerName>
<GLMonth>12</GLMonth>
<GLYear>2018</GLYear>
<TransactionSubtypeCode>DirectBillMoneyReceivedTxn</TransactionSubtypeCode>
<TransactionSubtypeDesc>Direct Bill Money Received</TransactionSubtypeDesc>
<IssueDate>2018-12-07T13:40:37</IssueDate>
<PaymentMethod>cash</PaymentMethod>
<PolicyRiskState>AL</PolicyRiskState>
<ReasonCode>Direct Bill Money Received</ReasonCode>
<RecordCreationDate>2018-12-07T13:40:37</RecordCreationDate>
<Source>BILLING</Source>
<TransactionAmount>2570.77</TransactionAmount>
<TransactionCreateDate>2018-12-07T13:40:37</TransactionCreateDate>
</BillingTransactionInfo>';
SELECT TOP (50000)
id = IDENTITY(INT,1,1),
X = CAST(REPLACE(CAST(@xml AS VARCHAR(8000)),
'<TransactionAmount>2570.77</TransactionAmount>',
CONCAT('<TransactionAmount>',ABS(CHECKSUM(NEWID())%2250000)*.01,'</TransactionAmount>')) AS XML)
INTO #things
FROM sys.all_columns a, sys.all_columns b;
Solution:
SELECT id, tr.amt
FROM #things AS t
CROSS APPLY (VALUES(t.X.value(
'(/*:BillingTransactionInfo/*:TransactionAmount/text())[1]','DECIMAL(10,2)'))) AS tr(amt)
WHERE tr.amt > 15000;
Returns:
id amt
----------- ------------
201 21876.97
202 21229.64
204 19188.62
209 21680.17
212 18603.47
213 20507.21
216 19536.31
218 19490.95
...
Updated to demonstrate how to modify the values to 10000 as per the OP's request.
UPDATE t
SET X.modify('
replace value of (/*:BillingTransactionInfo/*:TransactionAmount/text())[1]
with "10000"')
FROM #things AS t
CROSS APPLY (VALUES(t.X.value(
'(/*:BillingTransactionInfo/*:TransactionAmount/text())[1]','DECIMAL(10,2)'))) AS tr(amt)
WHERE tr.amt > 15000;
Thank you so much. I was able to select amounts greater than 15000. Please how could I update them?
– Dan Kalio
20 hours ago
@DanKalio I updated my answer to show how to modify the value. Let me know if you have other questions
– Alan Burstein
4 hours ago
add a comment |
Welcome to Stack Overflow. This should be rather simple. First lets generate 50K rows of sample data.
IF OBJECT_ID('tempdb..#things') IS NOT NULL DROP TABLE #things;
DECLARE @xml XML =
'<BillingTransactionInfo xmlns="http://xed.com/bc/gx.billsactioninfomodel">
<AccountingDate>2018-12-07T13:40:44</AccountingDate>
<AccountingDay>7</AccountingDay>
<AccountingMonth>12</AccountingMonth>
<AccountingYear>2018</AccountingYear>
<AccountNumber>PC:0049207</AccountNumber>
<AccountType>insured</AccountType>
<BillingReferenceNumber>50000018100</BillingReferenceNumber>
<CustomerName>JOHN MCGEE</CustomerName>
<GLMonth>12</GLMonth>
<GLYear>2018</GLYear>
<TransactionSubtypeCode>DirectBillMoneyReceivedTxn</TransactionSubtypeCode>
<TransactionSubtypeDesc>Direct Bill Money Received</TransactionSubtypeDesc>
<IssueDate>2018-12-07T13:40:37</IssueDate>
<PaymentMethod>cash</PaymentMethod>
<PolicyRiskState>AL</PolicyRiskState>
<ReasonCode>Direct Bill Money Received</ReasonCode>
<RecordCreationDate>2018-12-07T13:40:37</RecordCreationDate>
<Source>BILLING</Source>
<TransactionAmount>2570.77</TransactionAmount>
<TransactionCreateDate>2018-12-07T13:40:37</TransactionCreateDate>
</BillingTransactionInfo>';
SELECT TOP (50000)
id = IDENTITY(INT,1,1),
X = CAST(REPLACE(CAST(@xml AS VARCHAR(8000)),
'<TransactionAmount>2570.77</TransactionAmount>',
CONCAT('<TransactionAmount>',ABS(CHECKSUM(NEWID())%2250000)*.01,'</TransactionAmount>')) AS XML)
INTO #things
FROM sys.all_columns a, sys.all_columns b;
Solution:
SELECT id, tr.amt
FROM #things AS t
CROSS APPLY (VALUES(t.X.value(
'(/*:BillingTransactionInfo/*:TransactionAmount/text())[1]','DECIMAL(10,2)'))) AS tr(amt)
WHERE tr.amt > 15000;
Returns:
id amt
----------- ------------
201 21876.97
202 21229.64
204 19188.62
209 21680.17
212 18603.47
213 20507.21
216 19536.31
218 19490.95
...
Updated to demonstrate how to modify the values to 10000 as per the OP's request.
UPDATE t
SET X.modify('
replace value of (/*:BillingTransactionInfo/*:TransactionAmount/text())[1]
with "10000"')
FROM #things AS t
CROSS APPLY (VALUES(t.X.value(
'(/*:BillingTransactionInfo/*:TransactionAmount/text())[1]','DECIMAL(10,2)'))) AS tr(amt)
WHERE tr.amt > 15000;
Thank you so much. I was able to select amounts greater than 15000. Please how could I update them?
– Dan Kalio
20 hours ago
@DanKalio I updated my answer to show how to modify the value. Let me know if you have other questions
– Alan Burstein
4 hours ago
add a comment |
Welcome to Stack Overflow. This should be rather simple. First lets generate 50K rows of sample data.
IF OBJECT_ID('tempdb..#things') IS NOT NULL DROP TABLE #things;
DECLARE @xml XML =
'<BillingTransactionInfo xmlns="http://xed.com/bc/gx.billsactioninfomodel">
<AccountingDate>2018-12-07T13:40:44</AccountingDate>
<AccountingDay>7</AccountingDay>
<AccountingMonth>12</AccountingMonth>
<AccountingYear>2018</AccountingYear>
<AccountNumber>PC:0049207</AccountNumber>
<AccountType>insured</AccountType>
<BillingReferenceNumber>50000018100</BillingReferenceNumber>
<CustomerName>JOHN MCGEE</CustomerName>
<GLMonth>12</GLMonth>
<GLYear>2018</GLYear>
<TransactionSubtypeCode>DirectBillMoneyReceivedTxn</TransactionSubtypeCode>
<TransactionSubtypeDesc>Direct Bill Money Received</TransactionSubtypeDesc>
<IssueDate>2018-12-07T13:40:37</IssueDate>
<PaymentMethod>cash</PaymentMethod>
<PolicyRiskState>AL</PolicyRiskState>
<ReasonCode>Direct Bill Money Received</ReasonCode>
<RecordCreationDate>2018-12-07T13:40:37</RecordCreationDate>
<Source>BILLING</Source>
<TransactionAmount>2570.77</TransactionAmount>
<TransactionCreateDate>2018-12-07T13:40:37</TransactionCreateDate>
</BillingTransactionInfo>';
SELECT TOP (50000)
id = IDENTITY(INT,1,1),
X = CAST(REPLACE(CAST(@xml AS VARCHAR(8000)),
'<TransactionAmount>2570.77</TransactionAmount>',
CONCAT('<TransactionAmount>',ABS(CHECKSUM(NEWID())%2250000)*.01,'</TransactionAmount>')) AS XML)
INTO #things
FROM sys.all_columns a, sys.all_columns b;
Solution:
SELECT id, tr.amt
FROM #things AS t
CROSS APPLY (VALUES(t.X.value(
'(/*:BillingTransactionInfo/*:TransactionAmount/text())[1]','DECIMAL(10,2)'))) AS tr(amt)
WHERE tr.amt > 15000;
Returns:
id amt
----------- ------------
201 21876.97
202 21229.64
204 19188.62
209 21680.17
212 18603.47
213 20507.21
216 19536.31
218 19490.95
...
Updated to demonstrate how to modify the values to 10000 as per the OP's request.
UPDATE t
SET X.modify('
replace value of (/*:BillingTransactionInfo/*:TransactionAmount/text())[1]
with "10000"')
FROM #things AS t
CROSS APPLY (VALUES(t.X.value(
'(/*:BillingTransactionInfo/*:TransactionAmount/text())[1]','DECIMAL(10,2)'))) AS tr(amt)
WHERE tr.amt > 15000;
Welcome to Stack Overflow. This should be rather simple. First lets generate 50K rows of sample data.
IF OBJECT_ID('tempdb..#things') IS NOT NULL DROP TABLE #things;
DECLARE @xml XML =
'<BillingTransactionInfo xmlns="http://xed.com/bc/gx.billsactioninfomodel">
<AccountingDate>2018-12-07T13:40:44</AccountingDate>
<AccountingDay>7</AccountingDay>
<AccountingMonth>12</AccountingMonth>
<AccountingYear>2018</AccountingYear>
<AccountNumber>PC:0049207</AccountNumber>
<AccountType>insured</AccountType>
<BillingReferenceNumber>50000018100</BillingReferenceNumber>
<CustomerName>JOHN MCGEE</CustomerName>
<GLMonth>12</GLMonth>
<GLYear>2018</GLYear>
<TransactionSubtypeCode>DirectBillMoneyReceivedTxn</TransactionSubtypeCode>
<TransactionSubtypeDesc>Direct Bill Money Received</TransactionSubtypeDesc>
<IssueDate>2018-12-07T13:40:37</IssueDate>
<PaymentMethod>cash</PaymentMethod>
<PolicyRiskState>AL</PolicyRiskState>
<ReasonCode>Direct Bill Money Received</ReasonCode>
<RecordCreationDate>2018-12-07T13:40:37</RecordCreationDate>
<Source>BILLING</Source>
<TransactionAmount>2570.77</TransactionAmount>
<TransactionCreateDate>2018-12-07T13:40:37</TransactionCreateDate>
</BillingTransactionInfo>';
SELECT TOP (50000)
id = IDENTITY(INT,1,1),
X = CAST(REPLACE(CAST(@xml AS VARCHAR(8000)),
'<TransactionAmount>2570.77</TransactionAmount>',
CONCAT('<TransactionAmount>',ABS(CHECKSUM(NEWID())%2250000)*.01,'</TransactionAmount>')) AS XML)
INTO #things
FROM sys.all_columns a, sys.all_columns b;
Solution:
SELECT id, tr.amt
FROM #things AS t
CROSS APPLY (VALUES(t.X.value(
'(/*:BillingTransactionInfo/*:TransactionAmount/text())[1]','DECIMAL(10,2)'))) AS tr(amt)
WHERE tr.amt > 15000;
Returns:
id amt
----------- ------------
201 21876.97
202 21229.64
204 19188.62
209 21680.17
212 18603.47
213 20507.21
216 19536.31
218 19490.95
...
Updated to demonstrate how to modify the values to 10000 as per the OP's request.
UPDATE t
SET X.modify('
replace value of (/*:BillingTransactionInfo/*:TransactionAmount/text())[1]
with "10000"')
FROM #things AS t
CROSS APPLY (VALUES(t.X.value(
'(/*:BillingTransactionInfo/*:TransactionAmount/text())[1]','DECIMAL(10,2)'))) AS tr(amt)
WHERE tr.amt > 15000;
edited 4 hours ago
answered 21 hours ago
Alan BursteinAlan Burstein
3,7821713
3,7821713
Thank you so much. I was able to select amounts greater than 15000. Please how could I update them?
– Dan Kalio
20 hours ago
@DanKalio I updated my answer to show how to modify the value. Let me know if you have other questions
– Alan Burstein
4 hours ago
add a comment |
Thank you so much. I was able to select amounts greater than 15000. Please how could I update them?
– Dan Kalio
20 hours ago
@DanKalio I updated my answer to show how to modify the value. Let me know if you have other questions
– Alan Burstein
4 hours ago
Thank you so much. I was able to select amounts greater than 15000. Please how could I update them?
– Dan Kalio
20 hours ago
Thank you so much. I was able to select amounts greater than 15000. Please how could I update them?
– Dan Kalio
20 hours ago
@DanKalio I updated my answer to show how to modify the value. Let me know if you have other questions
– Alan Burstein
4 hours ago
@DanKalio I updated my answer to show how to modify the value. Let me know if you have other questions
– Alan Burstein
4 hours ago
add a comment |
Your XML has got a default namespace. It is possible to use a wildcard (like *:ElementName
), but it is recommended to declare namespaces.
Furthermore, the best way to filter for a value in cases, where you do not need that value is the native XML method .exist()
.
Try this:
WITH XMLNAMESPACES(DEFAULT('http://xed.com/bc/gx.billsactioninfomodel'))
UPDATE YourTable
SET TheXmlColumn.modify('replace value of (/BillingTransactionInfo/TransactionAmount/text())[1] with "12345"')
WHERE TheXmlColumn.exist('/BillingTransactionInfo[TransactionAmount > 15000]')=1;
You can read this as
- Use the given default namespace, when there is no explicit namespace
- Update your table and modify the XML in the stated way
- But hit only rows where there exists a
<BillingTransactionInfo>
with a<TransactionAmount>
higher then 15000.
Attention: Your sample does not look like this, but you must be sure, that your XML column will keep just one single <BillingTransactionInfo>
as statet in your question!
add a comment |
Your XML has got a default namespace. It is possible to use a wildcard (like *:ElementName
), but it is recommended to declare namespaces.
Furthermore, the best way to filter for a value in cases, where you do not need that value is the native XML method .exist()
.
Try this:
WITH XMLNAMESPACES(DEFAULT('http://xed.com/bc/gx.billsactioninfomodel'))
UPDATE YourTable
SET TheXmlColumn.modify('replace value of (/BillingTransactionInfo/TransactionAmount/text())[1] with "12345"')
WHERE TheXmlColumn.exist('/BillingTransactionInfo[TransactionAmount > 15000]')=1;
You can read this as
- Use the given default namespace, when there is no explicit namespace
- Update your table and modify the XML in the stated way
- But hit only rows where there exists a
<BillingTransactionInfo>
with a<TransactionAmount>
higher then 15000.
Attention: Your sample does not look like this, but you must be sure, that your XML column will keep just one single <BillingTransactionInfo>
as statet in your question!
add a comment |
Your XML has got a default namespace. It is possible to use a wildcard (like *:ElementName
), but it is recommended to declare namespaces.
Furthermore, the best way to filter for a value in cases, where you do not need that value is the native XML method .exist()
.
Try this:
WITH XMLNAMESPACES(DEFAULT('http://xed.com/bc/gx.billsactioninfomodel'))
UPDATE YourTable
SET TheXmlColumn.modify('replace value of (/BillingTransactionInfo/TransactionAmount/text())[1] with "12345"')
WHERE TheXmlColumn.exist('/BillingTransactionInfo[TransactionAmount > 15000]')=1;
You can read this as
- Use the given default namespace, when there is no explicit namespace
- Update your table and modify the XML in the stated way
- But hit only rows where there exists a
<BillingTransactionInfo>
with a<TransactionAmount>
higher then 15000.
Attention: Your sample does not look like this, but you must be sure, that your XML column will keep just one single <BillingTransactionInfo>
as statet in your question!
Your XML has got a default namespace. It is possible to use a wildcard (like *:ElementName
), but it is recommended to declare namespaces.
Furthermore, the best way to filter for a value in cases, where you do not need that value is the native XML method .exist()
.
Try this:
WITH XMLNAMESPACES(DEFAULT('http://xed.com/bc/gx.billsactioninfomodel'))
UPDATE YourTable
SET TheXmlColumn.modify('replace value of (/BillingTransactionInfo/TransactionAmount/text())[1] with "12345"')
WHERE TheXmlColumn.exist('/BillingTransactionInfo[TransactionAmount > 15000]')=1;
You can read this as
- Use the given default namespace, when there is no explicit namespace
- Update your table and modify the XML in the stated way
- But hit only rows where there exists a
<BillingTransactionInfo>
with a<TransactionAmount>
higher then 15000.
Attention: Your sample does not look like this, but you must be sure, that your XML column will keep just one single <BillingTransactionInfo>
as statet in your question!
answered 12 hours ago
ShnugoShnugo
48.8k72566
48.8k72566
add a comment |
add a comment |
To find the rows which have TransactionAmount of more than 15000, something like this should work:
SELECT *
FROM TableName
WHERE ColumnWithXml.value('(/BillingTransactionInfo/TransactionAmount)[1]','int') > 15000
Where TableName is the name of the table and ColumnWithXml is the name of the XML column (needs to be of type XML)
To update those rows, something like this should work:
UPDATE TableName
SET ColumnWithXml.modify('replace value of
(/BillingTransactionInfo/TransactionAmount)[1] with ("25000")')
WHERE ColumnWithXml.value('(/BillingTransactionInfo/TransactionAmount)[1]','int') > 15000
See here (Where clauses on XML columns):
https://www.sqlservercentral.com/Forums/Topic1545273-392-1.aspx
and here (updating data):
https://www.mssqltips.com/sqlservertip/2738/examples-of-using-xquery-to-update-xml-data-in-sql-server/
add a comment |
To find the rows which have TransactionAmount of more than 15000, something like this should work:
SELECT *
FROM TableName
WHERE ColumnWithXml.value('(/BillingTransactionInfo/TransactionAmount)[1]','int') > 15000
Where TableName is the name of the table and ColumnWithXml is the name of the XML column (needs to be of type XML)
To update those rows, something like this should work:
UPDATE TableName
SET ColumnWithXml.modify('replace value of
(/BillingTransactionInfo/TransactionAmount)[1] with ("25000")')
WHERE ColumnWithXml.value('(/BillingTransactionInfo/TransactionAmount)[1]','int') > 15000
See here (Where clauses on XML columns):
https://www.sqlservercentral.com/Forums/Topic1545273-392-1.aspx
and here (updating data):
https://www.mssqltips.com/sqlservertip/2738/examples-of-using-xquery-to-update-xml-data-in-sql-server/
add a comment |
To find the rows which have TransactionAmount of more than 15000, something like this should work:
SELECT *
FROM TableName
WHERE ColumnWithXml.value('(/BillingTransactionInfo/TransactionAmount)[1]','int') > 15000
Where TableName is the name of the table and ColumnWithXml is the name of the XML column (needs to be of type XML)
To update those rows, something like this should work:
UPDATE TableName
SET ColumnWithXml.modify('replace value of
(/BillingTransactionInfo/TransactionAmount)[1] with ("25000")')
WHERE ColumnWithXml.value('(/BillingTransactionInfo/TransactionAmount)[1]','int') > 15000
See here (Where clauses on XML columns):
https://www.sqlservercentral.com/Forums/Topic1545273-392-1.aspx
and here (updating data):
https://www.mssqltips.com/sqlservertip/2738/examples-of-using-xquery-to-update-xml-data-in-sql-server/
To find the rows which have TransactionAmount of more than 15000, something like this should work:
SELECT *
FROM TableName
WHERE ColumnWithXml.value('(/BillingTransactionInfo/TransactionAmount)[1]','int') > 15000
Where TableName is the name of the table and ColumnWithXml is the name of the XML column (needs to be of type XML)
To update those rows, something like this should work:
UPDATE TableName
SET ColumnWithXml.modify('replace value of
(/BillingTransactionInfo/TransactionAmount)[1] with ("25000")')
WHERE ColumnWithXml.value('(/BillingTransactionInfo/TransactionAmount)[1]','int') > 15000
See here (Where clauses on XML columns):
https://www.sqlservercentral.com/Forums/Topic1545273-392-1.aspx
and here (updating data):
https://www.mssqltips.com/sqlservertip/2738/examples-of-using-xquery-to-update-xml-data-in-sql-server/
answered 21 hours ago
HaukurHafHaukurHaf
9,72352845
9,72352845
add a comment |
add a comment |
Dan Kalio is a new contributor. Be nice, and check out our Code of Conduct.
Dan Kalio is a new contributor. Be nice, and check out our Code of Conduct.
Dan Kalio is a new contributor. Be nice, and check out our Code of Conduct.
Dan Kalio is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54245172%2ft-sql-to-modify-xml-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How do you want to modify the values? It is easy to find all XMLs where the amount exceeds a given border, but you did not tell us, what else you want to place there...
– Shnugo
12 hours ago
Yes, I will like to modify the values to 10000.
– Dan Kalio
5 hours ago