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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Quick If/Else Function Question

Author  Topic 

mxfrail
Yak Posting Veteran

84 Posts

Posted - 2009-07-23 : 10:10:50
This is what I am trying to accomplish -


CREATE FUNCTION ASP_JobDate
(
@DateEntered DATETIME,
@PSIJobNo INT
)
RETURNS DATETIME
AS
BEGIN
RETURN (

If SELECT jobdate from MailCoups.dbo.bbjthead.ljob where ljob = @PSIJobNo

Else @DateEntered

)
END


Basically I want to pass in 2 variables.

If there is a jobdate in the first part I want to return the job date. If not return the @DateEntered that I originally passed in.

Can someone quickly help me restructure this to make it work.

thanks.

ayamas
Aged Yak Warrior

552 Posts

Posted - 2009-07-23 : 10:15:02
[code]

CREATE FUNCTION ASP_JobDate
(
@DateEntered DATETIME,
@PSIJobNo INT
)
RETURNS DATETIME
AS
BEGIN
Declare @jobdate datetime
RETURN (

If EXISTS
( SELECT @jobdate =jobdate from MailCoups.dbo.bbjthead.ljob where ljob = @PSIJobNo
)
BEGIN

@jobdate
END

ELSE

@DateEntered


)
END


[/code]
Go to Top of Page

mxfrail
Yak Posting Veteran

84 Posts

Posted - 2009-07-23 : 10:22:06
Oh awesome.

I tried to punch that in ... get error -

incorrect syntax near keyword if.
line 13 incorrect syntax near "=".
Go to Top of Page

ron2112
Starting Member

44 Posts

Posted - 2009-07-23 : 10:22:36
[code]CREATE FUNCTION ASP_JobDate
(
@DateEntered DATETIME,
@PSIJobNo INT
)
RETURNS DATETIME
AS
BEGIN
RETURN ISNULL((SELECT jobdate
from MailCoups.dbo.bbjthead.ljob
where ljob = @PSIJobNo)
, @DateEntered)
END[/code]
Go to Top of Page

mxfrail
Yak Posting Veteran

84 Posts

Posted - 2009-07-23 : 11:14:34
quote:
Originally posted by ron2112

CREATE FUNCTION ASP_JobDate
(
@DateEntered DATETIME,
@PSIJobNo INT
)
RETURNS DATETIME
AS
BEGIN
RETURN ISNULL((SELECT jobdate
from MailCoups.dbo.bbjthead.ljob
where ljob = @PSIJobNo)
, @DateEntered)
END






Perfect thanks!
Go to Top of Page
   

- Advertisement -