简体   繁体   English

如何从 SQL Server 中的长字符串中提取日期

[英]How to extract date from a long string in SQL Server

I a long string like below in my column and I need to extract only the date ( 2021-07-05 ) from it.我的专栏中有一个像下面这样的长字符串,我只需要从中提取日期( 2021-07-05 )。

Could anyone please help?有人可以帮忙吗?

Name - Koteswararao vp Department - Prod Location - Hyderabad Domain - ND Job Title - Technical-ABAP Date of Hire - 2021-07-05 Vendor ID - v2345 Candidate ID - Associate ID - 3334 Name - Hardware Allocation

We can use PATINDEX with SUBSTRING here:我们可以在这里使用PATINDEXSUBSTRING

SELECT
    col,
    SUBSTRING(col,
              PATINDEX('%[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]%', col),
              10) AS date
FROM yourTable;

Demo 演示

The call to PATINDEX above finds the starting position of the date, while SUBSTRING takes 10 characters from that position.上面对PATINDEX的调用查找日期的起始位置,而SUBSTRING从该位置获取 10 个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM