Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi friends,
I want to check the given time is between two times. this is my query
SQL
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
Posted
Updated 8-Sep-12 0:27am
v2
Comments
__TR__ 8-Sep-12 8:16am    
The code seems to be working fine when i executed it in my system. I would suggest you to remove the if statement and execute the code to check and see what value is being assigned to @ti variable. Then you can manually check if the value falls between the @intime and @Outime range.
baskaran chellasamy 8-Sep-12 8:44am    
Ok thanks its working fine. the problem is system default time.
Maciej Los 9-Sep-12 17:31pm    
If the problem is solved, please share mark it as solved.

1 solution

It seems the will work only in your system as the following part will not run in every culture.
SQL
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

SQL
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900