SQL Server 2008 - 将 XML 分解到表中需要将 UNKNOWN DATE 值保留为 NULL

发布于 2024-09-24 10:54:43 字数 1554 浏览 4 评论 0原文

我想知道是否有人可以帮助

我在 SQL Server2008 中成功地将 XML 文档导入到具有 XML 数据类型的表中,但是当我尝试将任何没有输入的 DATE 值从该表粉碎到临时表时日期作为 1900-01-01 插入到临时表中。

我是否缺少一种切割方法来阻止它执行此操作,而只需插入 NULL 即可。我可以在临时表上使用 NULLIF 并将 1900-01-01 替换为 null,但我不愿意这样做,以防存在真正的 1900-01-01 值。

我的代码看起来像这样

SELECT tab.col.value('LastDate[1]','DATE') LastARD'

FROM   import.XMLCompanyDetail

 CROSS APPLY
          xmldata.nodes('//VN/CompanyList/Row') AS tab(col)

非常感谢

请参阅下面我正在导入的示例 XML

<?xml version="1.0" encoding="ISO-8859-1" ?>
<VN>
<CompanyList>
<Row num="1"><CoNum>7878</CoNum><CoName>ExampleName</CoName><DInc>1978-12-30</DInc><DDis></DDis></Row>
</CompanyList>
</VN>

日期 DDIS 应该为 NULL,但是当它导入到我的临时表时,它会插入 1901-01-01。

更新了代码以表明我的意思

create table staging_table
(DInc DATE NULL, LastARD DATE NULL);

with XMLCompanyDetail as 
(
SELECT CAST('<?xml version="1.0" encoding="ISO-8859-1" ?> 
<VN>
<CompanyList>
<Row num="1"><CoNum>7878</CoNum><CoName>ExampleName</CoName><DInc>1978-12-30</DInc><DDis></DDis></Row> 
</CompanyList>
</VN>
' AS XML) AS xmldata
)

INSERT INTO Staging_Table
SELECT tab.col.value('DInc[1]','DATE') DInc,
tab.col.value('DDis[1]','DATE') LastARD
FROM XMLCompanyDetail
CROSS APPLY
xmldata.nodes('//VN/CompanyList/Row') AS tab(col)

SELECT * FROM Staging_table
drop table staging_table

I was wondering if someone could help

I've successfully been able to import an XML document to a table with an XML data type in SQL Server2008, how ever when I try and shred from that table to a staging table any DATE values without an entered date are inserted to the staging table as 1900-01-01.

Is there a cleaver way i'm missing to stop it doing this and simply insert NULL instead. I could use NULLIF on the staging table and replace 1900-01-01 with null but I’m reluctant to do this in case there are genuine 1900-01-01 values.

My code looks something like this

SELECT tab.col.value('LastDate[1]','DATE') LastARD'

FROM   import.XMLCompanyDetail

 CROSS APPLY
          xmldata.nodes('//VN/CompanyList/Row') AS tab(col)

Many thanks

Please see below the example XML i'm importing

<?xml version="1.0" encoding="ISO-8859-1" ?>
<VN>
<CompanyList>
<Row num="1"><CoNum>7878</CoNum><CoName>ExampleName</CoName><DInc>1978-12-30</DInc><DDis></DDis></Row>
</CompanyList>
</VN>

The date DDIS should be NULL but rather when it imports to my staging table it inserts 1901-01-01 instead.

updated code to show what I mean

create table staging_table
(DInc DATE NULL, LastARD DATE NULL);

with XMLCompanyDetail as 
(
SELECT CAST('<?xml version="1.0" encoding="ISO-8859-1" ?> 
<VN>
<CompanyList>
<Row num="1"><CoNum>7878</CoNum><CoName>ExampleName</CoName><DInc>1978-12-30</DInc><DDis></DDis></Row> 
</CompanyList>
</VN>
' AS XML) AS xmldata
)

INSERT INTO Staging_Table
SELECT tab.col.value('DInc[1]','DATE') DInc,
tab.col.value('DDis[1]','DATE') LastARD
FROM XMLCompanyDetail
CROSS APPLY
xmldata.nodes('//VN/CompanyList/Row') AS tab(col)

SELECT * FROM Staging_table
drop table staging_table

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪#恶を代价 2024-10-01 10:54:43

编辑

更新问题后,以下内容有效,但我不确定是否有更好的方法。

SELECT tab.col.value('DInc[1][. != '''']','DATE') DInc,
tab.col.value('DDis[1][. != '''']','DATE') LastARD
FROM XMLCompanyDetail
CROSS APPLY
xmldata.nodes('//VN/CompanyList/Row') AS tab(col)

Edit

Following Update to the question the following works but I'm not sure if there is a better way.

SELECT tab.col.value('DInc[1][. != '''']','DATE') DInc,
tab.col.value('DDis[1][. != '''']','DATE') LastARD
FROM XMLCompanyDetail
CROSS APPLY
xmldata.nodes('//VN/CompanyList/Row') AS tab(col)
小伙你站住 2024-10-01 10:54:43

尝试改用datetime

declare @xml xml
set @xml = ''

select @xml.value('LastDate[1]','datetime') 

等等,这会返回null。这意味着,在插入临时表后,此 null 值将被该临时表中的默认设置覆盖。因此,如果您不希望发生此行为,请将其删除。

Try to use datetime instead:

declare @xml xml
set @xml = ''

select @xml.value('LastDate[1]','datetime') 

Wait, this returns null. That means that upon insert to your staging table this null value gets overridden by the default setting in that staging table. So remove that if you don't want this behavior to occur.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文