日期:2014-05-17  浏览次数:20679 次

江湖救急呀!截取时间类型字符串长度
ALTER PROCEDURE [dbo].[Proc_T_Novel_Topic_Add_ReCommand]
(
@BooksId int,
@UserId int,
@DateTime datetime,
@OrderType int
)
AS
declare @ID int

BEGIN

 if (select count(*) from dbo.T_Books_Order where BooksId=@BooksId and substring([DateTime],0,10)=@DateTime and [DateTime] is not null)>0
begin
select 0 as id
end

 else
 
 begin
 insert into dbo.T_Books_Order (UserId,OrderType,BooksId,[DateTime],Status)
 values(@UserId,@OrderType,@BooksId,@DateTime,1)
 
  select 1 as id
   
 end
END
不对呀 
我的意思就是 用一个已知的时间 去比较数据库里的时间 就想取 年月日 说以想截取 年月日 作比较 请大神们 指点迷津

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

ALTER PROCEDURE [dbo].[Proc_T_Novel_Topic_Add_ReCommand]
(
@BooksId int,
@UserId int,
@DateTime datetime,
@OrderType int
)
AS
declare @ID int

BEGIN

 if (select count(*) from dbo.T_Books_Order where BooksId=@BooksId and CONVERT(VARCHAR(10),[DateTime],120)=CONVERT(VARCHAR(10),@DateTime,120) and [DateTime] is not null)>0
begin
select 0 as id
end

 else
  
 begin
 insert into dbo.T_Books_Order (UserId,OrderType,BooksId,[DateTime],Status)
 values(@UserId,@OrderType,@BooksId,@DateTime,1)
  
  select 1 as id
    
 end
END

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

convert(datetime,convert(varchar,[DateTime],112)) =@DateTime

------解决方案--------------------
将日期转字符串可以这样
SQL code

Select convert(varchar(10), getdate(), 121) --2012-08-07

------解决方案--------------------
SQL code
if datediff(day,convert(varchar(10),dt1,120),dt2)=0
    print '日期一样'
else
    print '日期不一样'