SQL中文日期转换成数字,求解

kittysisi527 2016-07-22 04:30:41
SQL数据库里的时间dt是某年某月某日 上午/下午某时某分某秒(中文),希望select出来的时间格式是yyyy-mm-dd,运行以下脚本报错,缺失表达式,求解!

SELECT
CONVERT(varchar(10),CONVERT(dt,111),111)
FROM 表
...全文
709 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
道玄希言 2016-07-22
  • 打赏
  • 举报
回复


DECLARE @dt nvarchar(50) 
SET @dt = '2016年7月8日上午1时2分3秒'
SELECT CONVERT(date,REPLACE(REPLACE(SUBSTRING(@dt,
1, CHARINDEX('日',@dt)-1), '年', '-'), '月', '-'), 120)

--------------------------------------------------------------------------------
2016-07-08

唐诗三百首 2016-07-22
  • 打赏
  • 举报
回复
建个函数转换为Datetime类型,

create table 表(dt varchar(100))

insert into 表(dt)
 select '2016年7月22日 下午6时23分24秒' union all
 select '2016年7月22日 上午4时5分6秒'


-- 建函数
create function dbo.fn_conv2Datetime
(@x varchar(100)) returns datetime
as
begin
 declare @y datetime,@sx bit
 select @x=replace(@x,'年','-')
 select @x=replace(@x,'月','-')
 select @x=replace(@x,'日','')

 select @x=replace(@x,'时',':')
 select @x=replace(@x,'分',':')
 select @x=replace(@x,'秒','')

 select @sx=case when charindex('上午',@x)>0 then 0 else 1 end
 select @x=replace(@x,'上午',' ')
 select @x=replace(@x,'下午',' ')

 select @y=cast(@x as datetime)
 select @y=case when @sx=1 then dateadd(hh,12,@y) else @y end

 return @y
end


-- 结果
select dt,
       new_dt=dbo.fn_conv2Datetime(dt)
 from 表

/*
dt                                         new_dt
---------------------------------------- -----------------------
2016年7月22日 下午6时23分24秒                2016-07-22 18:23:24.000
2016年7月22日 上午4时5分6秒                  2016-07-22 04:05:06.000

(2 row(s) affected)
*/
kingtiy 2016-07-22
  • 打赏
  • 举报
回复

declare @date nvarchar(50)= '2014年05月01日 下午1时20分21秒';
select  case when @date like '%上午%'
             then convert(datetime,replace(replace(replace(replace(replace(replace(replace(@date,
                                                              '年','-'),'月','-'),
                                                              '日',''),'下午',''),
                                                           '时',':'),'分',':'),
                                           '秒',''))
             else dateadd(hh,12,
                          convert(datetime,replace(replace(replace(replace(replace(replace(replace(@date,
                                                              '年','-'),'月','-'),
                                                              '日',''),'下午',''),
                                                              '时',':'),'分',':'),
                                                   '秒','')))
        end;
zhang9418hn 2016-07-22
  • 打赏
  • 举报
回复
最笨的方法: declare @str nvarchar(100)= '2016年7月22日下午15时40分20秒' set @str=REPLACE(@str,'年','-') set @str=REPLACE(@str,'月','-') set @str=REPLACE(@str,'日',' ') set @str=REPLACE(@str,'下午','') set @str=REPLACE(@str,'上午','') set @str=REPLACE(@str,'时',':') set @str=REPLACE(@str,'分',':') set @str=REPLACE(@str,'秒','') select CONVERT(datetime,@str)
卖水果的net 2016-07-22
  • 打赏
  • 举报
回复
dt 这列的数据,是什么样的呢,你举几个例子。

22,297

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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