DATEINT
Updated 2023-10-06 23:41:21.187000
Syntax
SELECT [westclintech].[wct].[DATEINT](
<@Year, int,>
,<@Month, int,>
,<@Day, int,>)
Description
Use the scalar function DATEINT to return an int value for a specified Year, Month, and Day. DATEINT is the equivalent of the DATE function in EXCEL.
Arguments
@Year
The integer value representing the year. @Year is an expression that returns an int or that can be implicitly converted to int.
@Month
The integer value representing the month. @Month is an expression that returns an int or that can be implicitly converted to int.
@Day
The integer value representing the day of the month. @Day is an expression that returns an int or that can be implicitly converted to int.
Return Type
int
Remarks
DATEINT is the equivalent of the DATE function in EXCEL, except that it returns an integer representing the number of days from 01-Jan-1900. This is consistent with numeric representation of dates in SQL Server. EXCEL and .NET have different numeric representations of dates than SQL Server.
To convert a year, month, and day combination to a datetime, consider using the CALCDATE function.
To convert a year, month, and day combination to a float, consider using the DATEFLOAT function.
Examples
To represent 07-Feb-2011, enter the following SQL.
SELECT wct.DATEINT( 2011, --@Year
2, --@Month
7 --@Day
) as DATEINT;
This produces the following result.
{"columns":[{"field":"DATEINT","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"DATEINT":"40579"}]}
DATEINT is language independent. The datetime value returned is consistent across different language settings. Look at the following example.
SET LANGUAGE Italian;
SELECT wct.DATEINT(2011, 2, 7) as [DATEINT(2011,2,7)],
cast(cast('02/07/2011' as datetime) as int) as [cast(cast('02/07/2011' as
datetime) as int)];
This returns the following result.
{"columns":[{"field":"DATEINT(2011,2,7)","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"cast(cast('02/07/2011' as datetime) as int)","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"DATEINT(2011,2,7)":"40579","cast(cast('02/07/2011' as datetime) as int)":"40724"}]}
SET LANGUAGE us_english;
SELECT wct.DATEINT(2011, 2, 7) as [DATEINT(2011,2,7)],
cast(cast('02/07/2011' as datetime) as int) as [cast(cast('02/07/2011' as
datetime) as int)];
This returns the following result.
{"columns":[{"field":"DATEINT(2011,2,7)","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"cast(cast('02/07/2011' as datetime) as int)","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"DATEINT(2011,2,7)":"40579","cast(cast('02/07/2011' as datetime) as int)":"40579"}]}
As you can see, the same date string was passed in both statements, but the datetime value returned was different based upon the language setting for the database. Using DATEINT avoids that problem.
DATEINT allows you to perform operation on the date components. You can add or subtract integer values to @Year, @Month, @Date, and still be confident of having a valid value returned.
Let’s say you wanted to generate a schedule that showed the first Monday of every month. Here’s one way to do this in SQL.
SELECT DATEADD(
WEEK,
DATEDIFF(
WEEK,
0,
(DATEADD(
DAY,
(6 - DATEPART(DAY, DATEFROMPARTS(2011,
m.mth, 1))),
DATEFROMPARTS(2011, m.mth, 1)
)
)
),
0
)
FROM
(
VALUES
(1),
(2),
(3),
(4),
(5),
(6),
(7),
(8),
(9),
(10),
(11),
(12)
) m (mth);
This produces the following result.
{"columns":[{"field":"First Monday","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"First Monday":"2011-01-03 00:00:00.000"},{"First Monday":"2011-02-07 00:00:00.000"},{"First Monday":"2011-03-07 00:00:00.000"},{"First Monday":"2011-04-04 00:00:00.000"},{"First Monday":"2011-05-02 00:00:00.000"},{"First Monday":"2011-06-06 00:00:00.000"},{"First Monday":"2011-07-04 00:00:00.000"},{"First Monday":"2011-08-01 00:00:00.000"},{"First Monday":"2011-09-05 00:00:00.000"},{"First Monday":"2011-10-03 00:00:00.000"},{"First Monday":"2011-11-07 00:00:00.000"},{"First Monday":"2011-12-05 00:00:00.000"}]}
DATEINT allows you to perform operations on the components of the date rather than on the date value, simplifying the SQL.
SELECT CAST(fom + wct.Modulo(-wct.Modulo(fom, 7), 7) as datetime) as [First Monday]
FROM
(
SELECT wct.DATEINT(2011, mth, 1) as fom
FROM
(
VALUES
(1),
(2),
(3),
(4),
(5),
(6),
(7),
(8),
(9),
(10),
(11),
(12)
) m (mth)
) n;
This produces the following result.
{"columns":[{"field":"First Monday","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"First Monday":"2011-01-03 00:00:00.000"},{"First Monday":"2011-02-07 00:00:00.000"},{"First Monday":"2011-03-07 00:00:00.000"},{"First Monday":"2011-04-04 00:00:00.000"},{"First Monday":"2011-05-02 00:00:00.000"},{"First Monday":"2011-06-06 00:00:00.000"},{"First Monday":"2011-07-04 00:00:00.000"},{"First Monday":"2011-08-01 00:00:00.000"},{"First Monday":"2011-09-05 00:00:00.000"},{"First Monday":"2011-10-03 00:00:00.000"},{"First Monday":"2011-11-07 00:00:00.000"},{"First Monday":"2011-12-05 00:00:00.000"}]}
We could have also used the XLeratorDB modulo function (which is the equivalent of the EXCEL MOD function).
SELECT CAST(wct.DATEINT(2011, mth, 1 + wct.Modulo((2 - DATEPART(DW, wct.DATEINT(
2011, mth, 1))), 7)) as datetime) as [First Monday]
FROM
(
VALUES
(1),
(2),
(3),
(4),
(5),
(6),
(7),
(8),
(9),
(10),
(11),
(12)
) m (mth);
This produces the following result.
{"columns":[{"field":"First Monday","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"First Monday":"2011-01-03 00:00:00.000"},{"First Monday":"2011-02-07 00:00:00.000"},{"First Monday":"2011-03-07 00:00:00.000"},{"First Monday":"2011-04-04 00:00:00.000"},{"First Monday":"2011-05-02 00:00:00.000"},{"First Monday":"2011-06-06 00:00:00.000"},{"First Monday":"2011-07-04 00:00:00.000"},{"First Monday":"2011-08-01 00:00:00.000"},{"First Monday":"2011-09-05 00:00:00.000"},{"First Monday":"2011-10-03 00:00:00.000"},{"First Monday":"2011-11-07 00:00:00.000"},{"First Monday":"2011-12-05 00:00:00.000"}]}
DATEINT can simplify date calculations on the database. Let’s say that you are a company that has a 52-53 week fiscal year, and the year end is defined as the final Saturday in the year end month. You need to calculate the year end date and the number of days in the year.
DECLARE @YEM AS int = 8;
SELECT
yr,
CAST(ye - wct.Modulo(wct.Modulo(ye, 7) -5, 7) AS datetime) AS [Last Saturday],
(ye - wct.Modulo(wct.Modulo(ye, 7) -5, 7)) - (pye - wct.Modulo(wct.Modulo(pye, 7) -5, 7)) AS [Days IN YEAR]
FROM
(
SELECT
yr,
wct.DATEINT(n.yr, @yem + 1, 1) -1 AS ye,
wct.DATEINT(n.yr -1, @yem + 1, 1) -1 AS pye
FROM
(
VALUES
(2005),
(2006),
(2007),
(2008),
(2009),
(2010),
(2011),
(2012),
(2013),
(2014),
(2015),
(2016),
(2017),
(2018),
(2019)
) n(yr)
) m;
This produces the following result.
{"columns":[{"field":"yr","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Last Saturday","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Days IN YEAR","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"yr":"2005","Last Saturday":"2005-08-27 00:00:00.000","Days IN YEAR":"364"},{"yr":"2006","Last Saturday":"2006-08-26 00:00:00.000","Days IN YEAR":"364"},{"yr":"2007","Last Saturday":"2007-08-25 00:00:00.000","Days IN YEAR":"364"},{"yr":"2008","Last Saturday":"2008-08-30 00:00:00.000","Days IN YEAR":"371"},{"yr":"2009","Last Saturday":"2009-08-29 00:00:00.000","Days IN YEAR":"364"},{"yr":"2010","Last Saturday":"2010-08-28 00:00:00.000","Days IN YEAR":"364"},{"yr":"2011","Last Saturday":"2011-08-27 00:00:00.000","Days IN YEAR":"364"},{"yr":"2012","Last Saturday":"2012-08-25 00:00:00.000","Days IN YEAR":"364"},{"yr":"2013","Last Saturday":"2013-08-31 00:00:00.000","Days IN YEAR":"371"},{"yr":"2014","Last Saturday":"2014-08-30 00:00:00.000","Days IN YEAR":"364"},{"yr":"2015","Last Saturday":"2015-08-29 00:00:00.000","Days IN YEAR":"364"},{"yr":"2016","Last Saturday":"2016-08-27 00:00:00.000","Days IN YEAR":"364"},{"yr":"2017","Last Saturday":"2017-08-26 00:00:00.000","Days IN YEAR":"364"},{"yr":"2018","Last Saturday":"2018-08-25 00:00:00.000","Days IN YEAR":"364"},{"yr":"2019","Last Saturday":"2019-08-31 00:00:00.000","Days IN YEAR":"371"}]}
In this example the year end date is the Saturday that falls closest to the last day of the fiscal year end month.
DECLARE @YEM as int = 8;
SELECT yr,
CAST(ye - wct.Modulo(wct.Modulo(ye, 7) - 5, 7) + FLOOR(wct.Modulo(wct.Modulo(
ye, 7) - 5, 7) / 4) * 7 as datetime) as [Nearest Saturday],
(ye - wct.Modulo(wct.Modulo(ye, 7) - 5, 7) + FLOOR(wct.Modulo(wct.Modulo(
ye, 7) - 5, 7) / 4) * 7)
- (pye - wct.Modulo(wct.Modulo(pye, 7) - 5, 7) + FLOOR(wct.Modulo(wct.Modulo(
pye, 7) - 5, 7) / 4) * 7) as [Days In Year]
FROM
(
SELECT yr,
wct.DATEINT(n.yr, @yem + 1, 1) - 1 as ye,
wct.DATEINT(n.yr - 1, @yem + 1, 1) - 1 as pye
FROM
(
VALUES
(2005),
(2006),
(2007),
(2008),
(2009),
(2010),
(2011),
(2012),
(2013),
(2014),
(2015),
(2016),
(2017),
(2018),
(2019)
) n (yr)
) m;
This produces the following result.
{"columns":[{"field":"yr","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Nearest Saturday","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"Days In Year","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"yr":"2005","Nearest Saturday":"2005-09-03 00:00:00.000","Days In Year":"371"},{"yr":"2006","Nearest Saturday":"2006-09-02 00:00:00.000","Days In Year":"364"},{"yr":"2007","Nearest Saturday":"2007-09-01 00:00:00.000","Days In Year":"364"},{"yr":"2008","Nearest Saturday":"2008-08-30 00:00:00.000","Days In Year":"364"},{"yr":"2009","Nearest Saturday":"2009-08-29 00:00:00.000","Days In Year":"364"},{"yr":"2010","Nearest Saturday":"2010-08-28 00:00:00.000","Days In Year":"364"},{"yr":"2011","Nearest Saturday":"2011-09-03 00:00:00.000","Days In Year":"371"},{"yr":"2012","Nearest Saturday":"2012-09-01 00:00:00.000","Days In Year":"364"},{"yr":"2013","Nearest Saturday":"2013-08-31 00:00:00.000","Days In Year":"364"},{"yr":"2014","Nearest Saturday":"2014-08-30 00:00:00.000","Days In Year":"364"},{"yr":"2015","Nearest Saturday":"2015-08-29 00:00:00.000","Days In Year":"364"},{"yr":"2016","Nearest Saturday":"2016-09-03 00:00:00.000","Days In Year":"371"},{"yr":"2017","Nearest Saturday":"2017-09-02 00:00:00.000","Days In Year":"364"},{"yr":"2018","Nearest Saturday":"2018-09-01 00:00:00.000","Days In Year":"364"},{"yr":"2019","Nearest Saturday":"2019-08-31 00:00:00.000","Days In Year":"364"}]}