Welcome to Dream.In.Code
Getting Help is Easy!

Join 119,059 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,426 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



SQL and TimeFrames

 
Reply to this topicStart new topic

SQL and TimeFrames

RudyVB.net
post 21 Jul, 2008 - 08:42 AM
Post #1


D.I.C Head

**
Joined: 3 May, 2008
Posts: 59

Hello All!

I know there must be a way to do this, just not sure how.

So I have a table called tblSchedule. It has a column called REALDATE, in SMALLDATETIME format.

So a value for this would be 7/21/2008 10:50:00 AM.
So this is the scheduled time for a connection.

CODE
DECLARE @DELAY varchar(30)

SELECT @DELAY = DATEADD(minute,  -20, REALDATE)
FROM tblschedule
WHERE [DATE] = '7/21/2008' AND REALDATE > CAST(GETDATE() AS nvarchar(30)) AND CNID = '6'

PRINT @Delay


So this returns 'Jul 21 2008 10:30AM'

So what I want to do is, if CNID "6" connection is attemptempted 20 minutes before 10:50, return a value. So only between 10:30 and 10:50 this should fire, if it's before or after that, is should return no value.

Now I have this as the last part of my code.

CODE
SELECT REALDATE, CNID
FROM TBLSCHEDULE
WHERE [DATE] = '7/21/2008' AND @DELAY < CAST(GETDATE() AS nvarchar(30))


The problem I have is the greater or less than sign doesn't have a limit.
The above is return a value where '10:30' is less then Current TIME.

But when my Current TIME is 10:51, it is still going to return a value. I need it to only return a value between 10:30 and 10:49.

I hope this makes sense, I'm getting confused just explaining it. crazy.gif

Thanks

Rudy

This post has been edited by RudyVB.net: 21 Jul, 2008 - 08:43 AM
User is offlineProfile CardPM

Go to the top of the page


Trogdor
post 21 Jul, 2008 - 09:21 AM
Post #2


D.I.C Regular

Group Icon
Joined: 6 Oct, 2006
Posts: 489



Thanked 2 times

Dream Kudos: 125
My Contributions


instead of casting the date to a string and doing a compare, try it the other way around. Doing a stringcompare on a date is going to give you grief.

You could also check out dat arithmetic functions for the database you use (you did not mention this).

Lastly, many databases have a BETWEEN construct.
User is offlineProfile CardPM

Go to the top of the page

RudyVB.net
post 21 Jul, 2008 - 09:22 AM
Post #3


D.I.C Head

**
Joined: 3 May, 2008
Posts: 59

Hello All!

I think I got. It's amazing when you walk away sometimes, it becomes crystal clear.

CODE
DECLARE @DELAY varchar(30)

SELECT @DELAY = DATEADD(minute,  -20, REALDATE)
FROM tblschedule
WHERE [DATE] = '7/21/2008' AND REALDATE > CAST(GETDATE() AS nvarchar(30)) AND CNID = '6'

PRINT @Delay

SELECT REALDATE, CNID
FROM TBLSCHEDULE
WHERE [DATE] = '7/21/2008' AND @DELAY < CAST(GETDATE() AS nvarchar(30)) AND REALDATE > CAST(GETDATE() AS nvarchar(30))


I just added the extra AND comment to go in between the times. So I think this should work. As always, if anyone does have a better way of doing this, (there always is), I'm all ears!

Thanks!

Rudy
User is offlineProfile CardPM

Go to the top of the page

Trogdor
post 21 Jul, 2008 - 09:43 AM
Post #4


D.I.C Regular

Group Icon
Joined: 6 Oct, 2006
Posts: 489



Thanked 2 times

Dream Kudos: 125
My Contributions


as i mentioned before, do not use string comparison on dates.
User is offlineProfile CardPM

Go to the top of the page

RudyVB.net
post 21 Jul, 2008 - 10:14 AM
Post #5


D.I.C Head

**
Joined: 3 May, 2008
Posts: 59

QUOTE(Trogdor @ 21 Jul, 2008 - 09:21 AM) *

instead of casting the date to a string and doing a compare, try it the other way around. Doing a stringcompare on a date is going to give you grief.

You could also check out dat arithmetic functions for the database you use (you did not mention this).

Lastly, many databases have a BETWEEN construct.



Hi Trogdor!

Thanks for the reply. I'm using SQL 2005. It's not so ,much the date I'm going for, but the time in the date. I do have one correction that I made.
DECLARE @DELAY should be smalldatetime, instead of Varchar(30)

I was trying to find if SQL had a in between clause, couldn't find one.

I'm going to return the cnID number in my program, vb.net, and compare that in the code. So when the button is clicked, it take it's name, in this case, "6", and compare it to a string, the result from SQL code above. If it is the same number, then I know to alert the user that this connection will be used in the next 20 minutes, if there is NULL in that string, then proceed with making the connection.

I think that's how it should work. I have to create the login in vb.net and test it.

Your thoughts?

Rudy
User is offlineProfile CardPM

Go to the top of the page

Trogdor
post 21 Jul, 2008 - 12:44 PM
Post #6


D.I.C Regular

Group Icon
Joined: 6 Oct, 2006
Posts: 489



Thanked 2 times

Dream Kudos: 125
My Contributions


for sqlserver the syntax of between is WHERE (someting BETWEEN x AND y)

You can use i think it is convert to format the datetime as a number of seconds since epoch, in other words, an int.
From there you can solver it numericaly, as a difference of 20 minutes is simply 1200 seconds.

Your current solution might (seem to) work, but will it work 5 minutes before noon? 5 minutes after noon? around midnight? at the transition of summertime to wintertime?
User is offlineProfile CardPM

Go to the top of the page

RudyVB.net
post 21 Jul, 2008 - 02:45 PM
Post #7


D.I.C Head

**
Joined: 3 May, 2008
Posts: 59


Your current solution might (seem to) work, but will it work 5 minutes before noon? 5 minutes after noon? around midnight? at the transition of summertime to wintertime?
[/quote]


Why do you think I may have a problem around noon, midnight?
User is offlineProfile CardPM

Go to the top of the page

Trogdor
post 22 Jul, 2008 - 05:12 AM
Post #8


D.I.C Regular

Group Icon
Joined: 6 Oct, 2006
Posts: 489



Thanked 2 times

Dream Kudos: 125
My Contributions


example: is "10/1/2008 00:01:00" bigger or smaller then "9/31/2008 23:59:00"

same goes for am/pm notation, going from 9 o clock to 10 o clock, etc etc.
use numbers, not strings.

This post has been edited by Trogdor: 22 Jul, 2008 - 05:13 AM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/13/08 04:05PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month