You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: modules/n1ql/pages/n1ql-language-reference/datefun.adoc
+87-19Lines changed: 87 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -590,13 +590,23 @@ If the date string does not explicitly declare the value of a component, then th
590
590
* The month and day default to 1.
591
591
* The century (when not specified by year) defaults to 19 if year is greater than or equal to 69, or 20 otherwise.
592
592
* 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.
594
594
595
595
In cases where the timezone is not specified, the local system time is assumed.
596
596
597
597
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.
598
598
====
599
599
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
+
600
610
[#manipulating-components]
601
611
== Manipulating Date Components
602
612
@@ -1300,7 +1310,7 @@ WHERE reviews[0].date BETWEEN "2013-01-01 %" AND "2014-01-01 %";
1300
1310
NOTE: When querying between two dates, you must specify the full date (with time and time zone) or use the wildcard character (%).
1301
1311
1302
1312
[#fn-date-format-str]
1303
-
== DATE_FORMAT_STR(date1, fmt)
1313
+
== DATE_FORMAT_STR(date1, [input-fmt,] fmt)
1304
1314
1305
1315
=== Description
1306
1316
@@ -1313,6 +1323,14 @@ A string, or any valid xref:n1ql-language-reference/index.adoc[expression] which
1313
1323
+
1314
1324
If this argument is not a valid date string then `null` is returned.
1315
1325
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
+
1316
1334
fmt::
1317
1335
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.
1318
1336
+
@@ -1329,7 +1347,8 @@ A date string in the format specified.
1329
1347
----
1330
1348
SELECT DATE_FORMAT_STR('2016-05-15T00:00:23+00:00', '1111-11-11') as full_to_short,
1331
1349
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;
1333
1352
----
1334
1353
1335
1354
.Results
@@ -1338,8 +1357,9 @@ SELECT DATE_FORMAT_STR('2016-05-15T00:00:23+00:00', '1111-11-11') as full_to_sho
1338
1357
[
1339
1358
{
1340
1359
"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"
1343
1363
}
1344
1364
]
1345
1365
----
@@ -1799,15 +1819,15 @@ SELECT DATE_TRUNC_MILLIS(1463284740000, 'day') as day,
1799
1819
[
1800
1820
{
1801
1821
"day": 1463270400000,
1802
-
"month": 1462147200000,
1803
-
"year": 1451696400000
1822
+
"month": 1462060800000,
1823
+
"year": 1451606400000
1804
1824
}
1805
1825
]
1806
1826
----
1807
1827
====
1808
1828
1809
1829
[#fn-date-trunc-str]
1810
-
== DATE_TRUNC_STR(date1, part)
1830
+
== DATE_TRUNC_STR(date1, part [,fmt])
1811
1831
1812
1832
=== Description
1813
1833
@@ -1827,6 +1847,14 @@ This function accepts the components `millennium`, `century`, `decade`, `year`,
1827
1847
+
1828
1848
If an invalid part is specified, then `null` is returned.
1829
1849
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
+
1830
1858
=== Return Value
1831
1859
1832
1860
A date string representing the truncated date.
@@ -1838,7 +1866,8 @@ A date string representing the truncated date.
1838
1866
----
1839
1867
SELECT DATE_TRUNC_STR('2016-05-18T03:59:00Z', 'day') as day,
1840
1868
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;
1842
1871
----
1843
1872
1844
1873
.Results
@@ -1848,7 +1877,8 @@ SELECT DATE_TRUNC_STR('2016-05-18T03:59:00Z', 'day') as day,
1848
1877
{
1849
1878
"day": "2016-05-18T00:00:00Z",
1850
1879
"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"
1852
1882
}
1853
1883
]
1854
1884
----
@@ -2594,12 +2624,13 @@ AS Milliseconds;
2594
2624
====
2595
2625
2596
2626
[#fn-date-str-to-utc]
2597
-
== STR_TO_UTC(date1)
2627
+
== STR_TO_UTC(date1 [, [input-fmt,] fmt])
2598
2628
2599
2629
=== Description
2600
2630
2601
2631
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.
2603
2634
2604
2635
=== Arguments
2605
2636
@@ -2609,6 +2640,20 @@ This is the date to convert to UTC.
2609
2640
+
2610
2641
If this argument is not a valid date format, then `null` is returned.
2611
2642
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
+
2612
2657
=== Return Value
2613
2658
2614
2659
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.
2619
2664
[source,sqlpp]
2620
2665
----
2621
2666
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;
2623
2669
----
2624
2670
2625
2671
.Results
@@ -2628,19 +2674,21 @@ STR_TO_UTC('1111-11-11') as short_date;
2628
2674
[
2629
2675
{
2630
2676
"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"
2632
2679
}
2633
2680
]
2634
2681
----
2635
2682
====
2636
2683
2637
2684
[#fn-date-str-to-tz]
2638
-
== STR_TO_TZ(date1, tz)
2685
+
== STR_TO_TZ(date1, tz [, [input-fmt,] fmt])
2639
2686
2640
2687
=== Description
2641
2688
2642
2689
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.
2644
2692
2645
2693
=== Arguments
2646
2694
@@ -2655,28 +2703,48 @@ A string, or any valid xref:n1ql-language-reference/index.adoc[expression] which
2655
2703
+
2656
2704
If this argument is not a valid timezone, then `null` is returned.
2657
2705
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
+
2658
2720
=== Return Value
2659
2721
2660
2722
A single date string representing the date string converted to the specified timezone.
2661
2723
2724
+
[[ex-str-to-tz]]
2662
2725
=== Examples
2663
2726
2664
2727
====
2665
2728
[source,sqlpp]
2666
2729
----
2667
2730
SELECT STR_TO_TZ('1111-11-11T00:00:00+08:00', 'America/New_York') as est,
2668
2731
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,
0 commit comments