当前位置: 代码迷 >> Sql Server >> 日期类型转成字符串解决方案
  详细解决方案

日期类型转成字符串解决方案

热度:100   发布时间:2016-04-27 17:29:47.0
日期类型转成字符串
DECLARE   @Year   int,   @Month   int,   @Day   int;
DECLARE   @StartDate   varchar(50),   @EndDate   varchar(50);
SET   @Year   =   year(@Date);
SET   @Month   =   month(@Date);
SET   @Day   =   day(@Date);
SET   @StartDate   =   @Year+ '- '[email protected]+ '- '[email protected]+ '   00:00:00.000 ';
SET   @EndDate   =   @Year+ '- '[email protected]+ '- '[email protected]+ '   23:59:59.999 ';

哪里错了?

------解决方案--------------------
DECLARE @Year int, @Month int, @Day int;
你这句都定义了是整型了哦...再来下面那句当然错了.类弄都不一样
SET @StartDate = @Year+ '- '[email protected]+ '- '[email protected]+ ' 00:00:00.000 ';
------解决方案--------------------
SET @StartDate = RTRIM(@Year)+ '- '+RTRIM(@Month)+ '- '+RTRIM(@Day)+ ' 00:00:00.000 ';
SET @EndDate = RTRIM(@Year)+ '- '+RTRIM(@Month)+ '- '+RTRIM(@Day)+ ' 23:59:59.999 ';

------解决方案--------------------
用 RTRIM()函数把整数转换为字符串.
------解决方案--------------------
--TRY

DECLARE @Year int, @Month int, @Day int;
DECLARE @StartDate varchar(50), @EndDate varchar(50);
DECLARE @Date datetime;
SET @Date = GETDATE();
SET @Year = year(@Date);
SET @Month = month(@Date);
SET @Day = day(@Date);
SET @StartDate = rtrim(@Year)+ '- '+rtrim(@Month)+ '- '+rtrim(@Day)+ ' 00:00:00.000 ';
SET @EndDate = rtrim(@Year)+ '- '+rtrim(@Month)+ '- '+rtrim(@Day)+ ' 23:59:59.999 ';

SELECT @StartDate,@EndDate
------解决方案--------------------
不需要这么麻烦.

select @StartDate = convert(varchar(10),@Date,120) + ' 00:00:00.000 '
select @EndDate = convert(varchar(10),@Date,120) + ' 23:59:59.999 '

------解决方案--------------------
DECLARE @Year int, @Month int, @Day int,@Date datetime;
DECLARE @StartDate varchar(50), @EndDate varchar(50);
set @Date = getdate()
SET @StartDate = convert(char(10),@date,120) + ' 00:00:00.000 ';
SET @EndDate = convert(char(10),@date,120) + ' 23:59:59.999 ';
select @StartDate,@EndDate
------解决方案--------------------
DECLARE @Date datetime,@StartDate varchar(50), @EndDate varchar(50);
set @Date = getdate()
SET @StartDate = convert(char(10),@date,120) + ' 00:00:00.000 ';
SET @EndDate = convert(char(10),@date,120) + ' 23:59:59.999 ';
select @StartDate,@EndDate
  相关解决方案