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

日期类型转成字符串
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+ '- '+@Month+ '- '+@Day+ '   00:00:00.000 ';
SET   @EndDate   =   @Year+ '- '+@Month+ '- '+@Day+ '   23:59:59.999 ';

哪里错了?

------解决方案--------------------
DECLARE @Year int, @Month int, @Day int;
你这句都定义了是整型了哦...再来下面那句当然错了.类弄都不一样
SET @StartDate = @Year+ '- '+@Month+ '- '+@Day+ ' 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