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 Programming
 count days between two dates
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

eugz
Posting Yak Master

195 Posts

Posted - 09/28/2012 :  12:53:29  Show Profile  Reply with Quote
Hi All.

I have table where I keep request and date of request. And I would like to calculate days between to two date. How to calculate number of days between to date? In the result I would like to have number of request days in Month, in Quarter, and in Year.

Thanks.

tkizer
Almighty SQL Goddess

USA
37157 Posts

Posted - 09/28/2012 :  12:58:23  Show Profile  Visit tkizer's Homepage  Reply with Quote
You can use the DATEDIFF function for this.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 09/28/2012 :  13:33:52  Show Profile  Reply with Quote
you mean as x quarters y months z days format?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

eugz
Posting Yak Master

195 Posts

Posted - 09/28/2012 :  14:17:00  Show Profile  Reply with Quote
Hi thanks for replay.

I created select

	declare @DateFrom datetime
	declare @DateTo datetime
	set @DateFrom = '2011/3/07'
	set @DateTo = '2012/08/09'
	
	SELECT 
	dateadd(year, datediff(year, 0, ReqDate), 0) as call_date
	,DATEDIFF(day, ReqDate, GETDATE()) AS NumberOfDays
    from   dbo.Table1
	where convert(varchar(10),ReqDate,111) between convert(varchar(10),@DateFrom,111) and convert(varchar(10),@DateTo,111)
    group by dateadd(year, datediff(year, 0, ReqDate), 0),ReqDate


But it doesn't work. What is problem? How it fix?

Thanks.

Edited by - eugz on 09/28/2012 14:17:26
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 09/28/2012 :  14:23:43  Show Profile  Reply with Quote
quote:
Originally posted by eugz

Hi thanks for replay.

I created select

	declare @DateFrom datetime
	declare @DateTo datetime
	set @DateFrom = '2011/3/07'
	set @DateTo = '2012/08/09'
	
	SELECT 
	dateadd(year, datediff(year, 0, ReqDate), 0) as call_date
	,DATEDIFF(day, ReqDate, GETDATE()) AS NumberOfDays
    from   dbo.Table1
	where convert(varchar(10),ReqDate,111) between convert(varchar(10),@DateFrom,111) and convert(varchar(10),@DateTo,111)
    group by dateadd(year, datediff(year, 0, ReqDate), 0),ReqDate


But it doesn't work. What is problem? How it fix?

Thanks.


without understanding what output you're expecting its hard to suggest

Can you tell what exactly you're trying to get as output?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 09/28/2012 :  14:26:11  Show Profile  Reply with Quote
I would think you need something like this, assuming you are trying to get the total days
DECLARE @DateFrom DATETIME
DECLARE @DateTo DATETIME
SET @DateFrom = '2011/3/07'
SET @DateTo = '2012/08/09'

SELECT DATEADD(YEAR, DATEDIFF(YEAR, 0, ReqDate), 0) AS call_date,
       SUM(DATEDIFF(DAY, ReqDate, GETDATE())) AS NumberOfDays
FROM   dbo.Table1
WHERE  ReqDate BETWEEN @DateFrom AND @DateTo
GROUP BY
       DATEADD(YEAR, DATEDIFF(YEAR, 0, ReqDate), 0)
Go to Top of Page

eugz
Posting Yak Master

195 Posts

Posted - 09/28/2012 :  14:29:58  Show Profile  Reply with Quote
In column call_date I would like to have Years and NumberOfDays number of day when made request between DateFrom and DateTo for that year.

Thanks.

Edited by - eugz on 09/28/2012 14:34:17
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 09/28/2012 :  15:54:37  Show Profile  Reply with Quote
quote:
Originally posted by eugz

In column call_date I would like to have Years and NumberOfDays number of day when made request between DateFrom and DateTo for that year.

Thanks.


are datefrom dateto table fields?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

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.08 seconds. Powered By: Snitz Forums 2000