从 SQL 中的无格式文本解析日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1411072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:34:10  来源:igfitidea点击:

Parse a date from unformatted text in SQL

sqlsql-servertsql

提问by Beska

I'm trying to figure out an elegant way to get a date from a text column that has data like this "YYYYMMDD"...so we might see "20060508" as a value in the column, which I would like to be able to return from a query as a date (May 8, 2006).

我正在尝试找出一种优雅的方法来从包含“YYYYMMDD”这样的数据的文本列中获取日期……所以我们可能会看到“20060508”作为列中的一个值,我希望能够从查询返回作为日期(2006 年 5 月 8 日)。

I'm sure I can hack something together given enough time, but the approaches I'm thinking of seem pretty kludgy, and I suspect there's a way this can be elegantly done in a single query.

我确信如果有足够的时间,我可以一起破解一些东西,但是我想到的方法看起来很笨拙,我怀疑有一种方法可以在单个查询中优雅地完成。

Any suggestions?

有什么建议?

回答by marc_s

This is already a valid date - ISO-8601 format - just use:

这已经是一个有效的日期 - ISO-8601 格式 - 只需使用:

SELECT CAST('20060508' AS DATETIME)

or alternatively:

或者:

SELECT CONVERT(DATETIME, '20060508', 112)

and that should do just fine!

那应该没问题!

In order to get your "May 08, 2006" display, do another convert into varchar, using the date convert style 107:

为了显示“2006 年 5 月 8 日”,请使用日期转换样式 107 再次转换为 varchar:

SELECT CONVERT(VARCHAR(25), CAST('2006-05-08' AS DATETIME), 107)

See herefor more information on casting & converting in MS SQL

有关在 MS SQL 中进行转换和转换的更多信息,请参见此处

回答by Scott Ivey

select cast('20060508' as datetime) AS MyDate

gives you this result...

给你这个结果......

MyDate
-----------------------
2006-05-08 00:00:00.000

See herefor more information on casting & converting in MS SQL.

有关在 MS SQL 中进行强制转换和转换的更多信息,请参见此处

If you're trying to achieve a specific format (May 8, 2006) - you should consider just returning the column as a datetime value, and letting whatever is going to display that value to the end user (website, client app, report, etc) do the formatting. If you format it at the query, you'll be returning a string, which will make it harder to swap out formats from your front end in the future. If you want to do it in SQL - check out format 107 in the link above.

如果您正在尝试实现特定格式(2006 年 5 月 8 日) - 您应该考虑将列作为日期时间值返回,并让任何将要向最终用户显示该值的内容(网站、客户端应用程序、报告、等)进行格式化。如果您在查询时对其进行格式化,您将返回一个字符串,这将使将来更难从前端换出格式。如果您想在 SQL 中执行此操作,请查看上面链接中的格式 107。

回答by Chris McCall

No sweat, just CONVERTit. Style "112", or ISO would handle your example case.

没有汗水,只需转换它。样式“112”或 ISO 将处理您的示例案例。

SELECT CONVERT(datetime,'20060508',112)

returns

回报

 ----------------------- 2006-05-08 00:00:00.000

 (1 row(s) affected)

回答by KM.

try:

尝试:

SELECT REPLACE(CONVERT(varchar(30),CONVERT(datetime, '20060508'),107),' 0',' ')

output:

输出:

-------------------
May 8, 2006

(1 row(s) affected)