I am working on a Microsoft SQL Server table that contains multiple datatypes, including a column for xml. Example:
CREATE TABLE [sites]
(
[site_id] [int] NOT NULL,
[organization_id] [int] NULL,
[site_creationDate] [datetime] NULL,
[site_active] [bit] NULL,
[site_activatedBy] [varchar](20) NULL,
[contentModel_id] [int] NULL,
[site_settings] [xml] NULL
);
Here's what the XML looks like in the [site_settings] column:
Example 1:
<settings>
<s key="hasinventorynotifier" value="0" type="BIT" />
<s key="displayinstocknotifier" value="1" type="BIT" />
<s key="usesimplequoteform" type="BIT" value="1" />
</settings>
Example 2:
<settings>
<s key="isvisionimpaired" type="Bit" value="0" />
<s key="insurancerequesturl" type="String" value="" />
<s key="haswheelstudio" type="Bit" value="0" />
<s key="hasironfunnel" type="Bit" value="0" />
<s key="haselasticsearch" type="Bit" value="0" />
<s key="hasinventorynotifier" type="Bit" value="0" />
<s key="themeconfigurationjson" type="String" value="{&quot;siteHeader&quot;:{&quot;scheme&quot;:&quot;light&quot;,&quot;headerLinks&quot;:false,&quot;navType&quot;:&quot;expanded&quot;},&quot;body&quot;:{&quot;grid&quot;:&quot;compact&quot;},&quot;siteFooter&quot;:{&quot;scheme&quot;:&quot;light&quot;},&quot;logo&quot;:{&quot;enabled&quot;:true,&quot;imageUrl&quot;:&quot;//cdnmedia.example.com/images/organizations/7a1fd181-1154-4cb6-8c43-a6f354b9f824/logo/dealer-logo.gif?v=1455044570132&quot;}}" />
<s key="hasautoquote" type="Bit" value="0" />
<s key="usesimplequoteform" type="Bit" value="1" />
<s key="overrideinventorysearchtitle" type="Bit" value="1" />
<s key="overrideshowcasetitle" type="Bit" value="1" />
<s key="financingexternalurl" type="String" value="" />
<s key="disablemakeofferrequest" type="Bit" value="0" />
<s key="enablereceiveoffers" type="Bit" value="1" />
<s key="vehiclehistoryurl" type="String" value="" />
<s key="displayinstocknotifier" type="Bit" value="1" />
<s key="disableautoresponseemail" type="Bit" value="0" />
<s key="enablesolditemsremoval" type="Bit" value="0" />
<s key="usefinancingexternalurl" type="Bit" value="0" />
</settings>
As you can see from the above examples, the xml column is not very consistent from row to row and do not display all the "keys". (Note, I did not design this database.)
I want to create a view that shows all the columns, but also extracts some values from the xml structure in the [site_settings] column and display them as new columns in my view. I think our xml is kind of funky, as all the examples I've googled on trying to do this have failed for me.
Here's what kind of what I want:
CREATE VIEW [vw_Sites]
AS
SELECT
[site_id], [organization_id],
[site_creationDate], [site_active], [site_activatedBy],
[contentModel_id], [hasinventorynotifier],
[displayinstocknotifier], [usesimplequoteform],
[isvisionimpaired] (and more xml keys, etc...)
FROM
[sites] WITH (NOLOCK)
GO
I've tried a few methods of extracting via selecting the value of the column in these queries below, but the results come back NULL or blank.
SELECT
[site_settings].value('usesimplequoteform[1]','bit') AS Test1,
[site_settings].value('settings[1]','varchar(10)') AS Test2,
[site_settings].value('(s[@key="usesimplequoteform"]/@value)[1]','int') AS Test3
FROM
[sites]
Alternatively, a colleague of mines suggested using the following case statements, which do work, but I was wondering if there was a better way. Case example below:
SELECT
CASE WHEN CHARINDEX('usesimplequoteform" type="Bit" value="1"',CONVERT(VARCHAR(MAX),site_settings),1) > 0 THEN 1
WHEN CHARINDEX('usesimplequoteform" value="1" type="Bit"',CONVERT(VARCHAR(MAX),site_settings),1) > 0 THEN 1
ELSE 0
END AS useSimpleQuoteForm
FROM [sites]
But does anyone have a better way? The above case examples will only work on bit variables, and some of them are of type string (varchar). Any assistance would be greatly appreciated!
Clarification for output
Basically, the output would be a view that contains all the current columns from table [sites] and additional columns for each of the keys that can be present in the [sites].[site_settings] column.
[site_id] [organization_id] [site_creationDate] [site_active] [site_activatedBy] [contentModel_id] [hasinventorynotifier] [displayinstocknotifier] [usesimplequoteform] [the keys etc in xml]
5036 4886 2016-01-04 10:16:01.860 1 system 8 1 0 1 (value)
5037 2386 2015-01-05 11:29:23.880 1 system 2 0 0 1 (value)