Wrox Programmer Forums
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 8th, 2007, 02:11 AM
Authorized User
 
Join Date: Jan 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to hossrad
Default URGENT HELP PLEASE GUYS!

hi,
I have a function that gets a time from an input parameter as nvarchar.
in the function i want to check the input parameter for its validity.
i want to return a specific return value if the time parameter is not valid.
thanks for your help.

God Bless you,
Hossein
__________________
Be Sure,
-hossrad
 
Old May 8th, 2007, 02:13 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

How about posting your code here?

_________________________
- Vijay G
Strive for Perfection
 
Old May 8th, 2007, 02:34 AM
Authorized User
 
Join Date: Jan 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to hossrad
Default

ALTER FUNCTION dbo.SubtractTime_fx
(
@FromDate datetime,
@FromTime varchar(8),
@ToDate datetime,
@ToTime varchar(8)
)
RETURNS nvarchar(10)
AS
BEGIN
    DECLARE @SecondsBetween int
    DECLARE @HourPart tinyint
    DECLARE @MinPart tinyint
    DECLARE @SecondPart tinyint
    DECLARE @Temp varchar(100)

    SELECT @Temp=CAST(@FromTime as DateTime)
    if @@error<>0
        return '-1'
    SELECT @Temp=CAST(@ToTime as DateTime)
    if @@error<>0
        return '-1'
    if ((@FromDate=@ToDate) and (@FromTime>@ToTime)) or (@FromDate>@ToDate)
        return '-1'
    SELECT @SecondsBetween=DateDiff(second,+Cast(IsNull(@From Date,0) as datetime)+Cast(@FromTime as datetime),CAST(IsNull(@FromDate,0) as datetime)+Cast(@ToTime as dateTime))
    --return CAST(@SecondsBetween as nvarchar)
    SET @HourPart=@SecondsBetween / 3600
    SET @SecondsBetween= @SecondsBetween % 3600
    SET @MinPart=@SecondsBetween /60
    SET @SecondsBetween= @SecondsBetween % 60
    SET @SecondPart=@SecondsBetween
    return CAST(@HourPart as nvarchar)+':'+SUBSTRING('00',1,2-Len(CAST(@MinPart as nvarchar)))+CAST(@MinPart as nvarchar)+':'+SUBSTRING('00',1,2-Len(CAST(@SecondPart as nvarchar)))+CAST(@SecondPart as nvarchar)
END



God Bless you,
Hossein
 
Old May 8th, 2007, 04:06 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Ideally you should be doing the validation in your function and return something like True or False to say if the validation succeeded or failed. based on that you should be proceeding further.

_________________________
- Vijay G
Strive for Perfection
 
Old May 8th, 2007, 04:44 AM
Authorized User
 
Join Date: Jan 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to hossrad
Default

how should i do the validation?
i tried to cast it as datetime so that if the cast returned an error i can say its not a valid datetime! but when i call the function if the cast returns error the function breaks and returns the error! i want to handle the casting error in my function so that the function call doesnt return the error

God Bless you,
Hossein
 
Old May 8th, 2007, 06:31 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

The question here is why is the time passed in as a different parameter when sql server supports the Time part in the DATETIME Type?
Code:
@FromDate datetime,
@FromTime varchar(8),
@ToDate datetime,
@ToTime varchar(8)
And how is the time being passed? What if someone passed alphanumeric values to time variables? obviously the CAST will fail.

_________________________
- Vijay G
Strive for Perfection
 
Old May 8th, 2007, 11:39 PM
Authorized User
 
Join Date: Jan 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to hossrad
Default

Thats exactly the point! i just want to handle the cast fail myself.
i dont want the cast to work. i want when it fails the whole function does not fail! something like writing this code in delphi :
try
  a:=StrToDateTime(FromTime);
except
  //FromTime is not a valid time
end;



God Bless you,
Hossein
 
Old May 9th, 2007, 01:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

I dont think you can handle the CAST failure yourself. It is the error in data format that is passed and being subjected to CAST. So it would terminate the procedure/function from processing further. You dont have control of it and resume next in case of such errors, unless you parse the complete time part and confirm that hour:min:second is passed within their limits. These are too much to do manually. Thats why I asked why is that you pass the Time part in a separate parameter and add more to your code to validate it, rather than using the time part of DATETIME datatype, which means when a value passed with a datetime variable, it means that it is a valid entry and avoid manual validations thus.

Hope that explains.
Cheers

_________________________
- Vijay G
Strive for Perfection
 
Old May 9th, 2007, 01:31 AM
Authorized User
 
Join Date: Jan 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to hossrad
Default

Thanks A Lot for your help,
i think i got it!


God Bless you,
Hossein





Similar Threads
Thread Thread Starter Forum Replies Last Post
hi guys please help me jomar VB.NET 1 August 18th, 2008 01:14 PM
GUYS I REALLY NEED YOUR HELP!!! taniunya1 SQL Server DTS 1 October 23rd, 2006 07:53 AM
I need u guys assistance acube4real C++ Programming 1 October 20th, 2006 08:27 AM
what do u Guys think ! augustine General .NET 4 July 12th, 2004 02:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.