读书人

江湖救急呀!截取时间门类字符串长度

发布时间: 2012-08-28 12:37:01 作者: rapoo

江湖救急呀!截取时间类型字符串长度
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)ASdeclare @ID intBEGIN 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)>0beginselect 0 as idend else   begin insert into dbo.T_Books_Order (UserId,OrderType,BooksId,[DateTime],Status) values(@UserId,@OrderType,@BooksId,@DateTime,1)    select 1 as id     endEND
[解决办法]
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 '日期不一样' 

读书人网 >SQL Server

热点推荐