在sql 中如何把字符串23MAR16转换成日期格式如2016-03-23

jing85937513 2016-03-23 02:53:10
在sqlserver 数据库表中有一个字段类型为varchar,内容格式如:23MAR16,如何把此字符串转换成日期格式如2016-03-23
非常感谢!
select pdate from tab1
...全文
1032 5 打赏 收藏 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jing85937513 2016-03-24
  • 打赏
  • 举报
回复
引用 4 楼 spiritofdragon 的回复:
,tab1(pdate) as (
select '23MAR16' union all
select '21JAN17' 
)
这个是我的测试数据表。你自己的库里不是有tab1表么? 把这段删了,执行
非常感谢!非常感谢!非常感谢!
spiritofdragon 2016-03-23
  • 打赏
  • 举报
回复
,tab1(pdate) as (
select '23MAR16' union all
select '21JAN17' 
)
这个是我的测试数据表。你自己的库里不是有tab1表么? 把这段删了,执行
jing85937513 2016-03-23
  • 打赏
  • 举报
回复
[quote=引用 1 楼 spiritofdragon 的回复:] oracle有现成的函数,sql貌似目前还没有。自己写个对应表吧,才12行。 [code=sql]with mon(num,str) as ( select '01','JAN' union all select '02','FEB' union all select '03','MAR' union all select '04','APR' union all select '05','MAY' union all select '06','JUN' union all select '07','JUL' union all select '08','AUG' union all select '09','SEP' union all select '10','OCT' union all select '11','NOV' union all select '12','DEC' ) ,tab1(pdate) as ( select '23MAR16' union all select '21JAN17' ) select pdate,CAST('20'+SUBSTRING(t.pdate,6,2)+mon.num+SUBSTRING(t.pdate,1,2) as DATE) Date型 from tab1 t join mon on SUBSTRING(t.pdate,3,3)=mon.str 查询结果是: pdate Date型 21JAN17 2017-01-21 23MAR16 2016-03-23 如何改写 tab1(pdate) as ( select '23MAR16' union all select '21JAN17' 转换为从select pdate from tab1,pdate这一列所有的日期全变成这个样子 pdate Date型 23MAR16 2016-03-23 23MAR16 2016-03-23 23MAR16 2016-03-23 23MAR16 2016-03-23 23MAR16 2016-03-23 23MAR16 2016-03-23 23MAR16 2016-03-23 23MAR16 2016-03-23 23MAR16 2016-03-23 23MAR16 2016-03-23 不好意思,我sql不熟悉
Ginnnnnnnn 2016-03-23
  • 打赏
  • 举报
回复
可能需要转一下语言
SET LANGUAGE 'us_english'
DECLARE @DT VARCHAR(50) = '23MAR16'
SELECT CONVERT(SMALLDATETIME,@DT)
spiritofdragon 2016-03-23
  • 打赏
  • 举报
回复
oracle有现成的函数,sql貌似目前还没有。自己写个对应表吧,才12行。
with mon(num,str) as (
select '01','JAN' union all
select '02','FEB' union all
select '03','MAR' union all
select '04','APR' union all
select '05','MAY' union all
select '06','JUN' union all
select '07','JUL' union all
select '08','AUG' union all
select '09','SEP' union all
select '10','OCT' union all
select '11','NOV' union all
select '12','DEC' 
)
,tab1(pdate) as (
select '23MAR16' union all
select '21JAN17' 
)
select pdate,CAST('20'+SUBSTRING(t.pdate,6,2)+mon.num+SUBSTRING(t.pdate,1,2) as DATE) Date型
from tab1 t
join mon on SUBSTRING(t.pdate,3,3)=mon.str

703

社区成员

发帖
与我相关
我的任务
社区描述
提出问题
其他 技术论坛(原bbs)
社区管理员
  • community_281
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧