Date 関数とTime 関数は、日付、時刻、タイムスタンプを返したり操作したりします。

日付/時刻関数の解析と書式設定には、java.text.SimpleDateFormatクラス内で確立された規約を使用し、これらの関数で使用できる書式を定義します。このクラスがどのようにフォーマットを定義するかについては、Oracle Java documentationで詳しく説明しています。

To view the full table, click the expand button in its top right corner


Function

Definition

Data Type Constraint

CURDATE()

Returns current date

returns date

CURTIME()

Returns current time

returns time

NOW()

Returns current timestamp (date and time)

returns timestamp

DATE_TRUNC('precision',x)

Truncates DATE, TIME, or TIMESTAMP to a specified precision (for example, DATE_TRUNC('MONTH', startDate). Valid values for precision are:

  • MICROSECONDS
  • MILLISECONDS
  • SECOND
  • MINUTE
  • HOUR
  • DAY
  • WEEK
  • MONTH
  • QUARTER
  • YEAR
  • DECADE
  • CENTURY
  • MILLENNIUM

x in {time, date, timestamp}, returns timestamp

DAYNAME(x)

Returns name of the day

x in {date, timestamp}, returns string

DAYOFMONTH(x)

Returns day of the month

x in {date, timestamp}, returns integer

DAYOFWEEK(x)

Returns day of the week (Sunday=1)

x in {date, timestamp}, returns integer

DAYOFYEAR(x)

Returns Julian day number

x in {date, timestamp}, returns integer

EXTRACT(YEAR | MONTH | DAY | HOUR | MINUTE | SECOND FROM X)

Returns the given field value from the date value x. Produces the same result as the associated YEAR, MONTH, DAYOFMONTH, HOUR, MINUTE, SECOND functions. The SQL specification also allows for TIMEZONE_HOUR and TIMEZONE_MINUTE as extraction targets. In the CData Virtuality Server, all date values are in the time zone of the server

x in {date, time, timestamp}, returns integer

FORMATDATE(x, y)

Formats date x using format y

x is date, y is string, returns string

FORMATTIME(x, y)

Formats time x using format y

x is time, y is string, returns string

FORMATTIMESTAMP(x, y)

Formats timestamp x using format y

x is timestamp, y is string, returns string

FROM_UNIXTIME (unix_timestamp)

Returns the Unix timestamp (in seconds) as Timestamp value

unix_timestamp (in seconds) where unix_timestamp in {byte, short, integer} and unix_timestamp <= 2147483647

HOUR(x)

Returns hour (in military 24-hour format)

x in {time, timestamp}, returns integer

MINUTE(x)

Returns minute

x in {time, timestamp}, returns integer

MODIFYTIMEZONE (timestamp, endTimeZone, startTimeZone)

Returns a timestamp based upon the incoming timestamp adjusted for the differential between the start and end time zones. i.e. if the server is in GMT-6, then MODIFYTIMEZONE{ts '2006-01-10 04:00:00.0'}(,'GMT-7', 'GMT-8') will return the timestamp {ts '2006-01-10 05:00:00.0'} as read in GMT-6. The value has been adjusted 1 hour ahead to compensate for the difference between GMT-7 and GMT-8. If a time zone is not understood then no error will be thrown but the function will implicitly interpret it as GMT

startTimeZone and endTimeZone are strings, returns a timestamp

MODIFYTIMEZONE (timestamp, startTimeZone)

Returns timestamp in the same manner as MODIFYTIMEZONE (timestamp,  endTimeZone,  startTimeZone), but will assume that the endTimeZone is the same as the server process

Timestamp is a timestamp; startTimeZone  is a string, returns a timestamp

MONTH(x)

Returns month

x in {date, timestamp}, returns integer

MONTHNAME(x)

Returns name of the month

x in {date, timestamp}, returns string

PARSEDATE(x, y)

Parses date from x using format y

x, y in {string}, returns date

PARSETIME(x, y)

Parses time from x using format y

x, y in {string}, returns time

PARSETIMESTAMP(x,y)

Parses timestamp from x using format y

x, y in {string}, returns timestamp

PARSETIMESTAMP(x,y,z)

Parses timestamp from x using format y and language tag z

SELECT PARSETIMESTAMP('Thu Dec 23 18:26:07 +0000 2010', 'EEE MMM dd HH:mm:ss ZZZZZ yyyy', 'en');;
SELECT PARSETIMESTAMP('Thu Dec 23 18:26:07 +0000 2010', 'EEE MMM dd HH:mm:ss ZZZZZ yyyy', 'en.US');;

More information about valid language tags:  https://docs.oracle.com/javase/7/docs/api/java/util/Locale.html#forLanguageTag(java.lang.String) 

x, y, z in {string}, returns timestamp

QUARTER(x)

Returns quarter

x in {date, timestamp}, returns integer

SECOND(x)

Returns seconds

x in {time, timestamp}, returns integer

SERVERTIMEZONE()

Returns the server time zone according to the list of tz database time zones

returns a string in tz tag format, e.g.  Africa/Abidjan

TIMESTAMPADD(interval, count, timestamp)

Adds a specified interval count to the timestamp. Interval can be one of the following keywords:

  1. SQL_TSI_FRAC_SECOND - fractional seconds (billionths of a second)
  2. SQL_TSI_SECOND - seconds
  3. SQL_TSI_MINUTE - minutes
  4. SQL_TSI_HOUR - hours
  5. SQL_TSI_DAY - days
  6. SQL_TSI_WEEK - weeks using Sunday as the first day
  7. SQL_TSI_MONTH - months
  8. SQL_TSI_QUARTER - quarters (3 months)
  9. SQL_TSI_YEAR - years

The full interval amount based upon calendar fields will be added. For example, adding 1 QUARTER will move the timestamp up by three full months and not just to the start of the next calendar quarter.

Count can be of long or integer data type but in the range of integer data type ( -2,147,483,648 to 2,147,483,647 ) in both cases

count in {integer, long}, timestamp in {date, time, timestamp},  returns  timestamp 

TIMESTAMPCREATE(date, time)

Creates timestamp from date and time

date in {date}, time in {time}, returns timestamp

TIMESTAMPDIFF(interval, startTime, endTime)

Calculates the number of date part intervals crossed between the two timestamps.

Interval can be one of the same keywords as used by timestampadd; startTime, endTime are in {date, time, timestamp} and the return value is a long.

If (endTime > startTime), a non-negative number will be returned. If (endTime < startTime), a non-positive number will be returned. The date part difference is counted regardless of how close the timestamps are. For example, '2000-01-02 00:00:00.0' is still considered 1 hour ahead of '2000-01-01 23:59:59.999999'.

(情報) Compatibility issue: TIMESTAMPDIFF typically returns an integer, however, CData Virtuality Server version returns a long. You may receive an exception if you expect a value out of the integer range from a pushed down TIMESTAMPDIFF

startTime, endTime are in {date, time, timestamp}, returns long

WEEK(x)

Returns week in a year

x in {date, timestamp}, returns integer

YEAR(x)

Returns four-digit year

x in {date, timestamp}, returns integer

DATE_TRUNC function available since v4.0.8

Parsing Date Data Types from Strings

CData Virtuality Server は、'19970101' や'1996/31/1' のような異なるフォーマットで表示される日付を含む文字列を、日付関連のデータ型に暗黙的に変換しません。しかし、以下のparseDateparseTimeparseTimestamp関数を使用することで、異なる書式の文字列を適切なデータ型に明示的に変換することができます。これらの関数は、java.text.SimpleDateFormatクラス内で確立された規約を使用して、これらの関数で使用できる書式を定義しています。このクラスが日付と時刻の文字列書式を定義する方法については、Oracle Java documentationを参照してください。

例えば、java.text.SimpleDateFormatの規約に従ったフォーマット文字列で、これらの関数コールを使って文字列を解析し、必要なデータ型を返すことができます:

String

Function Call To Parse String

'1997010'

PARSEDATE(myDateString, 'yyyyMMdd')

'31/1/1996'

PARSEDATE(myDateString, 'dd''/''MM''/''yyyy')

'22:08:56 CST'

PARSETIME(myTime, 'HH:mm:ss z')

'03.24.2003 at 06:14:32'

PARSETIMESTAMP(myTimestamp, 'MM.dd.yyyy ''at'' hh:mm:ss')

Date and Time Pattern Strings

日付と時刻の書式は、 日付と時刻のパターン 文字列で指定します。'z'日付と時刻のパターン文字列内では、 'A' から 'Z' まで、および 'a' から  までの引用符で囲まれていない文字は、日付または時刻文字列の構成要素を表すパターン文字として解釈されます。テキストは、解釈を避けるためにシングルクォート(')を使って引用することができます。 "''" はシングルクォートを表します。それ以外の文字は解釈されず、フォーマット時に出力文字列にコピーされるか、パース時に入力文字列と照合されるだけです。

'z'以下のパターン文字が定義されています( 'A'  'Z' および 'a' から  までの他のすべての文字は予約されています。テーブルに列挙されているパターンは例であり、使用可能なパターンを網羅したものではありません):

Letter

Date or Time Component

Presentation

Example Pattern(s)

Example(s)

G

Era designator

Text

G

AD

y

Year

Year 

yyyy

1996

yy

96

Y

Week year (the year to which the given week belongs)

Year

YYYY

2009

YY

09

M

Month in year

Month

MMMM

July

MMM

Jul

MM

07

w

Week in year (number of the given week in the given year)

Number 

w

27

W

Week in month

Number 

W

2

D

Day in year 

Number 

DDD

003; 070; 189

d

Day in month

Number 

dd

10

F

Day of the week in a month

Number 

F

2

E

Day name in week

Text 

EEEE

Tuesday

EEE

Tue

u

Day number of week (1 = Monday, ..., 7 = Sunday)

Number 

u

1

a

Am/pm marker

Text 

a

PM

H

Hour in a day (0-23)

Number 

H

0

k

Hour in day (1-24)

Number 

kk

04

K

Hour in am/pm (0-11)

Number 

K

4

h

Hour in am/pm (1-12)

Number 

hh

01

m

Minute in hour 

Number 

m

30

s

Second in minute

Number 

s

55

S

Millisecond 

Number 

SSS

978

z

Time zone

General time zone

zzzz

Pacific Standard Time

z

PST

Z

Time zone

RFC 822 time zone

Z

-0800

X

Time zone

ISO 8601 time zone

X

-08

XXX

-0800

XXXX

-08:00

Specifying Time Zones

TIMEゾーンはいくつかの形式で指定できます。ESTは「東部標準時」を意味するなど、一般的な略語は使用できますが、曖昧になる可能性があるため、使用はお勧めしません。明確なタイムゾーンは、大陸または海洋、最大の都市の形で定義されます。例えば、America/New_York、America/Buenos_Aires、Europe/London。さらに、GMTオフセットでカスタムタイムゾーンを指定することもできます:GMT[+/-]HH:MM(例:  GMT-05:00)。

CData Virtuality Server はリモートのデータソースで設定されたタイムゾーン を制御できないことに注意してください。そのため、データソースと CData Virtuality Sources の間にタイム ゾーンの違いがある場合、タイムゾーンが関係するたびに Date-Time 関数の実行結果に違いが生じる可能性があります。

See Also

Setting the Server Locale for the WEEK and DAYOFWEEK Functions WEEK およびDAYOFWEEK 関数が日曜日を週の最初か最後として扱うかどうかを確認する方法と、それを変更する方法については、こちらをご覧ください。

Create Unix Timestamps 現在のタイムスタンプや指定されたタイムスタンプに対してUnix タイムスタンプを作成する方法については、こちらをご覧ください。

Use Values Larger Than 2147483647 with FROM_UNIXTIME 32ビット符号付きバイナリ整数を超える値を使用する必要がある場合の回避策については、こちらをご覧ください。

Filtering for Dates that Belong to the Current Week with SQL TIMESTAMPADD()DAYOFWEEK()CURDATE() を使用して現在の週の最初と最後の日付を動的に検索する方法については、こちらをご覧ください。

MSSQL ENDOFMONTH Replacement Microsoft SQL を使用している場合の回避策についてはこちらをご覧ください。

Check for (!)= '' and (NOT) NULL at the Same Time こちらは参考までに。