如何将 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.