Chapter 95 SQL Function MINUTE

Chapter 95 SQL Function MINUTE

A time function that returns the minutes of a datetime expression.

outline

{fn MINUTE(time-expression)}
复制代码

parameter

  • time-expression- An expression that is a column name, the result of another scalar function, or a string or numeric literal. It must be parsed as a datetime string or a time integer, where the underlying data type can be represented as %Time, %TimeStampor %PosixTime.

describe

MINUTEReturns an integer specifying the number of minutes in the given time or datetime value. Calculates minutes for $HOROLOGor $ZTIMESTAMPvalues, ODBC-formatted date strings, or timestamps.

Time Expression Timestamp can be a data type %Library.PosixTime(encoded 64-bit signed integer) or a data type %Library.TimeStamp( yyyy-mm-dd hh:mm:ss.fff).

To change the default time format, use the SET OPTIONcommand .

Note that time integers (elapsed seconds) can be provided, but not time strings ( hh:mm:ss). A datetime string ( yyyy-mm-dd hh:mm:ss) is required.

The time portion of the datetime string must be a valid time. Otherwise, an SQLCODE -400error <ILLEGAL VALUE>. The minutes ( mm) part must 0be 59 an integer in the range to . Leading zeros are optional on input; leading zeros are suppressed on output. The seconds ( :ss) part of a datetime string can be omitted, but the minutes part is still returned.

The date part of the datetime string is not validated.

When the minutes part is “0”OR “00”, MINUTEreturns zero minutes. Zero minutes are also returned if no time expression is provided, or if the minute part ( 'hh', 'hh:', 'hh::', or ) of the time expression is omitted entirely.'hh::ss'

You can use DATEPARTor DATENAMEto return the same time information.

也可以使用 MINUTE() 方法调用从 ObjectScript 调用此函数:

$SYSTEM.SQL.Functions.MINUTE(time-expression)
复制代码

示例

以下示例都返回数字 45,因为它是 datetime 字符串中时间表达式的第 45 分钟:

SELECT {fn MINUTE('2018-02-16 18:45:38')} AS ODBCMinutes

45
复制代码
SELECT {fn MINUTE(67538)} AS HorologMinutes

45
复制代码

以下示例也返回 45。如此处所示,时间值的秒部分可以省略:

SELECT {fn MINUTE('2018-02-16 18:45')} AS Minutes_Given

45
复制代码

以下示例返回 0 分钟,因为日期时间字符串中省略了时间表达式:

SELECT {fn MINUTE('2018-02-16')} AS Minutes_Given

0
复制代码

以下示例均返回当前时间的分钟部分:

SELECT {fn MINUTE(CURRENT_TIME)} AS Min_CurrentT,
       {fn MINUTE({fn CURTIME()})} AS Min_CurT,
       {fn MINUTE({fn NOW()})} AS Min_Now,
       {fn MINUTE($HOROLOG)} AS Min_Horolog,
       {fn MINUTE($ZTIMESTAMP)} AS Min_ZTS
       
27	27	27	27	27
复制代码

以下示例显示前导零被抑制。第一个 MINUTE 函数返回长度为 2,其他函数返回长度为 1。省略的时间被认为是 0 分钟,其长度为 1

SELECT LENGTH({fn MINUTE('2018-02-22 11:45:00')}),
       LENGTH({fn MINUTE('2018-02-22 03:05:00')}),
       LENGTH({fn MINUTE('2018-02-22 3:5:0')}),
       LENGTH({fn MINUTE('2018-02-22')})
       
2	1	1	1
复制代码

以下嵌入式 SQL 示例显示 MINUTE 函数识别为区域设置指定的 TimeSeparator 字符:

/// d ##class(PHA.TEST.SQLFunction).Minute()
ClassMethod Minute()
{
	d ##class(%SYS.NLS.Format).SetFormatItem("TimeSeparator", ".")
	&sql(
		SELECT {fn MINUTE('2018-02-22 18.45.38')}
			INTO :a)
	w "minutes=",a
}
复制代码
DHC-APP>d ##class(PHA.TEST.SQLFunction).Minute()
minutes=45
复制代码

Guess you like

Origin juejin.im/post/7080325735440711710