将xml值分配给局部变量并与日期进行比较

时间:2018-09-21 15:37:26

标签: sql sql-server casting

是否可以将节点值声明为局部变量,例如

DECLARE @xVal1 <type>;
SET @xVal1 = '/*:OrderWithItems/*:Order/*:ShipServiceLevel)[1]';

,然后在 WHERE 条件下参考下面的@ xVal1,而不是完整的xml路径。

 SELECT o.nOrderId AS 'OrderId',
    o.Source,
    o.Subsource,
    lc.cCountry,
    CAST(ObjectXML as xml).value('(/*:OrderWithItems/*:Order/*:ShipServiceLevel)[1]', 'NVARCHAR(max)') AS 'ServiceLevel',
    CAST(ObjectXML as xml).value('(/*:OrderWithItems/*:Order/*:LatestDeliveryDate)[1]', 'datetime') AS 'LatestDeliveryDate'
    FROM [OrderXML] x
    INNER JOIN [Open_Order] o ON o.pkOrderId = x.fkOrderId
    INNER JOIN [ListCountries] lc ON lc.pkCountryId = o.fkCountryId
    WHERE 
    o.Source = 'AMAZON' AND
    o.Subsource IN ('Trimming Shop', 'Wedding Decor - UK') AND
    lc.cCountry = 'United Kingdom' AND
    x.type = 'Default' AND
    CAST(ObjectXML as xml).value('(/*:OrderWithItems/*:Order/*:ShipServiceLevel)[1]', 'NVARCHAR(max)') = 'UK Next' AND
    CAST(ObjectXML as xml).value('(/*:OrderWithItems/*:Order/*:LatestDeliveryDate)[1]', 'datetime') = DATEADD(day, 1, GETDATE())

这是结果集:

Query Result

我想将 LatestDeliveryDate 值转换为日期格式 DD-MM-YYYY

0 个答案:

没有答案