1. Главная страница » Компьютеры

Ms sql количество дней между датами

Автор: | 16.12.2019

DATEDIFF() — данная функция вычисляет разницу между датами.

Содержание

Читайте также:  Bosch mcm 64051 обзор

Синтаксис

datepart Abbreviation
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw, w
hour hh
minute mi, n
second ss, s
millisecond ms
microsecond mcs
nanosecond ns

Пример 1

Итак, мы хотим узнать количество дней между двумя датами.

� езультат запроса:

DiffDate
61

Пример 2

Итак, мы хотим узнать количество дней между двумя датами (обратите внимание, что результат будет отрицательным).

������� ����������: SQL Server ���� ������ SQL Azure Azure Synapse Analytics (��������� ������ SQL) Parallel Data Warehouse APPLIES TO: SQL Server Azure SQL Database Azure Synapse Analytics (SQL DW) Parallel Data Warehouse

��� ������� ���������� ���������� ������������ ������ (����� ����� �� ������), ��������� � ��������� datepart, �� ������ �������, ��������� � ���������� startdateenddate. This function returns the count (as a signed integer value) of the specified datepart boundaries crossed between the specified startdate and enddate.

�������� � �������, ������� ������������ ������������ ������� �������� ���������� startdateenddate, ��. � ������ DATEDIFF_BIG (Transact-SQL). See DATEDIFF_BIG (Transact-SQL) for a function that handles larger differences between the startdate and enddate values. ����� ���� ����� ������ � ������� ���� � ������� � ����� Transact-SQL Transact-SQL ��. � ������ ���� ������ � ������� ���� � ������� (Transact-SQL). See Date and Time Data Types and Functions (Transact-SQL) for an overview of all Transact-SQL Transact-SQL date and time data types and functions.

�������������� ����������� � Transact-SQL Transact-SQL Syntax Conventions

��������� Syntax

��������� Arguments

datepart datepart
�������, � ������� ������� DATEDIFF �������� � ������� ����� startdateenddate. The units in which DATEDIFF reports the difference between the startdate and enddate. � ����� ������������ �������� datepart ��������� month ��� second . Commonly used datepart units include month or second .

�������� datepart �� ����� ���� ������� � ���������� ��� � ���� ������ � ��������, �������� ‘month’ . The datepart value cannot be specified in a variable, nor as a quoted string like ‘month’ .

� ����������� ���� ������� ����������� ��� ���������� �������� datepart. The following table lists all the valid datepart values. ������� DATEDIFF ��������� ���� ������ ��� datepart, ���� ����� ����������� ���������� ������� �����. DATEDIFF accepts either the full name of the datepart, or any listed abbreviation of the full name.

��� datepart datepart name ���������� datepart datepart abbreviation
year year yy, yyyy yy, yyyy
quarter quarter qq, q qq, q
month month mm, m mm, m
dayofyear dayofyear dy, y dy, y
day day dd, d dd, d
week week wk, ww wk, ww
hour hour �� hh
minute minute mi, n mi, n
second second ss, s ss, s
millisecond millisecond ms ms
microsecond microsecond mcs mcs
nanosecond nanosecond ns ns

������ ���������� ��� ��������� datepart � ���������� ����� ����� datepart ����� ���������� ���� � �� �� ��������. Each specific datepart name and abbreviations for that datepart name will return the same value.

startdate startdate
���������, ������� ����� ���� ��������� � ���� �� ��������� ��������. An expression that can resolve to one of the following values:

  • datedate
  • datetimedatetime
  • datetimeoffsetdatetimeoffset
  • datetime2datetime2
  • smalldatetimesmalldatetime
  • timetime

�� ��������� ��������������� ����������� �������������� ������ ����. Use four-digit years to avoid ambiguity. �������� � ���������� ����������� ���� ��. � ������ ��������� ��������� ������������ ������� two digit year cutoff. See Configure the two digit year cutoff Server Configuration Option for information about two-digit year values.

enddate enddate
��. �������� ��������� startdate. See startdate.

��� ������������ ������ Return Type

int int

������������ �������� Return Value

�������� ���� int, �������������� ������� ����� ����������� startdateenddate � ��������, ������������ ���������� datepart. The int difference between the startdate and enddate, expressed in the boundary set by datepart.

��������, SELECT DATEDIFF(day, ‘2036-03-01’, ‘2036-02-28’); ���������� �������� -2, �������� �� ��, ��� 2036 � ���������� ���. For example, SELECT DATEDIFF(day, ‘2036-03-01’, ‘2036-02-28’); returns -2, hinting that 2036 must be a leap year. ��� ��������, ���, ���� ������ �� startdDate "2036-03-01" � ��������� -2 ���, ����� ���������� �������� enddate "2036-02-28". This case means that if we start at startdate ‘2036-03-01’, and then count -2 days, we reach the enddate of ‘2036-02-28’.

� �������� ������������� �������� ��� ��������� ��� int (�� �2 147 483 648 �� 2 147 483 647) DATEDIFF ���������� ��������� �� ������. For a return value out of range for int (-2,147,483,648 to +2,147,483,647), DATEDIFF returns an error. ��� ������������ ������������ ������� ����� startdateenddate ���������� 24 ��� 20 ����� 31 ������ � 23 647 ������. For millisecond, the maximum difference between startdate and enddate is 24 days, 20 hours, 31 minutes and 23.647 seconds. ��� ������� ������������ ������� ���������� 68 ���, 19 ����, 3 ����, 14 ����� � 7 ������. For second, the maximum difference is 68 years, 19 days, 3 hours, 14 minutes and 7 seconds.

���� ����� ����������, startdateenddate, ��������� ������ �������� �������, � �������� datepart �� �������� �������� ������� datepart, �� DATEDIFF ���������� �������� 0. If startdate and enddate are both assigned only a time value, and the datepart is not a time datepart, DATEDIFF returns 0.

��� ���������� ������������� �������� DATEDIFF ���������� ��������� �������� ������� ������ ��� ���������� startdate ��� enddate. DATEDIFF uses the time zone offset component of startdate or enddate to calculate the return value.

��� ��� �������� ���� smalldatetime ����� �������� �� ������, �� ��� ������������� � ���������� startdateenddate �������� ���� smalldatetime ������� � ������������ � ������������ �������� ������ ����� 0. Because smalldatetime is accurate only to the minute, seconds and milliseconds are always set to 0 in the return value when startdate or enddate have a smalldatetime value.

���� ���������� ���� ������ date ��������� ������ �������� �������, � �������� ����������� ����� ���� DATEDIFF ������ �������� �� ���������: 1900-01-01 . If only a time value is assigned to a date data type variable, DATEDIFF sets the value of the missing date part to the default value: 1900-01-01 . ���� ���������� ���� ������ time ��� date ��������� ������ �������� ����, � �������� ����������� ����� ������� DATEDIFF ������ �������� �� ���������: 00:00:00 . If only a date value is assigned to a variable of a time or date data type, DATEDIFF sets the value of the missing time part to the default value: 00:00:00 . ���� � ����� �� ���������� startdate ��� enddate ������� ������ �����, � � ������ ������ ����, � �������� ����������� ���������� DATEDIFF ������ �������� �� ���������. If either startdate or enddate have only a time part and the other only a date part, DATEDIFF sets the missing time and date parts to the default values.

���� ��������� startdateenddate ����� ������ ���� ������ ����, �� ��� ���� ���� �� ��� ����� ������ ������ ������� ��� �������� ����� ������� ���������, DATEDIFF ����������� �������� 0 ����������� ������ ������� ���������. If startdate and enddate have different date data types, and one has more time parts or fractional seconds precision than the other, DATEDIFF sets the missing parts of the other to 0.

�������, ���������� ���������� datepart datepart boundaries

����������� ���� ���������� ����� ���������� �������� ���������� startdateenddate. The following statements have the same startdate and the same enddate values. ��������� ���� �������� ���������, � ��������� ������� ����� ���� ���������� 100 ���������� (0,0000001 �������). Those dates are adjacent and they differ in time by a hundred nanoseconds (.0000001 second). ������� ����� ����������� startdateenddate � ������ ���������� ���������� ���� ����������� ��� ��������� ������� ��������� datepart. The difference between the startdate and enddate in each statement crosses one calendar or time boundary of its datepart. ������ ��������� ���������� �������� 1. Each statement returns 1.

���� ��������� startdateenddate ����� ������ �������� ����, �� ���������� �������� ����������� ������, DATEDIFF ������ 0 ��� ����� week ��������� datepart. If startdate and enddate have different year values, but they have the same calendar week values, DATEDIFF will return 0 for datepart week.

Remarks Remarks

����������� DATEDIFF � ������������ SELECT

  • , WHERE , HAVING , GROUP BY � ORDER BY . Use DATEDIFF in the SELECT
  • , WHERE , HAVING , GROUP BY and ORDER BY clauses.
  • ������� DATEDIFF ������ �������� ��������� �������� � ���� datetime2. DATEDIFF implicitly casts string literals as a datetime2 type. ��� ��������, ��� DATEDIFF �� ������������ ������ ��� (���, �����, �����) ��� �������� ���� � ���� ������. This means that DATEDIFF does not support the format YDM when the date is passed as a string. ��� ������������� ������� ��� (���, �����, �����) ���������� ���� �������� ������ � ���� datetime ��� smalldatetime. You must explicitly cast the string to a datetime or smalldatetime type to use the YDM format.

    �������� SET DATEFIRST �� ������ �� DATEDIFF . Specifying SET DATEFIRST has no effect on DATEDIFF . DATEDIFF ������ ������� ����������� ������ ���� ������, ����� ���������� ����������� ������ �������. DATEDIFF always uses Sunday as the first day of the week to ensure the function operates in a deterministic way.

    DATEDIFF ����� ������������� � ��������� �� ������ ��� ����� ������ ���������, ���� ������� ����� enddatestartdate ������������ ����� ��������, ��������� �� ������� ���������, ����������� ��� int. DATEDIFF may overflow with a precision of minute or higher if the difference between enddate and startdate returns a value that is out of range for int.

    ������� Examples

    � ���� �������� ��������� ���������� ���� ������������ � �������� ���������� ��� ���������� startdateenddate. These examples use different types of expressions as arguments for the startdate and enddate parameters.

    A. A. �������� �������� � �������� ��������� � �������� ���� Specifying columns for startdate and enddate

    � ���� ������� �������������� ���������� ������ ����, ������������ ����� ������ � ���� �������� �������. This example calculates the number of day boundaries crossed between dates in two columns in a table.

    �. B. �������� ������������ ������������� ���������� � �������� ��������� � �������� ���� Specifying user-defined variables for startdate and enddate

    � ���� ������� � �������� ���������� startdateenddate ��������� ������������ ������������� ����������. In this example, user-defined variables serve as arguments for startdate and enddate.

    �. C. �������� ��������� ��������� ������� � �������� ��������� � �������� ���� Specifying scalar system functions for startdate and enddate

    � ���� ������� � �������� ���������� startdateenddate ��������� ��������� ��������� �������. This example uses scalar system functions as arguments for startdate and enddate.

    �. D. �������� ��������� ��������� �������� � ��������� ������� � �������� ��������� � �������� ���� Specifying scalar subqueries and scalar functions for startdate and enddate

    � ���� ������� � �������� ���������� startdateenddate ��������� ��������� ��������� �������. This example uses scalar subqueries and scalar functions as arguments for startdate and enddate.

    �. E. �������� �������� � �������� ��������� � �������� ���� Specifying constants for startdate and enddate

    � ���� ������� � �������� ���������� startdateenddate ������������ ���������� ���������. This example uses character constants as arguments for startdate and enddate.

    �. F. �������� �������� ��������� � ��������� ��������� ������� � �������� �������� ���� Specifying numeric expressions and scalar system functions for enddate

    � ���� ������� � �������� ���������� ��� enddate ������������ �������� ��������� (GETDATE() + 1) � ��������� ��������� ������� GETDATE � SYSDATETIME . This example uses a numeric expression, (GETDATE() + 1) , and scalar system functions GETDATE and SYSDATETIME , as arguments for enddate.

    �. G. �������� ����������� ������� � �������� ��������� ���� Specifying ranking functions for startdate

    � ���� ������� � �������� ��������� startdate. ������������ ����������� �������. This example uses a ranking function as an argument for startdate.

    �. H. �������� ���������� ������� ������� � �������� ��������� ���� Specifying an aggregate window function for startdate

    � ���� ������� � �������� ��������� startdate ������������ ���������� ������� �������. This example uses an aggregate window function as an argument for startdate.

    �. I. ����������� ������� ����� startdate � enddate � ���� ��������� ����������� ���� Finding difference between startdate and enddate as date parts strings

    ���� ���������� �������������� �����. Here is the result set.

    �������: Azure Synapse Analytics (��������� ������ SQL) Azure Synapse Analytics (SQL DW) � ������������ ��������� ������ Parallel Data Warehouse Examples: Azure Synapse Analytics (��������� ������ SQL) Azure Synapse Analytics (SQL DW) and ������������ ��������� ������ Parallel Data Warehouse

    � ���� �������� ��������� ���������� ���� ������������ � �������� ���������� ��� ���������� startdateenddate. These examples use different types of expressions as arguments for the startdate and enddate parameters.

    �. J. �������� �������� � �������� ��������� � �������� ���� Specifying columns for startdate and enddate

    � ���� ������� �������������� ���������� ������ ����, ������������ ����� ������ � ���� �������� �������. This example calculates the number of day boundaries crossed between dates in two columns in a table.

    �. K. �������� ��������� ��������� �������� � ��������� ������� � �������� ��������� � �������� ���� Specifying scalar subqueries and scalar functions for startdate and enddate

    � ���� ������� � �������� ���������� startdateenddate ��������� ��������� ��������� �������. This example uses scalar subqueries and scalar functions as arguments for startdate and enddate.

    �. L. �������� �������� � �������� ��������� � �������� ���� Specifying constants for startdate and enddate

    � ���� ������� � �������� ���������� startdateenddate ������������ ���������� ���������. This example uses character constants as arguments for startdate and enddate.

    �. M. �������� ����������� ������� � �������� ��������� ���� Specifying ranking functions for startdate

    � ���� ������� � �������� ��������� startdate. ������������ ����������� �������. This example uses a ranking function as an argument for startdate.

    �. N. �������� ���������� ������� ������� � �������� ��������� ���� Specifying an aggregate window function for startdate

    � ���� ������� � �������� ��������� startdate ������������ ���������� ������� �������. This example uses an aggregate window function as an argument for startdate.

    ������. ��������� ����� ����� � ��������� ����� ����� ������ � ‘2011-10-07 23:43:00’ � ‘2011-10-08 01:23:00’

    SQL Server

    ���������� ������� DATEDIFF ������ ��������:

    ��������� � 100 �����.

    � ������� ������������ ����������� ������������� ���� (ISO) � ���� ��������� ������ ‘yyyy-mm-ddThh:mm:ss’, ������� �� ������� �� �� ��������� �������� �������, �� � �� ������ �������.

    MySQL

    ������� DATEDIFF ���� � � MySQL, ������ ��� ����� ������ ������ �����. DATEDIFF ��������� ����� ���� ����� ����� ������, ���������� ����������� ���� �������. ��� ���� ���� ���� ������������ � ������� ����-�����, ������������ ������ ������������ ����. ������� ��� ��� ������������������ ������� ����� ���� � ��� �� ��������� -1. ������������� ��������� ����� �������, ���� ������ �������� ������ �������.

    ���� ���� �� ������� ���� � ������, ���� �������� ����� ������ ���������� ��� ���� �������:

    ������� �� ����� ������ ����� �������� ��� ������ ������ ���������� ������� � TIMESTAMPDIFF, ������� ����������� ������� DATEDIFF � ���� ����������������� ��������) � ������������� ������������ ����, ����������� ��� ��������, ����������� � ���������� ������� � ����������� ����� ������. SQL Server:

    PostgreSQL

    � PostgreSQL ��� �������, �������� DATEDIFF (SQL Server) ��� TIMESTAMPDIFF (MySQL). ������� ��� ������� ������ ����� ��������� ��������� ������������������ ��������:

    1. ����������� �������� ����� ����� ������ ����������;
    2. ��������� ����� ������ � ���������;
    3. �������� ���������� �� ���� 2 �������� �� 60, ����� �������� ��������� � �������.

    ��� ��������� ��������� ����� ����� �������� ���� �������� ������������� ����, ��� ���� ��������� ����� �������������� ����:

    ��������� — "01:40:00", ������� ���� �� ��� ����, ��� ���� ��� � ����� �����.

    ����� ����� ��������������� ���������� �������� AGE , ������� ��������� ������ ������� �������������� ���� ����� ����������:

    ��� ��������� ����� ������ � ��������� ������������� ��������

    �������� ��������� ���������� ��������� �������� ��� �������� ��������. ��� ��������� �������������� ������� ������ ������� ���������� ��������� �� 60:

    � ������ ������ �� ����� ���������, ���������� ����� ������, ��������� ��� �������� ������� � ������ �� ��������� ������. � ��������� ������, ����� �������� ���������� �����, ��������: 99.75:

    Oracle

    Oracle ���� �� ����� � ����� �������� ������� ���� DATEDIFF. ����� ����, Oracle �� ������������ ����������� ��������� ������������� ����/�������.

    �� ����� ��������� �������� � �������, ������ �� ��������, ��� �������� ��� (�������� ���� date ) � Oracle ���� � ���������� ����� ���� (�����). ����� ��� ���������� ��������� � ������� ����� ������ �������� ��� �������� �� 24 (����� ����� � ������), � ����� �� 60 (����� ����� � ����):

    �������� ������� ����� �������� � ������ ��������� ���������.

    Добавить комментарий

    Ваш адрес email не будет опубликован. Обязательные поля помечены *