SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Administration
 date time query confusion
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

kshahzad
Starting Member

45 Posts

Posted - 07/17/2013 :  11:51:26  Show Profile  Reply with Quote
i have this working query below,

i want to filter the record based on current date.

In the query i have to hardcode the date vlaues but i want this to dynamically grab the current date.

the column type is datetime.

select avg(TotalUsers) from UsersLoggedIn where TimeRecorded between '2013-07-16 08:26:26.173' and '2013-07-17 17:00:30.277'

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 07/17/2013 :  11:54:54  Show Profile  Reply with Quote

select avg(TotalUsers) from UsersLoggedIn where TimeRecorded >=DATEADD(dd,DATEDIFF(dd,0,GETDATE()),-1) AND TimeRecorded < DATEADD(dd,DATEDIFF(dd,0,GETDATE()),1)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

kshahzad
Starting Member

45 Posts

Posted - 07/17/2013 :  13:28:32  Show Profile  Reply with Quote
what about this

TimeRecorded >=DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0) AND TimeRecorded < DATEADD(dd,DATEDIFF(dd,0,GETDATE()),1)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 07/17/2013 :  13:34:49  Show Profile  Reply with Quote
quote:
Originally posted by kshahzad

what about this

TimeRecorded >=DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0) AND TimeRecorded < DATEADD(dd,DATEDIFF(dd,0,GETDATE()),1)


this wont start from previous date. You had start date as 16th Jul which was yesterday
Thats why I started with -1

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

kshahzad
Starting Member

45 Posts

Posted - 07/17/2013 :  13:37:53  Show Profile  Reply with Quote
i want to collect only of current date at the end of the day like after 9 PM
so i guess this would fit will in this scenario Correct?

TimeRecorded >=DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0) AND TimeRecorded < DATEADD(dd,DATEDIFF(dd,0,GETDATE()),1)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 07/18/2013 :  00:45:27  Show Profile  Reply with Quote
quote:
Originally posted by kshahzad

i want to collect only of current date at the end of the day like after 9 PM
so i guess this would fit will in this scenario Correct?

TimeRecorded >=DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0) AND TimeRecorded < DATEADD(dd,DATEDIFF(dd,0,GETDATE()),1)


Ok current day means above logic is correct

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000