diff --git a/README.md b/README.md index d3d20a4..bbab1d4 100644 --- a/README.md +++ b/README.md @@ -469,6 +469,7 @@ from 'ansi.json' FILTER 'colors' AS src; *from* and *to*. The *diff* specifies the units in which the difference is computed: - `year`, `yy`, `yyyy`: difference between date year parts + - `month`, `mm`, `m`: difference in months - `day`, `dd`, `d`: difference in calendar days - `hour`, `hh`: difference in hours - `minute`, `mi`, `n`: difference in minutes diff --git a/lang/builtin.go b/lang/builtin.go index a826502..6e05aca 100644 --- a/lang/builtin.go +++ b/lang/builtin.go @@ -1121,7 +1121,8 @@ func builtInDateDiff(args []Expr, row *Row, rows []*Row) (types.Value, error) { // XXX quarter, qq, q case "month", "mm", "m": - return types.IntValue(to.Month() - from.Month()), nil + return types.IntValue((int64(to.Year())*12 + int64(to.Month())) - + (int64(from.Year())*12 + int64(from.Month()))), nil // XXX dayofyear, dy, y diff --git a/lang/builtin_test.go b/lang/builtin_test.go index 324d687..c715327 100644 --- a/lang/builtin_test.go +++ b/lang/builtin_test.go @@ -1,5 +1,5 @@ // -// Copyright (c) 2020-2021 Markku Rossi +// Copyright (c) 2020-2023 Markku Rossi // // All rights reserved. // @@ -446,6 +446,18 @@ SELECT UNICODE(nstring), NCHAR(UNICODE(nstring));`, '2006-01-01 00:00:00.0000000');`, v: [][]string{{"1"}}, }, + { + q: `SELECT DATEDIFF(month, + '2005-12-31 23:59:59.9999999', + '2006-01-01 00:00:00.0000000');`, + v: [][]string{{"1"}}, + }, + { + q: `SELECT DATEDIFF(month, + '2005-01-01 00:00:00.0000000', + '2006-02-01 00:00:00.0000000');`, + v: [][]string{{"13"}}, + }, { q: `SELECT DATEDIFF(day, '2005-12-31 23:59:59.9999999',