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 |
|
cmowbray
Starting Member
5 Posts |
Posted - 2010-02-04 : 10:08:08
|
| Sorry this post should be in the sql 2005 section. I would greatly appreciate any help you can give me with the syntax of the following scalar-valued function. I am basicly trying to find out what the latest date is between 3 dates and return the value @LatestDate which I then want to add to a column in an already existing table called tbl_Adjustment. At present I have the following syntax and I am getting an error stating Incorrect syntax near '>'.CREATE FUNCTION [dbo].[GetLatestDate] (@Project_ID int,@Adjustment_ID int, @LatestDate datetime)RETURNS DatetimeASBEGINRETURN CASE @LatestDateWHEN SMT_Decision_Date >= DFP_Approval_Date And SMT_Decision_Date >= Ministerial_Approval_Date THEN SMT_Decision_DateWHEN DFP_Approval_Date Is Null AND Ministerial_Approval_Date Is Null Then SMT_Decision_DateWHEN SMT_Decision_Date >= DFP_Approval_Date AND Ministerial_Approval_Date Is Null Then SMT_Decision_DateWHEN DFP_Approval_Date>= SMT_Decision_Date AND Ministerial_Approval_Date Is Null Then DFP_Approval_DateWHEN Ministerial_Approval_Date >= SMT_Decision_Date And DFP_Approval_Date Is Null Then Ministerial_Approval_DateWHEN SMT_Decision_Date>= Ministerial_Approval_Date And DFP_Approval_Date Is Null Then SMT_Decision_DateWHEN DFP_Approval_Date>= SMT_Decision_Date AND DFP_Approval_Date >= Ministerial_Approval_Date THEN DFP_Approval_Date WHEN Ministerial_Approval_Date>= SMT_Decision_Date AND Ministerial_Approval_Date >= DFP_Approval_Date THEN Ministerial_Approval_DateELSE NULLEND FROM tbl_AdjustmentsWHERE Project_ID = @Project_ID And Adjustment_ID = @Adjustment_IDENDGO |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-04 : 10:43:34
|
| [code]CREATE FUNCTION [dbo].[GetLatestDate] (@Product_ID int,@Adjustment_ID int)RETURNS DatetimeASBEGINDECLARE @LatestDate datetimeSELECT @LatestDate= CASE WHEN SMT_Decision_Date >= DFP_Approval_Date And SMT_Decision_Date >= Ministerial_Approval_Date THEN SMT_Decision_DateWHEN DFP_Approval_Date Is Null AND Ministerial_Approval_Date Is Null Then SMT_Decision_DateWHEN SMT_Decision_Date >= DFP_Approval_Date AND Ministerial_Approval_Date Is Null Then SMT_Decision_DateWHEN DFP_Approval_Date>= SMT_Decision_Date AND Ministerial_Approval_Date Is Null Then DFP_Approval_DateWHEN Ministerial_Approval_Date >= SMT_Decision_Date And DFP_Approval_Date Is Null Then Ministerial_Approval_DateWHEN SMT_Decision_Date>= Ministerial_Approval_Date And DFP_Approval_Date Is Null Then SMT_Decision_DateWHEN DFP_Approval_Date>= SMT_Decision_Date AND DFP_Approval_Date >= Ministerial_Approval_Date THEN DFP_Approval_Date WHEN Ministerial_Approval_Date>= SMT_Decision_Date AND Ministerial_Approval_Date >= DFP_Approval_Date THEN Ministerial_Approval_DateELSE NULLEND FROM tbl_AdjustmentsWHERE Project_ID = @Project_ID And Adjustment_ID = @Adjustment_IDRETURN @LatestDate ENDGOand call likeSELECT dbo.[GetLatestDate](first param value,second param value)[/code] |
 |
|
|
cmowbray
Starting Member
5 Posts |
Posted - 2010-02-04 : 10:50:14
|
Many Thanks for your help Visakh16 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-04 : 10:52:19
|
quote: Originally posted by cmowbray Many Thanks for your help Visakh16
welcome to know more on types of user defined functions suggest you to refer below articlehttp://www.sqlteam.com/article/user-defined-functions |
 |
|
|
|
|
|