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

在news表中有一个DataTime类型的字段,如果“日”不足2位,怎样在“日”的前面加0呢?我的SQL语句:

我的SQL语句是:
select   DateName(year,newsTime)+ '- '+DateName(month,newstime)+ '- '+DateName(day,newstime)  
          as   日期       from   news

运行后的结果:
日期                                                                                                                                                                                      
----------------
2007-07-6
2007-09-13
2007-09-16
2007-10-20
2007-10-25
2008-06-8
2008-07-23
2009-09-1
NULL

(所影响的行数为   9   行)


我想要的结果是:如果“日”是1位,就在日的前面加0,如果“日”是2位,就直接输出日,总之“日”必须按2位输出。比如,上面结果的第一行2007-07-6就得按2007-07-06输出,怎样实现呢?

------解决方案--------------------
Select Convert(Varchar(10), GetDate(), 120)
--2007-05-25
------解决方案--------------------
select convert(varchar(10),newstime,120)
------解决方案--------------------
Select Convert(Varchar(10), newsTime, 120) As 日期 from news
------解决方案--------------------
select convert(nvarchar(10),Convert(datetime, '2007-07-6 '),120)

select convert(nvarchar(10),getdate(),120)
------解决方案--------------------
Select Convert(Varchar(10), newstime,120) as 日期 from news

------解决方案--------------------
select convert(char(10),newstime,120)
------解决方案--------------------
select dTime = Convert(varchar(10),getdate(),120)

select dTime = Convert(varchar(10),Convert(datetime, '2007/2/5 '),120)
------解决方案--------------------
select convert(varchar(10),newstime,120)