forked from yanivetrogi/PartitionsManagement
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.sql
159 lines (119 loc) · 3.91 KB
/
functions.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Thhe functions in this scrip[t are required for any user database that is managed by the PartionManagmnent.
*/
USE StackOverflow2010;
-- 1. IntToDatetime: returns datetime
IF OBJECT_ID('dbo.IntToDatetime', 'FN') IS NOT NULL DROP FUNCTION dbo.IntToDatetime;
GO
CREATE FUNCTION [dbo].[IntToDatetime]
(
@DateID bigint
)
/*
Yaniv Etrogi 20151201
Convert numeric values to datetime
Support cases where the input numeric value is 4, 8 or more figures
*/
RETURNS datetime
AS
BEGIN;
DECLARE @Result datetime;
IF LEN(@DateID) > 8
BEGIN;
SELECT @Result = CAST( CAST( @DateID/1000000 as varchar(4) ) + '-' + CAST( (@DateID/10000)%100 as varchar(2) ) + '-'
+ CAST( (@DateID/100)%100 as varchar(2) ) + ' ' + CAST( @DateID%100 as varchar(2) ) + ':00:00.000' as DATETIME );
END;
IF LEN(@DateID) = 8
BEGIN;
SELECT @DateID = @DateID*100
SELECT @Result = CAST( CAST( @DateID/1000000 as varchar(4) ) + '-' + CAST( (@DateID/10000)%100 as varchar(2) ) + '-'
+ CAST( (@DateID/100)%100 as varchar(2) ) + ' ' + CAST( @DateID%100 as varchar(2) ) + ':00:00.000' as DATETIME );
END;
IF LEN(@DateID) = 6
BEGIN;
SELECT @Result = CAST(CAST( @DateID/100 as varchar(4) ) + '-' + CAST( (@DateID)%100 as varchar(2) ) + '-01 ' + '00:00:00.000' AS datetime);
END;
RETURN @Result;
END;
GO
-----------------------------------------------------------------------------------------------------------------------------------
-- 2. DatetimeToInt: returns int
IF OBJECT_ID('dbo.DatetimeToInt', 'FN') IS NOT NULL DROP FUNCTION dbo.DatetimeToInt;
GO
CREATE FUNCTION dbo.DatetimeToInt
(
@DateVal datetime
,@ReturnLength tinyint
)
/*
Yaniv Etrogi 20151208
Accepts a datetime value and returns a numeric datetime value based on the required length
*/
RETURNS int
AS
BEGIN;
DECLARE @Result int;
IF @ReturnLength = 6
BEGIN;
SELECT @Result = (( YEAR(@DateVal) * 100 + MONTH(@DateVal)) );
END;
IF @ReturnLength = 8
BEGIN;
SELECT @Result = (( YEAR(@DateVal) * 100 + MONTH(@DateVal)) * 100 + DAY(@DateVal))
END;
IF @ReturnLength = 10
BEGIN;
SELECT @Result = (( YEAR(@DateVal) * 100 + MONTH(@DateVal)) * 100 + DAY(@DateVal)) * 100 + datepart(hour,@DateVal);
END;
IF @ReturnLength = 12
BEGIN;
SELECT @Result = CAST ((( YEAR(@DateVal) * 100 + MONTH(@DateVal)) * 100 + DAY(@DateVal)) * 100 + datepart(hour,@DateVal)AS bigint)* 100 + DATEPART(minute,@DateVal);
END;
RETURN @Result;
END;
GO
-----------------------------------------------------------------------------------------------------------------------------------
/*
-----------------------------------------------------------------------------------------------------------------------------------
IF OBJECT_ID('dbo.fn_RemoveChars', 'FN') IS NOT NULL DROP FUNCTION dbo.fn_RemoveChars;
GO
CREATE FUNCTION [dbo].[fn_RemoveChars](@inputString VARCHAR(8000), @ValidChars VARCHAR(1000))
RETURNS VARCHAR(8000)
AS
BEGIN;
WHILE @inputString like '%[^' + @validChars + ']%'
SELECT @inputString = REPLACE(@inputString,SUBSTRING(@inputString,PATINDEX('%[^' + @validChars + ']%',@inputString),1),'');
RETURN @inputString;
END;
GO
-- returns int
go
CREATE FUNCTION dbo.DatetimeToDateIdInt
(
@DateVal datetime
)
RETURNS int
AS
BEGIN;
DECLARE @Result int;
SELECT @Result = (( YEAR(@DateVal) * 100 + MONTH(@DateVal)) * 100 + DAY(@DateVal)) * 100+ datepart(hour,@DateVal);
RETURN @Result;
END;
GO
-----------------------------------------------------------------------------------------------------------------------------------
-- returns bigint
go
CREATE FUNCTION dbo.DatetimeToDateIdBigint
(
@DateVal datetime
)
RETURNS bigint
AS
BEGIN;
DECLARE @Result bigint;
SELECT @Result = CAST ((( YEAR(@DateVal) * 100 + MONTH(@DateVal)) * 100 + DAY(@DateVal)) * 100+ datepart(hour,@DateVal)AS BIGINT)*100+datepart(minute,@DateVal);
RETURN @Result;
END;
GO
-----------------------------------------------------------------------------------------------------------------------------------
*/