Skip to content

Commit 29f3d30

Browse files
[DOC-11152] Changes to date functions (#383)
--------- Co-authored-by: rakhi-prathap <[email protected]>
1 parent 49b6286 commit 29f3d30

File tree

1 file changed

+87
-19
lines changed

1 file changed

+87
-19
lines changed

modules/n1ql/pages/n1ql-language-reference/datefun.adoc

Lines changed: 87 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -590,13 +590,23 @@ If the date string does not explicitly declare the value of a component, then th
590590
* The month and day default to 1.
591591
* The century (when not specified by year) defaults to 19 if year is greater than or equal to 69, or 20 otherwise.
592592
* All other numeric components default to 0.
593-
* The time zone defaults to the local local system time zone.
593+
* The time zone defaults to the local system time zone.
594594
595595
In cases where the timezone is not specified, the local system time is assumed.
596596
597597
For example, `2016-02-07` is equivalent to `2016-02-07T00:00:00` and parsing just `16` as the year is equivalent to `2016-01-01T00:00:00` in the local system time zone.
598598
====
599599

600+
[NOTE#tzn-date-format]
601+
.TZN Date Format
602+
====
603+
In addition to the date formats listed <<date-string,here>>, Couchbase Server 8.0 and later also supports the `TZN` (Time Zone Name) format.
604+
This format parses date strings in the same way as `TZD` but outputs the time zone name instead of the offset.
605+
For example, the `TZN` representation of the "Australia/Darwin" time zone is `ACST`.
606+
607+
For an example of its usage, refer to the <<ex-str-to-tz,STR_TO_TZ()>> function.
608+
====
609+
600610
[#manipulating-components]
601611
== Manipulating Date Components
602612

@@ -1300,7 +1310,7 @@ WHERE reviews[0].date BETWEEN "2013-01-01 %" AND "2014-01-01 %";
13001310
NOTE: When querying between two dates, you must specify the full date (with time and time zone) or use the wildcard character (%).
13011311

13021312
[#fn-date-format-str]
1303-
== DATE_FORMAT_STR(date1, fmt)
1313+
== DATE_FORMAT_STR(date1, [input-fmt,] fmt)
13041314

13051315
=== Description
13061316

@@ -1313,6 +1323,14 @@ A string, or any valid xref:n1ql-language-reference/index.adoc[expression] which
13131323
+
13141324
If this argument is not a valid date string then `null` is returned.
13151325

1326+
input-fmt::
1327+
The format of the input string, `date1`.
1328+
This can be a string, or any valid xref:n1ql-language-reference/index.adoc[expression] which evaluates to a string.
1329+
+
1330+
*Optional argument*.
1331+
Only required if `date1` is not in a standard format or if the input and output formats are different.
1332+
Available in clusters running Couchbase Server 8.0 and later.
1333+
13161334
fmt::
13171335
A string, or any valid xref:n1ql-language-reference/index.adoc[expression] which evaluates to a string, representing a <<date-string,supported date format>> to output the result as.
13181336
+
@@ -1329,7 +1347,8 @@ A date string in the format specified.
13291347
----
13301348
SELECT DATE_FORMAT_STR('2016-05-15T00:00:23+00:00', '1111-11-11') as full_to_short,
13311349
DATE_FORMAT_STR('2016-05-15', '1111-11-11T00:00:00+00:00') as short_to_full,
1332-
DATE_FORMAT_STR('01:10:05', '1111-11-11T01:01:01Z') as time_to_full;
1350+
DATE_FORMAT_STR('01:10:05', '1111-11-11T01:01:01Z') as time_to_full,
1351+
DATE_FORMAT_STR('15-MAY-2016', 'DD-MON-YYYY', 'YYYY-MM-DD') as month_to_numeric;
13331352
----
13341353
13351354
.Results
@@ -1338,8 +1357,9 @@ SELECT DATE_FORMAT_STR('2016-05-15T00:00:23+00:00', '1111-11-11') as full_to_sho
13381357
[
13391358
{
13401359
"full_to_short": "2016-05-15",
1341-
"short_to_full": "2016-05-15T00:00:00-07:00",
1342-
"time_to_full": "0000-01-01T01:10:05-08:00"
1360+
"short_to_full": "2016-05-15T00:00:00Z",
1361+
"time_to_full": "0000-01-01T01:10:05Z",
1362+
"month_to_numeric": "2016-05-15"
13431363
}
13441364
]
13451365
----
@@ -1799,15 +1819,15 @@ SELECT DATE_TRUNC_MILLIS(1463284740000, 'day') as day,
17991819
[
18001820
{
18011821
"day": 1463270400000,
1802-
"month": 1462147200000,
1803-
"year": 1451696400000
1822+
"month": 1462060800000,
1823+
"year": 1451606400000
18041824
}
18051825
]
18061826
----
18071827
====
18081828

18091829
[#fn-date-trunc-str]
1810-
== DATE_TRUNC_STR(date1, part)
1830+
== DATE_TRUNC_STR(date1, part [,fmt])
18111831

18121832
=== Description
18131833

@@ -1827,6 +1847,14 @@ This function accepts the components `millennium`, `century`, `decade`, `year`,
18271847
+
18281848
If an invalid part is specified, then `null` is returned.
18291849

1850+
fmt::
1851+
The format of the input string, `date1`.
1852+
This can be a string, or any valid xref:n1ql-language-reference/index.adoc[expression] which evaluates to a string.
1853+
+
1854+
*Optional argument*.
1855+
Only required if `date1` is not in a standard format.
1856+
Available in clusters running Couchbase Server 8.0 and later.
1857+
18301858
=== Return Value
18311859

18321860
A date string representing the truncated date.
@@ -1838,7 +1866,8 @@ A date string representing the truncated date.
18381866
----
18391867
SELECT DATE_TRUNC_STR('2016-05-18T03:59:00Z', 'day') as day,
18401868
DATE_TRUNC_STR('2016-05-18T03:59:00Z', 'month') as month,
1841-
DATE_TRUNC_STR('2016-05-18T03:59:00Z', 'year') as year;
1869+
DATE_TRUNC_STR('2016-05-18T03:59:00Z', 'year') as year,
1870+
DATE_TRUNC_STR('05/18/2016 03:59:00', 'month', 'MM/DD/YYYY HH24:MI:SS') as month_custom;
18421871
----
18431872
18441873
.Results
@@ -1848,7 +1877,8 @@ SELECT DATE_TRUNC_STR('2016-05-18T03:59:00Z', 'day') as day,
18481877
{
18491878
"day": "2016-05-18T00:00:00Z",
18501879
"month": "2016-05-01T00:00:00Z",
1851-
"year": "2016-01-01T00:00:00Z"
1880+
"year": "2016-01-01T00:00:00Z",
1881+
"month_custom": "05/01/2016 00:00:00"
18521882
}
18531883
]
18541884
----
@@ -2594,12 +2624,13 @@ AS Milliseconds;
25942624
====
25952625

25962626
[#fn-date-str-to-utc]
2597-
== STR_TO_UTC(date1)
2627+
== STR_TO_UTC(date1 [, [input-fmt,] fmt])
25982628

25992629
=== Description
26002630

26012631
Converts a date string into the equivalent date in UTC.
2602-
The output date format follows the date format of the date passed as input.
2632+
By default, the output date format follows the date format of the date passed as input.
2633+
In clusters running Couchbase Server 8.0 and later, you can specify a different output format if needed.
26032634

26042635
=== Arguments
26052636

@@ -2609,6 +2640,20 @@ This is the date to convert to UTC.
26092640
+
26102641
If this argument is not a valid date format, then `null` is returned.
26112642

2643+
input-fmt::
2644+
The format of the input string, `date1`.
2645+
This can be a string, or any valid xref:n1ql-language-reference/index.adoc[expression] which evaluates to a string.
2646+
+
2647+
*Optional argument*.
2648+
Only required if `date1` is not in a standard format or if the input and output formats are different.
2649+
2650+
fmt::
2651+
The format of the resulting UTC date.
2652+
This can be a string, or any valid xref:n1ql-language-reference/index.adoc[expression] which evaluates to a string, and must be a <<date-string,supported date format>>.
2653+
+
2654+
*Optional argument*.
2655+
If not specified, the output date format follows the date format of the input string, `date1`.
2656+
26122657
=== Return Value
26132658

26142659
A single date string representing the date string converted to UTC.
@@ -2619,7 +2664,8 @@ A single date string representing the date string converted to UTC.
26192664
[source,sqlpp]
26202665
----
26212666
SELECT STR_TO_UTC('1111-11-11T00:00:00+08:00') as full_date,
2622-
STR_TO_UTC('1111-11-11') as short_date;
2667+
STR_TO_UTC('1111-11-11') as short_date,
2668+
STR_TO_UTC('1111-11-11', 'YYYY-MM-DD', 'YYYY-MM-DD HH:MI:SS') as utc_date;
26232669
----
26242670
26252671
.Results
@@ -2628,19 +2674,21 @@ STR_TO_UTC('1111-11-11') as short_date;
26282674
[
26292675
{
26302676
"full_date": "1111-11-10T16:00:00Z",
2631-
"short_date": "1111-11-11"
2677+
"short_date": "1111-11-11",
2678+
"utc_date": "1111-11-11 12:00:00"
26322679
}
26332680
]
26342681
----
26352682
====
26362683

26372684
[#fn-date-str-to-tz]
2638-
== STR_TO_TZ(date1, tz)
2685+
== STR_TO_TZ(date1, tz [, [input-fmt,] fmt])
26392686

26402687
=== Description
26412688

26422689
Converts a date string to its equivalent in the specified timezone.
2643-
The output date format follows the date format of the date passed as input.
2690+
By default, the output date format follows the date format of the date passed as input.
2691+
In clusters running Couchbase Server 8.0 and later, you can specify a different output format if needed.
26442692

26452693
=== Arguments
26462694

@@ -2655,28 +2703,48 @@ A string, or any valid xref:n1ql-language-reference/index.adoc[expression] which
26552703
+
26562704
If this argument is not a valid timezone, then `null` is returned.
26572705

2706+
input-fmt::
2707+
The format of the input string, `date1`.
2708+
This can be a string, or any valid xref:n1ql-language-reference/index.adoc[expression] which evaluates to a string.
2709+
+
2710+
*Optional argument*.
2711+
Only required if `date1` is not in a standard format or if the input and output formats are different.
2712+
2713+
fmt::
2714+
The format of the output date.
2715+
This can be a string, or any valid xref:n1ql-language-reference/index.adoc[expression] which evaluates to a string, and must be a <<date-string,supported date format>>.
2716+
+
2717+
*Optional argument*.
2718+
If not specified, the output date format follows the date format of the input string, `date1`.
2719+
26582720
=== Return Value
26592721

26602722
A single date string representing the date string converted to the specified timezone.
26612723

2724+
[[ex-str-to-tz]]
26622725
=== Examples
26632726

26642727
====
26652728
[source,sqlpp]
26662729
----
26672730
SELECT STR_TO_TZ('1111-11-11T00:00:00+08:00', 'America/New_York') as est,
26682731
STR_TO_TZ('1111-11-11T00:00:00+08:00', 'UTC') as utc,
2669-
STR_TO_TZ('1111-11-11', 'UTC') as utc_short;
2732+
STR_TO_TZ('1111-11-11', 'UTC') as utc_short,
2733+
STR_TO_TZ('1111-11-11', 'UTC', 'YYYY-MM-DD', 'YYYY-MM-DD HH:MI:SS') as utc_datetime,
2734+
STR_TO_TZ('1111-11-11T00:00:00+07:00', 'Europe/Paris',
2735+
"YYYY-MM-DDThh:mm:ssTZD", "YYYY-MM-DDThh:mm:ssTZN") as tzn;
26702736
----
26712737
26722738
.Results
26732739
[source,json]
26742740
----
26752741
[
26762742
{
2677-
"est": "1111-11-10T11:00:00-05:00",
2743+
"est": "1111-11-10T11:03:58-04:56",
26782744
"utc": "1111-11-10T16:00:00Z",
2679-
"utc_short": "1111-11-11"
2745+
"utc_short": "1111-11-11",
2746+
"utc_datetime": "1111-11-11 12:00:00",
2747+
"tzn": "1111-11-10T17:09:21LMT"
26802748
}
26812749
]
26822750
----

0 commit comments

Comments
 (0)