Link to home
Start Free TrialLog in
Avatar of fjkilken
fjkilken

asked on

Need SQL function to output two dates

Hi
I have a query which outputs a pair of dates based upon the current date - it works just fine but I need to put it into a Function and need help - here is my attempt at writing the Function - thanks for the help!

CREATE FUNCTION [dbo].[imaps_fn_date2intelcalrng](@intl_mo_strt, @intl_mo_end)

RETURNS DATETIME
AS
BEGIN

	DECLARE @intl_mo_strt SMALLDATETIME
	DECLARE @intl_mo_end DATETIME	

		SELECT DISTINCT MIN([intel_strt_dtm]) AS intl_mo_strt, MAX([intel_end_dtm]) AS intl_mo_end
		FROM [dbo].[imaps_intel_clndr]
		WHERE CAST(intel_yr AS CHAR(4)) + CASE WHEN LEN(intel_mo) = 1	THEN '0' + CAST(intel_mo AS CHAR(2)) ELSE CAST(intel_mo AS CHAR(2)) END = 
			(
				SELECT DISTINCT CAST(intel_yr AS CHAR(4)) + CASE WHEN LEN(intel_mo) = 1	THEN '0' + CAST(intel_mo AS CHAR(2)) ELSE CAST(intel_mo AS CHAR(2)) END AS YearMonth
				FROM  [dbo].[imaps_intel_clndr]
				WHERE [intel_strt_dtm] = CONVERT(VARCHAR, GETDATE(),101)
			)
   RETURN(@intl_mo_strt, @intl_mo_end)
END

Open in new window

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

let me check out, but to return 2 values, you cannot use RETURN DATETIME, as that is 1 single value.
you need to get a table-valued return type instead.
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fjkilken
fjkilken

ASKER

perfect - thanks a lot!