如何使用 TSQL for SQL Server 从文本字段中提取日期
我正在尝试从文本字段中提取日期。我正在使用 PADINDEX 来执行此操作。我的结果非常不一致。无论如何,我可以通过 SQL 来做到这一点吗?我没有可以为我执行此操作的应用程序。我正在尝试将此作为所需的报告。
文本字段的日期并不总是列为 MM/DD/YYYY,有时会列为 M/DD/YYYY。另外,
这是我正在使用的查询:
select
substring(ar.finding_text,patindex('%[0-9]%/[0-9][0-9]/[0-9][0-9][0-9][0-9]%',ar.finding_text),10)
FROM [ARKPPDB].[PowerPath].[dbo].[accession_2] a
LEFT OUTER JOIN acc_results ar on a.id = ar.acc_id
I'm trying to extract date from a text field. I'm using PADINDEX to do this. My results are very inconsistent. Is there anyway I can do this through SQL. I don't have an application to do this for me. I am trying to get this for a report that is needed.
The text field has dates that are not always listed as MM/DD/YYYY sometimes its listed as M/DD/YYYY. Also,
Here is the query I am using:
select
substring(ar.finding_text,patindex('%[0-9]%/[0-9][0-9]/[0-9][0-9][0-9][0-9]%',ar.finding_text),10)
FROM [ARKPPDB].[PowerPath].[dbo].[accession_2] a
LEFT OUTER JOIN acc_results ar on a.id = ar.acc_id
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我得到了这个工作。感谢大家的帮助。
I got this working. Thank you all for your assistance.
我从另一个线程中获得了有关这篇文章的更多信息,包括数据实际外观的提示。这是我创建的响应的相同代码部分,包括测试数据(如果其他人想玩的话)。
I got a little more information about this post from another thread including a hint of what the data actually looks like. Here's the same code part of the response I created including the test data if someone else wants to play.
您可以添加这样的模式“优先级”:
因此,一些示例文本:
这应该返回“2/1/2022 3:27 PM”,您应该能够将其转换为 DATETIME。我没有打扰中央。您展示的每个示例都包含中部时间,因此您可以假设现在是中部时间。
You could add a pattern "priority" like this:
So, some example text:
This should return '2/1/2022 3:27 PM' which you should be able to convert to a DATETIME. I did not bother with the Central. Every example you showed included Central, so you can probably just assume it is Central time.