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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing Dates

Status
Not open for further replies.

Guest_imported

New member
Joined
Jan 1, 1970
Messages
0
What I am trying to do is to campare dates that a user enters in the page. If the first date is greater then the second date, it should give them an error. It works fine when the dates are correct, but if the dates are something like this
FromDate: 3/1/01
ToDate : 2/1/02
it shows the error saying that the From date is greater then the ToDate. Here is how I wrote it. I have this statement in a stored procedure:

CREATE PROCEDURE Proc7
(
@FromDate Varchar(20)
,@ToDate Varchar(20) )
As

Declare
@fromdt datetime
, @todt datetime
Begin Transaction

Select @fromdt = Cast(@Fromdate As datetime) + ' 12:00 am' -- Converts the data into datetime format
Select @todt = Cast(@ToDate As datetime) + ' 11:59 pm'

If @fromdt > @todt


Begin
Rollback Transaction
Raiserror('From Date is after the To Date',16,1)
Return
End
If @fromdate < @todate

Select ClientCase.ClientCaseNum
, SubjectName.SubjectFirstNm + ' ' + SubjectName.SubjectLastNm As Subject_Name
, Requester.ReqFirstNm + ' ' + Requester.ReqLastNm As Requester_Name
, ClientCase.DeliveredDt As Delivered_Date
From ClientCase(nolock)
INNER Join SubjectName(nolock)
On ClientCase.SubjectID = SubjectName.SubjectID
INNER Join Requester(nolock)
ON ClientCase.RequesterID = Requester.RequesterID
INNER Join Client(nolock)
ON ClientCase.ClientID = Client.ClientID
where ClientCase.DeliveredDt Between @fromdt and @todt
 
The only thing I can think of is to make sure your dateformat is correct.

SET DATEFORMAT mdy

This sets it so that the format is month/day/year. It might be set to 'ymd' in which case the FROM date is Jan 1, 2003 and the TO date is Jan 2, 2002
 
If figured it out. I didn't have a Begin and AND after the second if statement
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top