Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
majidbhutta
Starting Member
13 Posts |
Posted - 2005-12-01 : 16:35:45
|
| Please Correct This Udf_function.below is the function that compares only time .there are 3 datetime variables.@StartTime datetime@EndTime datetime@DayTime datetime@StartTime is always less than @EndTime regardless of its dates.and @DayTime may be any time. We have to calculate whether @DayTime lies between @StartTime and @EndTime or not. And on its behalf we return a value.if @DayTime lies between @StartTime and @EndTime return 1 else return 0.But tested with these values result is not ok.set @StartTime='12/1/2005 12:30:49 PM'set @EndTime='12/2/2005 4:30:49 AM'set @DayTime='2005-12-02 00:56:02.730'select dbo.udf_IsShiftValid(@StartTime,@EndTime,@DayTime )it returns 0 where i think it should return 1. plz correct me where m i wrong.plz correct this function according to the requirements.CREATE FUNCTION dbo.udf_IsShiftValid(@StartTime datetime, @EndTime datetime,@DayTime datetime) RETURNS int AS BEGIN DECLARE @RtValue AS intIFconvert(varchar,@DayTime,114)>=convert(varchar,@StartTime,114) AND convert(varchar,@DayTime,114)<convert(varchar,@EndTime,114)BEGINSET @RtValue=1ENDELSEBEGINSET @RtValue=0END |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-12-01 : 18:16:07
|
| >>@StartTime is always less than @EndTime regardless of its datesif you disregard the date portion, doesn't your @StartTime and @endTime values violate the rule?@startTime = 12:30:49 PM@endTime = 4:30:49 AMin this case isn't starttime greater than endtime?Be One with the OptimizerTG |
 |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2005-12-01 : 19:23:55
|
majidbhuttaon top of what TG says above, just get rid of the convert statements - you have specified in the function declaration that these must already by datetime so therefore there's no conversion required. Unless your requirements are that these can be varchars. Secondly, your test data values are varchar and not datetime, and this means you have an implicit conversion occurring which you are neither aware of nor controlling. So your test values should be as follows:set @StartTime=convert(datetime, '12/1/2005 12:30:49 PM',101)set @EndTime=(datetime, '12/2/2005 4:30:49 AM',101)set @DayTime=(datetime, '2005-12-02 00:56:02.730', 121) CREATE FUNCTION dbo.udf_IsShiftValid(@StartTime datetime, @EndTime datetime,@DayTime datetime) RETURNS int AS BEGIN DECLARE @RtValue AS intIF@DayTime>=@StartTime AND @DayTime<@EndTimeBEGINSET @RtValue=1ENDELSEBEGINSET @RtValue=0END better still, why not make your function more intelligent so that you don't have to have the start and end times in any particular order? CREATE FUNCTION dbo.udf_IsShiftValid(@StartTime datetime, @EndTime datetime,@DayTime datetime) RETURNS int AS BEGIN DECLARE @RtValue AS intdeclare @st as datetimedeclare @et as datetime//if dates are supplied in wrong order, swap themif @starttime > @endtime begin set @st = @endtime set @et = @starttimeendelsebegin set @et = @endtime set @st = @starttimeendIF @DayTime>=@st AND @DayTime<@etBEGIN SET @RtValue=1ENDELSEBEGIN SET @RtValue=0END --I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2005-12-01 : 21:56:55
|
This seems simpler, and works OK.create function dbo.udf_IsShiftValid( @StartTime datetime, @EndTime datetime, @DayTime datetime) returns int as beginif @DayTime between @StartTime and @EndTime return 1-- In case the Start and End time are reversedif @DayTime between @EndTime and @StartTime return 1return 0endgoprint '*** Valid ***'select [Shift Valid] = dbo.udf_IsShiftValid ( '2005-12-01 12:30:49.000', '2005-12-02 04:30:49.000', '2005-12-02 04:30:49.000' )union allselect [Shift Valid] = dbo.udf_IsShiftValid ( '2005-12-01 12:30:49.000', '2005-12-02 04:30:49.000', '2005-12-01 12:30:49.000' )union allselect [Shift Valid] = dbo.udf_IsShiftValid ( -- Start and End reversed '2005-12-02 04:30:49.000', '2005-12-01 12:30:49.000', '2005-12-01 12:30:49.000' )union allselect [Shift Valid] = dbo.udf_IsShiftValid ( -- Start and End reversed '2005-12-02 04:30:49.000', '2005-12-01 12:30:49.000', '2005-12-02 04:30:49.000' )print '*** Not Valid ***'select [Shift Valid] = dbo.udf_IsShiftValid ( '2005-12-01 12:30:49.000', '2005-12-02 04:30:49.000', '2005-12-02 04:30:49.003' )union allselect [Shift Valid] = dbo.udf_IsShiftValid ( '2005-12-01 12:30:49.000', '2005-12-02 04:30:49.000', '2005-12-01 12:30:48.997' )union allselect [Shift Valid] = dbo.udf_IsShiftValid ( -- Start and End reversed '2005-12-02 04:30:49.000', '2005-12-01 12:30:49.000', '2005-12-01 12:30:48.997' )union allselect [Shift Valid] = dbo.udf_IsShiftValid ( -- Start and End reversed '2005-12-02 04:30:49.000', '2005-12-01 12:30:49.000', '2005-12-02 04:30:49.003' )*** Valid ***Shift Valid ----------- 1111(4 row(s) affected)*** Not Valid ***Shift Valid ----------- 0000(4 row(s) affected) CODO ERGO SUM |
 |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2005-12-01 : 22:06:56
|
except that between includes the "equals to" on both ends of the comparison (which I suspect is probably what you want, but your code above had your comparison only "one-ended" ie strictly less than)but if you're OK about swapping ends, then as the colonel says... --I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
|
|
|
|
|
|