Generate Sequence Code A-Z then 0-9 on SQL Server - sql-server

I have a case to generate sequence ID but alphabet first then numeric ( A-Z then 0-9 ) I see the question
here and try the solution, but the solution always give me 0-9 first then A-Z
WITH seq AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY x.alpha + y.number + z.number ) AS Id,
CONVERT(nchar(3), x.alpha + y.number + z.number) AS Result
FROM
(
VALUES
('0'), ('1'), ('2'), ('3'), ('4'), ('5'), ('6'), ('7'), ('8'), ('9'),
('A'), ('B'), ('C'), ('D'), ('E'), ('F'), ('G'), ('H'), ('I'), ('J'),
('K'), ('L'), ('M'), ('N'), ('O'), ('P'), ('Q'), ('R'), ('S'), ('T'),
('U'), ('V'), ('W'), ('X'), ('Y'), ('Z')
) x(alpha) ,
(
VALUES
('A'), ('B'), ('C'), ('D'), ('E'), ('F'), ('G'), ('H'), ('I'), ('J'),
('K'), ('L'), ('M'), ('N'), ('O'), ('P'), ('Q'), ('R'), ('S'), ('T'),
('U'), ('V'), ('W'), ('X'), ('Y'), ('Z'),
('0'), ('1'), ('2'), ('3'), ('4'), ('5'), ('6'), ('7'), ('8'), ('9')
) y(number),
(
VALUES
('A'), ('B'), ('C'), ('D'), ('E'), ('F'), ('G'), ('H'), ('I'), ('J'),
('K'), ('L'), ('M'), ('N'), ('O'), ('P'), ('Q'), ('R'), ('S'), ('T'),
('U'), ('V'), ('W'), ('X'), ('Y'), ('Z'), ('1'), ('2'), ('3'), ('4'), ('5'), ('6'), ('7'), ('8'), ('9')
) z(number)
)
SELECT * FROM seq WHERE Id = (SELECT Id + 1 FROM seq WHERE Result = 'A1Z')
the Sequence what should generated is :
AAA,...,AAZ,AA1,...,AA9,ABA,...,ABZ,AB1,..,AB9,...999
sample when I hit the A1Z the expected next value is A11, but the solution I tried give me A21 , is the behavior of SQL is start by 0-9 then A-Z , how to accomplish my requirement ?

Perhaps this is what you want? Numbers have lower value than letters in T-SQL, so when you order them then '9' has a lower number than 'A'. What you could do, however, is check if the value is a valid int value or not, and then order the values that aren't first, and then by the character.
This gives you the following:
WITH Characters AS(
SELECT V.C
FROM (VALUES('0'), ('1'), ('2'), ('3'), ('4'), ('5'), ('6'), ('7'), ('8'), ('9'),
('A'), ('B'), ('C'), ('D'), ('E'), ('F'), ('G'), ('H'), ('I'), ('J'),
('K'), ('L'), ('M'), ('N'), ('O'), ('P'), ('Q'), ('R'), ('S'), ('T'),
('U'), ('V'), ('W'), ('X'), ('Y'), ('Z')) V(C)),
Sequences AS(
SELECT CONCAT(C1.C,C2.C,C3.C) AS Sequence,
ROW_NUMBER() OVER (ORDER BY TRY_CONVERT(int,C1.C), C1.C, TRY_CONVERT(int,C2.C), C2.C, TRY_CONVERT(int,C3.C), C3.C) AS RN
FROM Characters C1
CROSS JOIN Characters C2
CROSS JOIN Characters C3)
SELECT *
FROM Sequences;
You didn't state where 0 goes, so I assumed 'AAZ','AA0', 'AA1'...

You can try to use ASCII function with a simple algorithm in ORDER BY, judging the char whether number or letter. if the char is number need to add 43
because the gap between Z (Capital letter) and 0 (first number) is 43, we need to add 43 to the number char to make sure the number is always behind letters.
WITH seq AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY IIF(x.alpha LIKE '[A-Z]', ASCII(x.alpha), ASCII(x.alpha) + 43),
IIF(y.number LIKE '[A-Z]', ASCII(y.number), ASCII(y.number) + 43),
IIF(z.number LIKE '[A-Z]', ASCII(z.number), ASCII(z.number) + 43)) AS Id,
CONVERT(nchar(3), x.alpha + y.number + z.number) AS Result
FROM
(
VALUES
('A'), ('B'), ('C'), ('D'), ('E'), ('F'), ('G'), ('H'), ('I'), ('J'),
('K'), ('L'), ('M'), ('N'), ('O'), ('P'), ('Q'), ('R'), ('S'), ('T'),
('U'), ('V'), ('W'), ('X'), ('Y'), ('Z'),
('0'), ('1'), ('2'), ('3'), ('4'), ('5'), ('6'), ('7'), ('8'), ('9')
) x(alpha) ,
(
VALUES
('A'), ('B'), ('C'), ('D'), ('E'), ('F'), ('G'), ('H'), ('I'), ('J'),
('K'), ('L'), ('M'), ('N'), ('O'), ('P'), ('Q'), ('R'), ('S'), ('T'),
('U'), ('V'), ('W'), ('X'), ('Y'), ('Z'),
('0'), ('1'), ('2'), ('3'), ('4'), ('5'), ('6'), ('7'), ('8'), ('9')
) y(number),
(
VALUES
('A'), ('B'), ('C'), ('D'), ('E'), ('F'), ('G'), ('H'), ('I'), ('J'),
('K'), ('L'), ('M'), ('N'), ('O'), ('P'), ('Q'), ('R'), ('S'), ('T'),
('U'), ('V'), ('W'), ('X'), ('Y'), ('Z'),
('1'), ('2'), ('3'), ('4'), ('5'), ('6'), ('7'), ('8'), ('9')
) z(number)
)
SELECT * FROM seq WHERE Id = (SELECT Id + 1 FROM seq WHERE Result = 'A1Z');
sqlfiddle

Related

Convert to array from database

From my database, I want to get this array
mySelectedEvents = {
"2022-09-13": [
{"eventDescp": "11", "eventTitle": "111"},
{"eventDescp": "22", "eventTitle": "22"}
],
"2022-09-30": [
{"eventDescp": "22", "eventTitle": "22"}
],
"2022-09-20": [
{"eventTitle": "ss", "eventDescp": "ss"}
]
};
These are my tables :
INSERT INTO `event_date` (`id`, `date`) VALUES
(1, '2022-09-13'),
(2, '2022-09-30'),
(3, '2022-09-20');
INSERT INTO `event_list` (`id`, `descript`, `title`, `id_event_date`) VALUES
(1, '11', '111', 1),
(2, '22', '22', 1),
(3, '22', '22', 2),
(4, 'ss', 'ss', 3);
[{"id":"1","date":"2022-09-13","descript":"11","title":"111","id_event_date":"1"},{"id":"2","date":"2022-09-13","descript":"22","title":"22","id_event_date":"1"},{"id":"3","date":"2022-09-30","descript":"22","title":"22","id_event_date":"2"},{"id":"4","date":"2022-09-20","descript":"ss","title":"ss","id_event_date":"3"}]
I tried this
Future<dynamic> getData() async {
var url = 'http://xxxxxxxxxx/getEvents.php';
var res = await http.get(Uri.parse(url));
var response = json.decode(res.body);
Map<String, dynamic> mySelectedEvents = Map.fromIterable(response,
key: (item) => item['date'],
value: (item) =>
{'eventDescp': item['descript'], 'eventTitle': item['title']});
print(mySelectedEvents);
return mySelectedEvents;
}
I think the code that you have written is good, are you sure that your database does have more than two elements?

Google Sheets transpose, split, repeat, keeping blanks

I am in way over my head and am getting lost amongst the multiple functions. I have got messy data that I have managed to get close to where I want it, but can't fine-tune it.
https://docs.google.com/spreadsheets/d/1wXM8DowXjzCt8etP1qYo6SsA-egIlm-UOp8RnfCYv44/edit?usp=sharing
There are notes in the Sheet that explain much of what I'm trying to do.
I have used this to duplicate the Plant column, but it also duplicates the following columns (not ideal but I can live with it)
=ArrayFormula(vlookup(transpose(split(query(rept(row(A2:E)&" ",J2:J),,9^9)," ")), {row(A2:E),A2:J},{2,3,4,5,6},0))
i tried this on the Nums and Storage column:
=ArrayFormula(SUBSTITUTE(QUERY(FLATTEN(TRIM(SPLIT(FILTER(F2:F&";*", F2:F<>""), ";", 0, 1))), "Select Col1 Where Col1 <> ''"), "*",, ))
but it adds spaces in the wrong place and expands to too many rows. Seems to add extra blanks.
Is this even possible?
use:
=ARRAYFORMULA({A1:I1; QUERY(REGEXREPLACE(""&SPLIT(FLATTEN(A2:A15&"×"&
IF(IFERROR(SPLIT(G2:G15, ";"), "2♦"&G2:G15)="",, REGEXREPLACE(SEQUENCE(1, 20)&"♦"&IF(B2:B15="", "3♦"&B2:B15, B2:B15), "^1♦", ))&"×"&
IF(IFERROR(SPLIT(G2:G15, ";"), "2♦"&G2:G15)="",, REGEXREPLACE(SEQUENCE(1, 20)&"♦"&IF(C2:C15="", "3♦"&C2:C15, C2:C15), "^1♦", ))&"×"&
IF(IFERROR(SPLIT(G2:G15, ";"), "2♦"&G2:G15)="",, REGEXREPLACE(SEQUENCE(1, 20)&"♦"&IF(D2:D15="", "3♦"&D2:D15, D2:D15), "^1♦", ))&"×"&
IF(IFERROR(SPLIT(G2:G15, ";"), "2♦"&G2:G15)="",, REGEXREPLACE(SEQUENCE(1, 20)&"♦"&IF(E2:E15="", "3♦"&E2:E15, E2:E15), "^1♦", ))&"×"&
IFERROR(SPLIT(F2:F15, ";"), "4♦"&F2:F15)&"×"&IFERROR(SPLIT(G2:G15, ";"), "5♦"&G2:G15)&"×"&
IF(IFERROR(SPLIT(G2:G15, ";"), "2♦"&G2:G15)="",, REGEXREPLACE(SEQUENCE(1, 20)&"♦"&IF(H2:H15="", "3♦"&H2:H15, H2:H15), "^1♦", ))&"×"&I2:I15),
"×"), ".*♦.*", ), "where Col9 is not null", 0)})
update:
=ARRAYFORMULA({A1:I1; QUERY(REGEXREPLACE(""&SPLIT(FLATTEN(A2:A9&"×"&
IF(IFERROR(SPLIT(G2:G9, ";"), "2♦"&G2:G9)="",, REGEXREPLACE(SEQUENCE(1, IFERROR(COLUMNS(SPLIT(G2:G9, ";")), 1))&"♦"&IF(B2:B9="", "3♦"&B2:B9, B2:B9), "^1♦", ))&"×"&
IF(IFERROR(SPLIT(G2:G9, ";"), "2♦"&G2:G9)="",, REGEXREPLACE(SEQUENCE(1, IFERROR(COLUMNS(SPLIT(G2:G9, ";")), 1))&"♦"&IF(C2:C9="", "3♦"&C2:C9, C2:C9), "^1♦", ))&"×"&
IF(IFERROR(SPLIT(G2:G9, ";"), "2♦"&G2:G9)="",, REGEXREPLACE(SEQUENCE(1, IFERROR(COLUMNS(SPLIT(G2:G9, ";")), 1))&"♦"&IF(D2:D9="", "3♦"&D2:D9, D2:D9), "^1♦", ))&"×"&
IF(IFERROR(SPLIT(G2:G9, ";"), "2♦"&G2:G9)="",, REGEXREPLACE(SEQUENCE(1, IFERROR(COLUMNS(SPLIT(G2:G9, ";")), 1))&"♦"&IF(E2:E9="", "3♦"&E2:E9, E2:E9), "^1♦", ))&"×"&
IFERROR(SPLIT(F2:F9, ";"), "4♦"&F2:F9)&"×"&IFERROR(SPLIT(G2:G9, ";"), "5♦"&G2:G9)&"×"&
IF(IFERROR(SPLIT(G2:G9, ";"), "2♦"&G2:G9)="",, REGEXREPLACE(SEQUENCE(1, IFERROR(COLUMNS(SPLIT(G2:G9, ";")), 1))&"♦"&IF(H2:H9="", "3♦"&H2:H9, H2:H9), "^1♦", ))&"×"&I2:I9),
"×"), ".*♦.*", ), "where Col9 is not null", 0)})

Solr DIH: documents are not nested

I'm trying to import some data as nested documents.
I've tried to simplify my problem as much as possible.
Here my parent query:
SELECT 1 AS id,
'xxx' AS col1,
'yyy' AS col2
UNION
SELECT 2 AS id,
'xxx' AS col1,
'yyy' AS col2
UNION
SELECT 3 AS id,
'xxx' AS col1,
'yyy' AS col2
You can see data here:
1 | xxx | yyy
2 | xxx | yyy
3 | xxx | yyy
My child query is:
SELECT 1 AS id,
1 AS rel_id,
'aaa' AS col1,
'bbb' AS col2
UNION
SELECT 2 AS id,
1 AS rel_id,
'aaa' AS col1,
'bbb' AS col2
UNION
SELECT 3 AS id,
2 AS rel_id,
'aaa' AS col1,
'bbb' AS col2
UNION
SELECT 4 AS id,
3 AS rel_id,
'aaa' AS col1,
'bbb' AS col2
Data is:
1 | 1 | aaa | bbb
2 | 1 | aaa | bbb
3 | 2 | aaa | bbb
4 | 3 | aaa | bbb
I'm trying to nest using this DIH configuration:
<entity
name="item"
query="select 1 as id, 'xxx' as col1, 'yyy' as col2 union select 2 as id, 'xxx' as col1, 'yyy' as col2 union select 3 as id, 'xxx' as col1, 'yyy' as col2">
<field column="id" name="id"/>
<field column="col1" name="column1_s" />
<field column="col2" name="column2_s" />
<entity
name="autor"
child="true"
query="select 1 as id, 1 as rel_id, 'aaa' as col1, 'bbb' as col2 union select 2 as id, 1 as rel_id, 'aaa' as col1, 'bbb' as col2 union select 3 as id, 2 as rel_id, 'aaa' as col1, 'bbb' as col2 union select 4 as id, 3 as rel_id, 'aaa' as col1, 'bbb' as col2"
cacheKey="rel_id" cacheLookup="item.id" cacheImpl="SortedMapBackedCache">
<field column="node_type" template="autor"/>
<field column="alt_code" name="id" template="${autor.id}-${autor.rel_id}"/>
<field column="col1" name="column1_s" />
<field column="col2" name="column2_s" />
</entity>
</entity>
However, they are not nested:
$ curl "http://localhost:8983/solr/arxius/select?q=*%3A*"
{
"responseHeader": {
"status": 0,
"QTime": 0,
"params": {
"q": "*:*"
}
},
"response": {
"numFound": 7,
"start": 0,
"numFoundExact": true,
"docs": [
{
"id": "1",
"column2_s": "bbb",
"column1_s": "aaa",
"_version_": 1682901366056419300
},
{
"id": "2",
"column2_s": "bbb",
"column1_s": "aaa",
"_version_": 1682901366056419300
},
{
"id": "1",
"column2_s": "yyy",
"column1_s": "xxx",
"_version_": 1682901366056419300
},
{
"id": "3",
"column2_s": "bbb",
"column1_s": "aaa",
"_version_": 1682901366058516500
},
{
"id": "2",
"column2_s": "yyy",
"column1_s": "xxx",
"_version_": 1682901366058516500
},
{
"id": "4",
"column2_s": "bbb",
"column1_s": "aaa",
"_version_": 1682901366058516500
},
{
"id": "3",
"column2_s": "yyy",
"column1_s": "xxx",
"_version_": 1682901366058516500
}
]
}
}
As you can see, documents are not nested.
I've been struggling a lot over that issue.
I've tried to strightforward the problem.
I hope I've explained so well.
Please, any ideas?

Trigger exists but is not showing up in Trigger folder under Server Objects, Database Triggers, or Table

I have looked at these threads and while they are similar, they do not answer my question.
Can't see the triggers that I created in SQL Server Management Studio 2008
Unable to find where triggers are stored in sql server 2008
Where does a Server trigger save in SQL Server?
In SSMS 17.9.1 (SQL Server 2017), I can see the trigger exists when using this code
select * from FocalAreas.sys.triggers
I can also see on the SharingPermissionTest (table where I want the trigger) > View Dependencies, the trigger is there. However, when I expand the SharingPermissionTest trigger folder, there is nothing there. When I expand the Programmability > Database Triggers on the database there is nothing there. When I expand the Server Objects > Triggers there is nothing there. Anybody have any insight into what's going on? This was my SQL to create the trigger:
USE FocalAreas
GO
CREATE TRIGGER dbo.SharingPermissionsTrigger
ON FocalAreas.dbo.FOCALREFERENCEAREAS
AFTER INSERT
AS BEGIN
DECLARE #FocalRefID nvarchar(50)
DECLARE #StateID nvarchar(2)
SELECT #FocalRefID = i.FocalRefID
FROM Inserted i
WHERE 1=1
SELECT #StateID = mp.StateID
FROM Inserted i, FocalAreas.dbo.MonitoringPoint as mp
WHERE i.FocalRefID = mp.FocalRefID
INSERT INTO FocalAreas.dbo.SharingPermissionsTest
Values
(next value for SharingPermissionSequence, #FocalRefID, 'NBTC', #StateID,
'Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed',
'Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed',
'Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed'
,'Not Allowed'),
(next value for SharingPermissionSequence, #FocalRefID, 'StateWildlifeAgency', #StateID,
'Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed',
'Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed',
'Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed'
,'Not Allowed'),
(next value for SharingPermissionSequence, #FocalRefID, 'FedPartners', #StateID,
'Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed',
'Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed',
'Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed'
,'Not Allowed'),
(next value for SharingPermissionSequence, #FocalRefID, 'NGO', #StateID,
'Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed',
'Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed',
'Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed'
,'Not Allowed'),
(next value for SharingPermissionSequence, #FocalRefID, 'Public', #StateID,
'Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed',
'Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed',
'Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed','Not Allowed'
,'Not Allowed')
END
GO
Expand the tree for the TABLE FocalAreas.dbo.FOCALREFERENCEAREAS, right click on the "Triggers" node/folder, click "Refresh", then expand the "Triggers" tree. The trigger should be there.
This is the solution from the first link you posted, by the way, but in the text of your question, you don't describe following this solution correctly. You mention "when I expand the SharingPermissionTest trigger folder, there is nothing there." but you never mention looking under the actual table that the trigger is on, which is what the answer in the linked question tells you to do.

SQL Server : returning the related xml with value, nodes methods

I have a the below sample XML schema that I am querying from in SQL Server 2012. I have a table with a XML column. I want to include the snippet of xml when each row is returned.
Below is the existing query and more detailed explanation.
<dev:Doc xmlns:dev="http://www.w3.org/2001/XMLSchema" Number="0" SchemaVersion="0.1" Settings="Testing" Title="Ordering">
<dev:Forms FormId="A1">
<dev:A1 ItemNumber="1" ItemGuid="{F402C584-555E-4D07-8C35-E88889B9DA44}">
<dev:Codes>D</dev:Codes>
<dev:Required>true</dev:Required>
<dev:Informational>false</dev:Informational>
<dev:Visitors>
<dev:Visitor Name="Dev01" Location="STLRF">
<dev:Divisions>
<dev:Division Number="1" Name="TFR3" Usage="Monitor">
<dev:Description>Development Fundamentals</dev:Description>
</dev:Division>
<dev:Division Number="2" Name="DEF32" Usage="Monitor">
<dev:Description>Testing Fundamentals</dev:Description>
</dev:Division>
<dev:Division Number="3" Name="DEP13" Usage="None">
<dev:Description>Guided Fundamentals</dev:Description>
</dev:Division>
</dev:Divisions>
</dev:Visitor>
<dev:Visitor Name="Dev02" Location="STLRF">
<dev:Divisions>
<dev:Division Number="1" Name="TFR3" Usage="Monitor">
<dev:Description>Development Fundamentals</dev:Description>
</dev:Division>
<dev:Division Number="2" Name="DEF32" Usage="Monitor">
<dev:Description>Testing Fundamentals</dev:Description>
</dev:Division>
<dev:Division Number="3" Name="DEP13" Usage="None">
<dev:Description>Guided Fundamentals</dev:Description>
</dev:Division>
</dev:Divisions>
</dev:Visitor>
<dev:Visitor Name="Dev03" Location="FGRTY">
<dev:Divisions>
<dev:Division Number="1" Name="TFR3" Usage="Monitor">
<dev:Description>Development Fundamentals</dev:Description>
</dev:Division>
<dev:Division Number="2" Name="DEF32" Usage="Monitor">
<dev:Description>Testing Fundamentals</dev:Description>
</dev:Division>
<dev:Division Number="3" Name="DEP13" Usage="None">
<dev:Description>Guided Fundamentals</dev:Description>
</dev:Division>
</dev:Divisions>
</dev:Visitor>
</dev:Visitors>
<dev:Senders>
<dev:Sender Name="FGY(14A)" />
</dev:Senders>
</dev:A1>
</dev:Forms>
<dev:Forms FormId="A2">
<dev:A2 ItemNumber="1" ItemGuid="{3563F33E-B03A-4859-850E-A87D35BD8562}">
<dev:Codes>C</dev:Codes>
<dev:Required>true</dev:Required>
<dev:Informational>false</dev:Informational>
<dev:Remarks>Support</dev:Remarks>
<dev:Notes>Ready</dev:Notes>
<dev:Visitors>
<dev:Visitor Name="GHFF">
<dev:Divisions>
<dev:Division Number="0" Name="Trial" Usage="None">
<dev:FromLocation>LOPO</dev:FromLocation>
<dev:ToLocation>RDSS</dev:ToLocation>
<dev:Description>Rich Filter</dev:Description>
</dev:Division>
</dev:Divisions>
</dev:Visitor>
</dev:Visitors>
<dev:Senders>
<dev:Sender Name="W33R" />
</dev:Senders>
<dev:IsReady>true</dev:IsReady>
<dev:IsCall>false</dev:IsCall>
</dev:A2>
<dev:A2 ItemNumber="2" ItemGuid="{CCFB2D5D-A23E-412D-8541-536451873713}">
<dev:Codes>A</dev:Codes>
<dev:Required>true</dev:Required>
<dev:Informational>false</dev:Informational>
<dev:Remarks>Loader Ready</dev:Remarks>
<dev:Notes>Ready</dev:Notes>
<dev:Visitors>
<dev:Visitor Name="UDT">
<dev:Divisions>
<dev:Division Number="0" Name="Trial" Usage="None">
<dev:FromLocation>TYUJ</dev:FromLocation>
<dev:ToLocation>DETF</dev:ToLocation>
<dev:Description>Web Enhance</dev:Description>
</dev:Division>
</dev:Divisions>
</dev:Visitor>
</dev:Visitors>
<dev:Senders>
<dev:Sender Name="RJ4" />
</dev:Senders>
<dev:IsReady>true</dev:IsReady>
<dev:IsCall>false</dev:IsCall>
</dev:A2>
</dev:Forms>
</dev:Doc>
Sample query
;WITH XMLNAMESPACES (Default 'http://www.w3.org/2001/XMLSchema' )
SELECT
a.value('#Number[1]', 'int') as Number
,b.value('(#FormId)[1]', 'NVARCHAR(50)') Form
--,XmlDocument.query('Doc/Forms') as FormXml,
,c.value('#ItemGuid[1]', 'uniqueidentifier') as ItemGuid
,c.value('#ItemNumber[1]', 'INT') AS ItemNumber
,d.value('(#Name)[1]','nvarchar(50)') As Visitor
,d.value('(#Location)[1]','nvarchar(50)') As Location
,e.value('(#Name)[1]', 'NVARCHAR(50)') As Sender
FROM
XmlTable As X
CROSS APPLY XmlDocument.nodes('Doc') As aa(a)
CROSS APPLY a.nodes('Forms') As bb(b)
CROSS APPLY b.nodes('*') As cc(c)
CROSS APPLY c.nodes('Visitors/Visitor') as dd(d)
CROSS APPLY c.nodes('Senders/Sender') as ee(e)
What I would like to do is include the snippet of xml after the Form column to show the snippet it came from.
I am trying this
XmlDocument.query('Doc/Forms') as FormXml,
The above is returning from that element down, I just want to return everything from <dev:Forms FormId="A1"> to the </dev:A1> for each row related to A1. So when Form is A2 I want A2 - /A2 <--- everything between
I believe what you want to get is the XML that is referenced by the b "table", no?
Try this line:
, b.query('.') as FormXml
Does that give you want you're looking for? It should give you the XML of the <Forms> node.

Resources