Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8677993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:36:08+00:00 2026-06-12T20:36:08+00:00

I have an XML column that looks something like the following; DECLARE @str AS

  • 0

I have an XML column that looks something like the following;

DECLARE @str AS VARCHAR(8000)

SET @str = '<Root xmlns="http://myurl.com/services/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
           <Notifications>
               <Notification>
                  <NotificationDate>2012-08-16</NotificationDate>
                  <Scopes>
                    <NotificationScope>
                        <Code>a</Code>
                    </NotificationScope>  
                    <NotificationScope>
                        <Code>b</Code> 
                    </NotificationScope>
                  </Scopes>
                </Notification>
                <Notification>
                  <NotificationDate>2012-08-20</NotificationDate>
                  <Scopes>
                    <NotificationScope>
                        <Code>a</Code>
                    </NotificationScope>
                  </Scopes>
                </Notification>
            </Notifications></Root>'

I want to be able to get a count of the number of NotificationScope Elements per notification. So for example, I am looking for something like;

Notification Date                   Count
 2012-08-16                          2
 2012-08-20                          1

In fact, as in reality as this data is obtained from a database column, I am really only interested in returning those records that have ANY Notification Date with a Count that is greater than 1.

So far, all I’ve managed to come up with is the following, but that just gives me a count of ;

declare @xmlvar XML;
set @xmlvar = (SELECT cast(@str AS XML))
;WITH XMLNAMESPACES('http://myurl.com/services/' AS p)
SELECT * FROM (
select @xmlvar.value('count(/p:Root/p:Notifications/p:Notification/p:Scopes/p:NotificationScope)', 'INT') AS 'NotificationScopeCount',
       @xmlvar.value('count(/p:Root/p:Notifications/p:Notification/p:NotificationDate)', 'INT') AS 'NotificationDateCount'
 ) a WHERE NotificationScopeCount > NotificationDateCount

but ideally, I’d like to be able to get the associated date also. Please forgive me if this is a horrible way of going about this, I haven’t used XML datatypes much before in SQL.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:36:09+00:00Added an answer on June 12, 2026 at 8:36 pm

    You can try to use something like this:

    ;WITH XMLNAMESPACES('http://myurl.com/services/' AS p)
    SELECT
        NotificationDate = Notif.value('(p:NotificationDate)[1]', 'DATE'),
        CodeCount = Notif.value('count(p:Scopes/p:NotificationScope/p:Code)', 'int')
    FROM
        @xmlvar.nodes('/p:Root/p:Notifications/p:Notification') AS Tbl(Notif)
    

    Gives me an output of:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I have some XML that looks like this: DECLARE @xml XML SET @xml
I have an xml document that looks something like this: <units> <unit> <year></year> <month></month>
Say, I have a xml column that looks like this: <mi> <m>42</m> </mi> Assuming
I have a XML column in a table that looks like this: <word A=al
I have a local sqlite database file that looks something like this… _ID |
I have code which parses XML that looks like this: <custom_fields> <custom_field> <column_name>foo</column_name> <column_value>0</column_value>
I have table with a XML column (called MetaData) which looks like this: <props>
I've got a very large xml data set that is structured like the following:
I have an XML variable that looks like this: <code> <IDs> <ID id=1>a</ID> <ID
I have some XML code that looks like this <SEARCHRESULTS> <FUNCTION name=BarGraph> <PARAMETER name=numList></PARAMETER>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.