Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting records based on sperated date and time fields 1

Status
Not open for further replies.

bminaeff

Programmer
Dec 26, 2007
49
US
I have a table with two columns one is the date and one is the time so for example one records date might be '06/20/08' and its time might be '08:52:32:140'. I need a stored procedure to return back the counts of another field where the date and time is later than some date/time input parameter. Here is my erroneous code and I think all that needs to be fixed is the WHERE clause. Any ideas how to do that?

Also, this may be the wrong place to post this question but I'll give it a go. The SortLane field is limited to certain values (between 1 and 9) so it will output something like this when I execute the procedure.
SortLane Total
4 2
5 2
8 2
9 14

Does anyone have some example code as to how to get data into a piece of vb.net code?

Thanks

Code:
CREATE PROCEDURE [dbo].[Sorter_Counts]
	-- Add the parameters for the stored procedure here
	(@ResetTime [nvarchar] (50),
	 @ResetDate [nvarchar] (50))
		
	AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
		SELECT SortLane, COUNT(SortLane) as total
		FROM Archive_Table
		WHERE SortDate > @ResetDate AND SortTime > @ResetTime
		GROUP BY SortLane
		ORDER BY SortLane ASC
END
 
In SQL Server (pre-2008 anyways), Dates and Times go together in the DATETIME/SMALLDATETIME data types. You cannot have a date without a time. So you need to compare against a DATETIME variable in your WHERE clause. You can do this by concatenating your two existing variables into one string and then casting it to DATETIME...although I would suggest re-engineering your procedure and application to accept a single DATETIME variable as opposed to two VARCHARs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top