Question : T SQL Query and Dates

Hi,

I have a stored procedure as shown below which is called from a C# Web Application.  The idea of the procedure is to return all items which are marked as unavailable on the specified date.  Now this seems to work fine so long as the date is unambiguous i.e. 15/07/2010 but fails where the date could be either DD/MM or MM/YY such as 08/07/2010.  I would have thought that as SQL dateTimes are used through-out it would be OK..

When I view the records in Query Analyzer the dates are displayed as YYYY-MM-DD.  When I look at the date in the C# web page it is displayed as DD/MM/YY.

I have a record which in QA shows

startdate 2010-08-03 00:00:00
enddate 2010-08-06 00:00:00

The @onDay parameter should be being passed in as 05/08/2010 but I get no matching rows...

Any suggestions as to what may be the problem and how to get this query to work reliably?

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
ALTER PROCEDURE [dbo].[usp_vehicleabsence_enumerate]
	@onDay DateTime

AS
BEGIN

-- If we have not been passed a specific date then include all absence records
IF @onDay IS NOT NULL
	SELECT ivehicleabsence.* ,ivehicle.* 
		FROM ivehicleabsence
	LEFT JOIN ivehicle ON (ivehicleabsence.ivehicle_id = ivehicle.ivehicle_id)
	WHERE ivehicleabsence.ivehicleabsence_start <= @onDay
	AND ivehicleabsence.ivehicleabsence_end >= @onDay

ELSE
	SELECT ivehicleabsence.* ,ivehicle.* 
		FROM ivehicleabsence
		LEFT JOIN ivehicle ON (ivehicleabsence.ivehicle_id = ivehicle.ivehicle_id)

END

Answer : T SQL Query and Dates

you have to paas the correct format datetime from your C# program. The dates passed to sql server procedure should be in the format mm/dd/yyyy. So I suggest you either modify your C# code to convert dd/mm/yyyy to mm/dd/yyyy before passing it to stored proc as an argument
Random Solutions  
 
programming4us programming4us