如何在sql中检查时间 [英] how to check time between times in sql

查看:68
本文介绍了如何在sql中检查时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友,

我想检查给定时间是两次之间。这是我的查询

Hi friends,
I want to check the given time is between two times. this is my query

alter procedure ins
as
begin
declare @ti time(7)
declare @intime time(7),@outime time(7)
set @intime='10:10:00'
set @outime='15:30:20'
SELECT @ti= Convert(char(2),DATEPART(hour,GETDATE())) + ':'+convert(char(2),datepart(mi,GETDATE())) + ':' + convert(char(2),datepart(S,GETDATE()))
if @ti  between @intime and @outime
begin
select @ti
end

end





我的代码有什么问题。我想检查两次特定时间之间的给定时间,并在成功完成此操作后执行一些功能。

我没有得到任何错误但是结果不显示

感谢提前



what wrong with my code.i want to check the given time between two particular times and do some function after successfully completed this.
I didnt get any error but the result is not display
thanks advance

推荐答案

看起来这只会在你的系统中起作用,因为下面的部分不会在每个部分都运行文化。

It seems the will work only in your system as the following part will not run in every culture.
SELECT @ti = CONVERT(CHAR(2), DATEPART(hour, GETDATE())) + ':' + CONVERT(CHAR(2), DATEPART(mi, GETDATE())) 
	       + ':' + CONVERT(CHAR(2), DATEPART(S, GETDATE()))



您知道不同的文化有不同的日期时间设置。通过连接准备时间/日期会在不同的文化中给出错误的结果。无论如何使用以下代码




You know different culture has different date time settings. Preparing time/date by concatenation will give you wrong result in different culture. Anyway use the following code

DECLARE @ti TIME(7)
	DECLARE @intime     TIME(7),
	        @outime     TIME(7)
	
	SET @intime = '10:10:00'
	SET @outime = '15:30:20'
	
	/*
	SELECT @ti = CONVERT(CHAR(2), DATEPART(hour, GETDATE())) + ':' + CONVERT(CHAR(2), DATEPART(mi, GETDATE())) 
	       + ':' + CONVERT(CHAR(2), DATEPART(S, GETDATE()))
	       */
	
	SET @ti=CONVERT(VARCHAR(8),GETDATE(),108)
		
	IF @ti BETWEEN @intime AND @outime
	BEGIN
	    SELECT @ti
	END



希望它适用于您。


Hope it will work for you.


这篇关于如何在sql中检查时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆