SQL Server - Date difference between two dates..

Asked By jeccinta jeccinta on 10-Apr-07 01:52 AM
  Am working in asp.net and sqlserver.I have a textbox in which date of joining will be entered in dd/mm/yyyy format.On form load in a  textbox i will get the current date in dd/mm/yyyy format. I should calculate the datedifference between the date entered in textbox and system date.i need the number of days to be calculated.This s the query i have used...But its not working properly...

 date of joining : 10/05/2006

select datediff(d,'10/05/2006',convert(varchar(10),getdate(),103))"days" .

Am getting the output as 364 which is wrong.

help me in doing this..Thanx in advance..

Response - F Cali replied to jeccinta jeccinta on 10-Apr-07 10:13 PM

No need to convert the date to VARCHAR.  Simply use the GETDATE() function as the second parameter:

select datediff(d,'10/05/2006',GETDATE()) AS 'Days'

SQL Server Helper

Date Difference in sql - Shailendrasinh Parmar replied to jeccinta jeccinta on 12-Aug-08 07:24 AM

Try the following query ::

select datediff(d,getdate(),'1/1/2007')

Hope it helps

Date time help - ashish chotalia replied to jeccinta jeccinta on 16-Jun-09 02:07 PM

Try out following and see the result.

Declare @startdate  DATETIME
set @startdate = '2/11/09 3:00 PM'
Declare @Enddate  DATETIME
set @Enddate = '5/14/09 10:00 PM'
select DATEDIFF(dd,@startdate,@Enddate)