日期:2014-05-18  浏览次数:20468 次

格式化字符串
数据库是这样存储的

ut_rq16 (char) Length 16

2012030607:37:54
2012030607:38:38
2012030607:40:34
2012030607:41:10

我想显示成
2012-03-06-07:37:54
该怎么样格式化?


我试了下联机丛书里边的格式
select top 30 convert(char(8),ghrq,120) + '-' + convert(char(8),ghrq,108) time, * from test order by ghrq asc

 108 - hh:mm:ss 
 110 美国 mm-dd-yy 
怎么查询出来的和联机格式不一样?
20120306-20120306
20120306-20120306
20120306-20120306
20120306-20120306
20120306-20120306







------解决方案--------------------
SQL code
select convert(char(10),cast(substring('2012030607:37:54',0,9)as DATEtime),120)+'-'+RIGHT('2012030607:37:54',8)

------解决方案--------------------
SQL code


select convert(datetime,left('2012030607:37:54',8)+' '+right('2012030607:37:54',
LEN('2012030607:37:54')-8),120) as [date]
date
2012-03-06 07:37:54.000

------解决方案--------------------
习惯,在编码里面有些是以0开始的。
------解决方案--------------------
SQL code

select substring(date,1,4)+'-'+substring(date,5,2)+'-'+substring(date,7,2)+' '+substring(date,9,8) as date
from a
结果
date
----------------------
2012-03-06 07:37:54

(1 行受影响)

------解决方案--------------------
SQL code
create table tb(col varchar(16))
insert into tb values('2012030607:37:54')
insert into tb values('2012030607:38:38')
insert into tb values('2012030607:40:34')
insert into tb values('2012030607:41:10')
go

select left(col,4) + '-' + substring(col,5,2) + '-' + substring(col , 7,2) + ' ' + right(col,8) from tb

drop table tb

/*
                                    
----------------------------------- 
2012-03-06 07:37:54
2012-03-06 07:38:38
2012-03-06 07:40:34
2012-03-06 07:41:10

(所影响的行数为 4 行)

*/