-
Notifications
You must be signed in to change notification settings - Fork 1
/
fnValidatePeopleID.sql
45 lines (35 loc) · 1 KB
/
fnValidatePeopleID.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
USE [Campus6]
GO
/****** Object: UserDefinedFunction [custom].[fnValidatePeopleID] Script Date: 2021-11-18 11:20:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Wyatt Best
-- Create date: 2021-11-18
-- Description: Given a potentially malformed ID number, return a valid PEOPLE_CODE_ID.
-- =============================================
CREATE FUNCTION [custom].[fnValidatePeopleID] (@PCID NVARCHAR(10))
RETURNS NVARCHAR(10)
AS
BEGIN
--Remove dashes
SET @PCID = REPLACE(@PCID, '-', '')
--Remove spaces
SET @PCID = REPLACE(@PCID, ' ', '')
-- Try to fix @PCID, first by padding with zeros, then by prepending 'P'
IF LEN(@PCID) < 9
SET @PCID = REPLICATE('0', 9 - LEN(@PCID)) + @PCID
IF LEN(@PCID) = 9
SET @PCID = 'P' + @PCID
--Verify that the PCID actually exists in PEOPLE table
SET @PCID = (
SELECT PEOPLE_CODE_ID
FROM PEOPLE
WHERE PEOPLE_CODE_ID = @PCID
)
-- Return the result of the function
RETURN @PCID
END
GO