Skip to content

Commit

Permalink
DATEDIFF: month, mm, m.
Browse files Browse the repository at this point in the history
  • Loading branch information
markkurossi committed Aug 20, 2023
1 parent 23f8ef2 commit 2a7b635
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lang/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 13 additions & 1 deletion lang/builtin_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2020-2021 Markku Rossi
// Copyright (c) 2020-2023 Markku Rossi
//
// All rights reserved.
//
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 2a7b635

Please sign in to comment.