[phpBB Debug] PHP Warning: in file [ROOT]/phpbb/session.php on line 580: sizeof(): Parameter must be an array or an object that implements Countable
[phpBB Debug] PHP Warning: in file [ROOT]/phpbb/session.php on line 636: sizeof(): Parameter must be an array or an object that implements Countable
Unofficial UNIT-e Forum • TVF_AgeEnrStart
Page 1 of 1

TVF_AgeEnrStart

Posted: Thu Mar 02, 2017 10:40 pm
by robinwilson16
Returns the age of a learner when the programme started

Code: Select all

CREATE FUNCTION [dbo].[TVF_AgeEnrStart] (
	@Session VARCHAR(7),
	@ProgEnrID numeric(16,0), 
	@StudentRef VARCHAR(20)
)
RETURNS TABLE
AS
RETURN

	
	--DECLARE @StudentRef varchar(20) = '16001195'

	SELECT DISTINCT
		Value = FLOOR ( DATEDIFF ( DAY, P.p_dob, MEP.e_start ) / 365.23076923074 )
	FROM ulive.dbo.capd_module PRG
	INNER JOIN ulive.dbo.capd_moduleenrolment MEP
		ON MEP.e_module = PRG.m_id
	INNER JOIN ulive.dbo.capd_student S
		ON S.s_id = MEP.e_student
	INNER JOIN ulive.dbo.capd_person P
		ON P.p_id = S.s_id
	WHERE
		PRG.m_type = 'P'
		AND PRG.m_start <= CAST(LEFT ( @Session, 2 ) + RIGHT ( @Session, 2 ) + '-07-31' AS DATE)
		AND PRG.m_end >= CAST(LEFT ( @Session, 4 ) + '-08-01'​​ AS DATE)
		AND S.s_studentreference = @StudentRef
		AND MEP.e_id = @ProgEnrID