简体   繁体   English

日期转换从“JAN02/19 到 2019-01-02

[英]Date conversion from "JAN02/19 to 2019-01-02

如何将 varchar(25) 列的值从“JAN02/19”格式的日期转换为“2019-01-02”(YYYY-MM-DD)?

Perhaps something like this也许像这样

Declare @S varchar(25)='JAN02/19'

Select try_convert(date,replace(@S,'/',' 20'))

Returns退货

2019-01-02

You can do something like below:您可以执行以下操作:

DECLARE @Date Varchar(10)
Set @Date='JAN02/19'
select DATEFROMPARTS('20'+substring(@Date,7,2),CHARINDEX(@Date,'JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC')/4+1,substring(@Date,4,2))

This should give the output in the desired format.这应该以所需的格式提供输出。

Edit:编辑:

To accomodate improper month values:要容纳不正确的月份值:

DECLARE @Date Varchar(10)
Set @Date='JAN02/19'
select DATEFROMPARTS('20'+substring(@Date,7,2),NULLIF(CHARINDEX(substring(@Date,1,3),'JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC'),0)/4+1,substring(@Date,4,2))

This will return NULL in case the month values are not proper.如果月份值不正确,这将返回 NULL。

暂无
暂无

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

相关问题 日期字符串中使用的日期格式是什么,'2019-01-21T19:02:25Z' - What is the date format used in date string, '2019-01-21T19:02:25Z' 将日期时间转换为日期字符串,从 2019-05-02 12:00:00 到 2019-05-02 - to convert datetime into date string from 2019-05-02 12:00:00 to 2019-05-02 从日期时间中提取日期,格式为2015-01-01-15:00:02:30 - Extract date from date time in the format 2015-01-01-15:00:02:30 如何在SQL中的日期范围内执行累积计算? (例如,针对01的计算使用来自01的数据,对于02使用01和02,依此类推) - How can I perform cumulative calculations over a date range in SQL? (e.g. calculation for 01 uses data from 01, 02 uses 01 and 02, and so on) 转换nvarchar为datetime-格式19/02/2019 12.00.00 AM - Convert nvarchar to datetime - format 19/02/2019 12.00.00 AM 当日期前导零时,VBA 错误匹配表中的日期和查询中的日期,例如 01/04 或 02/04 - VBA error matching date from table and date from query when date has leading zero eg 01/04 or 02/04 问一下php总结01 + 01 = 02 - ask about php summarize 01 + 01 = 02 截断了错误的日期值:“上午02:02:02” - Truncated incorrect date value: '02:02:02 am' MySQL日期转换(“ 2013年1月30日”到“ 2013-01-28”) - MySQL date conversion(“30-Jan-2013” to “2013-01-28”) 在SQL Server中将'2018-07-02 00:01:22.000'转换为'02 -Jul-18' - Convert '2018-07-02 00:01:22.000' to '02-Jul-18' in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM