Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Number Of Days Between Dates
Message
From
28/09/2009 12:43:53
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01426473
Message ID:
01426486
Views:
52
>>Hi Kevin,
>>
>>One method would be to use the T-SQL DATEDIFF ( datepart , startdate , enddate ) function.
>>
>>Bill
>>
>>>How do I calculate the number of days between two dates in SQL?
>>>
>>>
>>>SELECT APM_InvoiceDate - APM_DiscDate AS [VR-DSC-DAYS]
>>>	FROM iERP72_Test.dbo.APM
>>>
>>>
>>>This returns a datetime
>
>Ok, got it:
>
>So now I want to see only those rows with Days > 0
>
>This isn't right:
>
>
>
>SELECT APM_InvoiceDate, APM_DiscDate, DATEDIFF(year, APM_InvoiceDate , APM_DiscDate) AS Days, APM_InvoiceDate - APM_DiscDate AS [VR-DSC-DAYS]
>	FROM iERP72_Test.dbo.APM
>	GROUP BY Days
>	HAVING Days > 0
>
>
>Give me 'Invalid column name 'Days'.'
SELECT APM_InvoiceDate, 
  APM_DiscDate, 
  DATEDIFF(year, APM_InvoiceDate , APM_DiscDate) AS Days, 
  APM_InvoiceDate - APM_DiscDate AS [VR-DSC-DAYS]
	FROM iERP72_Test.dbo.APM
	WHERE DATEDIFF(year, APM_InvoiceDate , APM_DiscDate) > 0
Group by is irrelevant there. If it were relevant then you would write as:

GROUP BY DATEDIFF(year, APM_InvoiceDate , APM_DiscDate)

Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform