-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprop_name3.sql
77 lines (70 loc) · 2.11 KB
/
prop_name3.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
CREATE FUNCTION patindex("pattern" VARCHAR, "expression" VARCHAR ) RETURNS INT AS $BODY$
SELECT
COALESCE(
STRPOS(
$2
,(
SELECT
( REGEXP_MATCHES(
$2
,'(' || REPLACE( REPLACE( TRIM( $1, '%' ), '%', '.*?' ), '_', '.' ) || ')'
) )[ 1 ]
LIMIT 1
)
)
,0
)
;
$BODY$ LANGUAGE 'sql' IMMUTABLE;
CREATE FUNCTION ProperCase (name VARCHAR) RETURNS VARCHAR AS
$$
DECLARE
TempString VARCHAR;
PS VARCHAR;
SETPS = ''
-- lower case entire string
SET TempString = lower(@String)
WHILE patindex('%[-( '']%',TempString) > 0 BEGIN
-- Check to see if first character of TempString is whitespace
IF (patindex('%[-( '']%',SUBSTRINGTempString,1,1)) > 0)
THEN
SETPS =PS + SUBSTRINGTempString,1,1)
ELSE -- TempString starts with a Name
IF SUBSTRING(TempString,1,2) = 'mc'
THEN
SET PS = PS + 'Mc'
SET TempString = SUBSTRING(@Tempstring,3,LEN(TempString))
END
IF SUBSTRING(TempString,1,3) = 'mac'
THEN
SET PS = PS + 'Mac'
SET TempString = SUBSTRING(@Tempstring,4,LEN(TempString))
END
-- upper case first character and return string up to the next space
SET PS = PS + UPPER(SUBSTRING(TempString,1,1)) +
SUBSTRING(TempString,2,patindex('%[-( '']%',TempString)-1)
END LOOP
-- truncation string that we have already processed
SET TempString = SUBSTRING(TempString,
patindex('%[-( '']%',TempString)+1,LEN(TempString))
-- Trim off leading spaces
SET TempString = LTRIM(TempString)
END
IF SUBSTRING(TempString,1,2) = 'mc'
THEN
SET PS = PS + 'Mc'
SET TempString = SUBSTRING(@Tempstring,3,LEN(TempString))
END
IF SUBSTRING(TempString,1,3) = 'mac'
THEN
SET PS = PS + 'Mac'
SET TempString = SUBSTRING(@Tempstring,4,LEN(TempString))
END
-- proper case last word/name
SET PS = PS + UPPER(SUBSTRING(TempString,1,1)) +
SUBSTRING(TempString,2,LEN(TempString))
-- check for spaces in front of special characters
SET PS = Replace(PS,' -','-')
SET PS = Replace(PS,' ''','''')
RETURN (PS)
END