日付関数
関数
次に、SAQL 日付関数のリストを示します。
date(year, month, day)
1date('OrderDate_Year', 'OrderDate_Month', 'OrderDate_Day')date_diff(datepart,startdate,enddate)
- year
- month
- quarter
- day
- week
- hour
- minute
- second
enddate は終了日を示します。
2 つの日付の差は、指定された日付要素の違いに基づいて計算されます。たとえば、2 つの日付の年の差は、enddate の年要素から startdate の年要素が差し引かれて計算されます。date_diff("year", toDate("31-12-2015", "dd-MM-yyyy"), toDate("1-1-2016", "dd-MM-yyyy")) の結果は 1 になります。
同じように、日付要素の月を使用した例を次に示します。date_diff("month", toDate("31-12-2015", "dd-MM-yyyy"), toDate("1-1-2016", "dd-MM-yyyy")) の結果も 1 です。
startdate が enddate より後である場合、結果は差を表す、負の整数になります。| クエリ | 結果 |
| date_diff(“year", '2004-02-29', '2005-02-28’) | 1 |
| date_diff(“year", '2012/01/01', '2012/12/31’) | 0 |
| date_diff(“month", '2003-02-01', '2003-05-01’) | 3 |
| date_diff(“month", '2004/02/28', '2004/03/31’) | 1 |
| date_diff(“quarter", '2012-12-12', '2013-01-05') | 1 |
| date_diff(“week", '2012-12-12', '2013-01-05') | 3 |
| date_diff(“day", '2012-12-12', '2013-01-05') | 24 |
| date_diff(“hour", '2012-12-12', '2013-01-05') | 576 |
| date_diff(“minute", '2012-12-12', '2013-01-05') | 34560 |
| date_diff(“second", '2016-09-15 19:42:36', '2016-09-16 19:42:36') | 86400 |
クエリの例:
1q = load \"em/dates\";
2q = foreach q generate date_diff("year", toDate(DateOfBirth, "yyyy-MM-dd"), now()) as age;
3q = order q by age asc;無効な例:
1q = group q by date_diff("month", toDate(DateOfBirth, "yyyy-MM-dd"),
2 toDate(RegisteredDate));1q = order q by date_diff("year", toDate(DateOfBirth, "yyyy-MM-dd"),
2 toDate(RegisteredDate));1q = filter q by date_diff("day", toDate(DateOfBirth, "yyyy-MM-dd"), now());date_to_epoch(date)
1970 年 1 月 1 日 00:00:00.000 GMT からの秒数を返します。これより前の日付が渡された場合、結果は負の数値になります。パラメータが日付でない場合、結果はエラーになります。パラメータとして null が渡された場合、null が返されます。
例:
1date_to_epoch(now()) == 1496404452 (current time)1date_to_epoch(toDate("2017-06-02 11:54:12")) == 1496404452date_to_string(date, formatString)
1q = foreach q generate date_to_string(now(), \"yyyy-MM-dd HH:mm:ss\") as ds1;dateRange(startArray_y_m_d, endArray_y_m_d)
1dateRange([1970, 1, 1], [1970, 1, 31])day_in_month(date)
day_in_quarter(date)
day_in_week(date)
date は参照日を示します。
1q = foreach q generate day_in_week(toDate(OrderDate));day_in_year(date)
daysBetween(date1, date2)
1q = foreach q generate daysBetween(toDate(OrderDate, “yyyy-MM-dd”),
2 now()) as daysToShip;1q = foreach q generate daysBetween(toDate(OrderDate, “yyyy-MM-dd”),
2 toDate(ShipDate, “yyyy-MM-dd”)) as daysToShip;1q = foreach q generate daysBetween(toDate(OrderDate_Year + “:”
2 + OrderDate_Month + “:” + OrderDate_Day, “yyyy:MM:dd”), toDate(ShipDate_Year + “:”
3 + ShipDate_Month + “:” + ShipDate_Day, “yyyy:MM:dd”)) as daysToShip;month_days(date)
date は参照日を示します。
| クエリ | 結果 |
| month_days(toDate('2004-02-12', "yyyy-MM-dd") | 29 |
| month_days(toDate('2012-04-07', "yyyy-MM-dd") | 30 |
| month_days(toDate('1990-13-11', "yyyy-MM-dd") | Null |
クエリの例:
1q = load \"em/dates\";
2q = foreach q generate month_days(toDate(BillDate, "yyyy-MM-dd")) as BillingMonth;
3q = order q by BillingMonth asc;無効な例:
1q = group q by month_days(toDate(BillDate, "yyyy-MM-dd"));1q = order q by month_days(toDate(BillDate, "yyyy-MM-dd"));1q = filter q by month_days(toDate(BillDate, "yyyy-MM-dd"));month_last_day(date)
now()
1q = foreach q generate now() as now;一般的に、この関数は daysBetween() および toString() 関数で使用されます。
quarter_days(date)
quarter_last_day(date)
toDate(string [,formatString])
1q = foreach q generate toDate(OrderDate);1q = foreach q generate toDate(OrderDate_Day + \"-\" + OrderDate_Month + \"-\" + OrderDate_Year, \"dd-MM-yyyy\");多くの場合、この関数は daysBetween() または toString() に引数として渡されます。
toDate(epoch_seconds)
この関数は、日付への期間の加算または日付からの期間の減算を行う場合に便利です。日付のタイムゾーンの違いを調整するときに時差を秒数で加算または減算すると、正しいローカル日付が算出されます。その時間がローカル子午線を越えている場合は、別の日付が算出されます。
たとえば、Current_Date が '1970-01-01 00:00:00' からの秒数で表される現在日付の場合、関数 toDate(Current_Date - 8*3600) は 8 時間を減算します。実例については、「タイムゾーンの使用」を参照してください。
toString(date, formatString)
1q = foreach q generate toString(now(), \"yyyy-MM-dd HH:mm:ss\") as ds1;week_last_day(date)
date は参照日を示します。
| クエリ | 結果 |
| week_last_day(toDate('2016-12-08', "yyyy-MM-dd")) | 2016-12-10 |
| week_last_day(toDate('2015-07-05', "yyyy-MM-dd")) | 2015-07-11 |
| week_last_day(toDate('2012-11-33', "yyyy-MM-dd")) | エラー |
クエリの例:
1q = load \"em/dates\";
2q = foreach q generate week_last_day(toDate(BillDate, "yyyy-MM-dd")) as BillingWeek;
3q = order q by BillingWeek asc;無効な例:
1q = group q by week_last_day(toDate(BillDate, "yyyy-MM-dd"));1q = order q by week_last_day(toDate(BillDate, "yyyy-MM-dd"));1q = filter q by week_last_day(toDate(BillDate, "yyyy-MM-dd"));year_days(date)
year_last_day(date)
固定日付範囲の指定
固定日付の範囲を指定するには、dateRange() 関数を使用します。日付を年、月、日の順序で指定します。
例
1a = filter a by date('year', 'month', 'day') in [dateRange([1970, 1, 1], [1970, 1, 11])];相対日付範囲の指定
相対日付範囲を指定するには、相対日付キーワードを含む配列で in 演算子を使用します。次に、4 つの例を示します。
1a = filter a by date('year', 'month', 'day') in ["1 year ago".."current year"];
2a = filter a by date('year', 'month', 'day') in ["2 quarters ago".."2 quarters ahead"];
3a = filter a by date('year', 'month', 'day') in ["4 months ago".."1 year ahead"];
4a = filter a by date('year', 'month', 'day') in ["2 fiscal_years ago".."current day"];- current day
- n day(s) ago
- n day(s) ahead
- current week
- n week(s) ago
- n week(s) ahead
- current month
- n month(s) ago
- n month(s) ahead
- current quarter
- n quarter(s) ago
- n quarter(s) ahead
- current fiscal_quarter
- n fiscal_quarter(s) ago
- n fiscal_quarter(s) ahead
- current year
- n year(s) ago
- n year(s) ahead
- current fiscal_year
- n fiscal_year(s) ago
- n fiscal_year(s) ahead
| 相対日付値キーワード | 開始日 | 終了日 |
|---|---|---|
| current day | 2014/12/16 00:00:00 | 2014/12/16 23:59:59 |
| current quarter | 2014/10/1 00:00:00 | 2014/12/31 23:59:59 |
| 1 year ago | 2013/1/1 00:00:00 | 2013/12/31 23:59:59 |
| 1 month ahead | 2015/1/1 00:00:00 | 2015/1/31 23:59:59 |
| current fiscal_year | 2014/2/1 00:00:00 | 2015/1/31 23:59:59 |
| current fiscal_quarter | 2014/11/1 00:00:00 | 2015/1/31 23:59:59 |
| 2 fiscal_quarters ahead | 2015/5/1 00:00:00 | 2015/7/31 23:59:59 |
| current day - 1 year | 2013/12/16 00:00:00 | 2013/12/16 23:59:59 |
| current fiscal_year + 5 days | 2014/2/6 00:00:00 | 2014/2/6 23:59:59 |
日付の加算と減算
相対日付キーワードを使用して、日付の加算と減算を実行できます。
例
次に、加算と減算を使用した相対日付キーワードの時間枠の例を示します。これらの時間枠の例では、現在の日付は 2014/12/16、FiscalMonthOffset 1 (会計年度は 2 月 1 日に開始) です。
1a= filter a by date('year', 'month', 'day') in ["current day - 1 year"..] ;1a= filter a by date('year', 'month', 'day') in ["current day".."2 years ahead + 3 months"];1a= filter a by date('year', 'month', 'day') in ["current fiscal_year + 5 days".."2 years ahead + 3 months"];無制限の相対日付範囲の使用
例
1a = filter a by date('year','month','day') in [.."current month"];例
1q = filter q by OrderDate in [“2015-01-01"..];タイムゾーンの使用
toDate() 関数の実用的な用途は、Analytics ダッシュボードでのタイムゾーンの変更を計算することです。次の JSON コードフラグメントでは、変換に computeExpression アクションを使用し、saqlExpression を使用して toDate() 関数をコールします。この方法により、ローカルまたは UTC の最も適切な日時をダッシュボードに表示できます。
1"Extract_Opportunity": {
2 "action": "computeExpression",
3 "parameters": {
4 "source": "Digest_Opportunity",
5 "mergeWithSource": true,
6 "computedFields": [
7 {
8 "name": "CreatedDateNew",
9 "type": "Date",
10 "format": "MM/dd/yyyy",
11 "saqlExpression": "toDate(CreatedDate_sec_epoch - 8*3600)"
12 }
13 ]
14 }
15},この例は、既存の日付 CreatedDate_sec_epoch を取得し、8 時間を減算して新しい日付 CreateDateNew を作成しています。次の表に、この計算によって (形式が適用された) CreatedDateNew 日付がどのように変更されるかを示します。いずれの場合も、時間の変更に伴い日付も変更されています。
| CreatedDate_sec_epoch | CreatedDateNew |
|---|---|
| 2015-11-03T06:49:25.00OZ | 11/2/2015 |
| 2014-08-19T06:42:33.00OZ | 8/18/2014 |
| 2014-09-28T03:12:25.00OZ | 9/27/2014 |
詳細は、「computeExpression 変換」 トピックを参照してください。