简体   繁体   English

如何将子字符串中的数字转换为 SQL 中的日期格式?

[英]How to convert a number in a substring to date format in SQL?

How do I format this particular example to a dd-mm-yyyy format?如何将此特定示例格式化为 dd-mm-yyyy 格式?

In table say table1, we have a string in the following format where I need to take the date value out.在 table1 中,我们有一个以下格式的字符串,我需要在其中取出日期值。

AB-20092018-data1 
CD-21918-notsodata1
EF-31012017-data1
GH-31117-notsodata

As above your 31117 records are going to be quite hard to determine what the actual date is supposed to be.如上所述,您的31117记录将很难确定实际日期应该是什么。

You can do something very messy like this to get your date format out of the ones that actually have a date in it.你可以做一些像这样非常混乱的事情来让你的日期格式脱离那些实际有日期的格式。

SELECT   cast(right(convert1, 4) + '/' + substring(convert1, 3, 2) + '/' + left(convert1, 2) as date) as LongDate1
FROM    (
    select left(substring('EF-31012017-data1',CHARINDEX('-','EF-31012017-data1') +1,len('EF-31012017-data1')),charindex('-',substring('EF-31012017-data1',CHARINDEX('-','EF-31012017-data1') +1,len('EF-31012017-data1')))-1) Convert1
) longer

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

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