当前位置: 代码迷 >> Sql Server >> 高手聚会,这有关问题有几种解决办法(日期格式转换)
  详细解决方案

高手聚会,这有关问题有几种解决办法(日期格式转换)

热度:145   发布时间:2016-04-27 19:28:07.0
高手聚会,这问题有几种解决方法(日期格式转换)。
如何用最简单的SQL语句将200710101212 转换成2007-10-10 12:12 



=====================================================================================================
chuifengde 
树上的鸟儿 
等 级:
 发表于:2007-10-15 11:28:431楼 得分:45 
declare @a varchar(200) 
set @a= '200710101212 ' 
select convert(varchar(11),cast(left(@a,8) as smalldatetime),120)+stuff(right(@a,4),3,0, ': ') 
=====================================================================================================
 
 


------解决方案--------------------
declare @Time varchar(100)
set @Time='200710101212'
select(cast(substring(@Time,1,2) + '-' + substring(@Time,3,2)+ '-' + substring(@SendTime,5,2) + ' '+ substring(@SendTime,7,2) + ':' + substring(@SendTime,9,2))as datetime)
------解决方案--------------------
set ANSI_PADDING OFF
declare @a varchar(100)
set @a= '200710101212 '
declare @test varchar(100)
select @test = substring(@a,1,4)+'-'+substring(@a,5,2)+'-'+substring(@a,7,2)+' ' +substring(@a,9,2) + ':'+substring(@a,11,2)
select cast(@test as datetime)
select convert(varchar(50),@test,120);
go
------解决方案--------------------
SQL code
declare @i varchar(20)set @i=200710101212select    stuff(stuff(stuff(stuff(@i,11,0,':'),9,0,' '),7,0,'-'),5,0,'-'),    left(@i,4)+'-'+substring(@i,5,2)+'-'+substring(@i,7,2)+' '+substring(@i,9,2)+':'+right(@i,2),    convert(varchar(16),convert(datetime,stuff(stuff(@i,11,0,':'),9,0,' ')),120),    convert(varchar(11),convert(datetime,left(@i,8)),120)+stuff(right(@i,4),3,0,':')
  相关解决方案