Skip to content

Use age_in_days for age calculations#1119

Merged
labkey-martyp merged 3 commits intorelease26.3-SNAPSHOTfrom
26.3_fb_tnprc_migration
Apr 28, 2026
Merged

Use age_in_days for age calculations#1119
labkey-martyp merged 3 commits intorelease26.3-SNAPSHOTfrom
26.3_fb_tnprc_migration

Conversation

@labkey-martyp
Copy link
Copy Markdown
Contributor

@labkey-martyp labkey-martyp commented Apr 22, 2026

Rationale

The new age_in_days is consistent across postgres and sql server and more in-line with age as measuring calendar days. A few fixes and cleanups associated with age calculations as well.

Related Pull Requests

Changes

  • Use age_in_days in calculated age at time columns
  • Use age_in_days in demographicsAge
  • Consistently check lastDayAtCenter
  • Fix ageClassAtTime which was showing current age class

Copy link
Copy Markdown
Contributor

@labkey-bpatel labkey-bpatel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Please review my comment.

" CAST(CASE WHEN x.birth IS NULL OR x.effDate IS NULL THEN NULL\n" +
" ELSE age_in_days(x.birth, x.effDate) END AS INTEGER) AS AgeAtTimeDays,\n" +
" CAST(CASE WHEN x.birth IS NULL OR x.effDate IS NULL THEN NULL\n" +
" ELSE CONVERT(age_in_months(x.birth, x.effDate), INTEGER) END AS INTEGER) AS AgeAtTimeMonths,\n" +
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AgeAtTimeYearsRounded, AgeAtTimeDays, and AgeAtTimeMonths - these were previously cast as float (even though it produced whole number values first), but now they are integer -- is this intentional?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverted these back to float

Comment thread ehr/src/org/labkey/ehr/table/DefaultEHRCustomizer.java
labkey-martyp added a commit to LabKey/platform that referenced this pull request Apr 28, 2026
#### Rationale
1. age_in_days / age(..., SQL_TSI_DAY) — Preserves SQL Server's
calendar-boundary semantics for timestampdiff(SQL_TSI_DAY, ...) on
Postgres, where the native operator counts 24-hour intervals instead.
Consistent with the existing age_in_months/age_in_years functions;
permanent addition to LabKey SQL.
2. CAST(... AS BOOLEAN) in SELECT lists (QuerySelect.java) — On SQL
Server, boolean QMethodCalls in a select list are wrapped in CASE WHEN
(...) THEN 1 ELSE 0 END to avoid predicates as scalar values. But CAST(x
AS BIT) is already a value, not a predicate, and SQL Server rejects the
wrapped form. Fix excludes ConvertInfo from the wrap. (repro: insert
failing query / error)
3. DatabaseMigrationService.registerSchemaContributor — Interface for
DatabaseMigrationService.registerSchemaContributor in premiumModules

#### Related Pull Requests
- LabKey/premiumModules#499
- LabKey/tnprcEHRModules#277
- LabKey/ehrModules#1119

#### Changes
- Method.java — age_in_days method, age(..., SQL_TSI_DAY) dispatch,
updated validation
- QueryTestCase.jsp — tests for both new syntaxes including datetime
calendar-boundary cases
- LabKeySql.md — documents age_in_days
- QuerySelect.java — skip boolean wrap for CAST/CONVERT on SQL Server
- DatabaseMigrationService.java — new registerSchemaContributor
interface method

---------

Co-authored-by: Adam Rauch <adam@labkey.com>
@labkey-martyp labkey-martyp merged commit 2d62f2b into release26.3-SNAPSHOT Apr 28, 2026
9 of 11 checks passed
@labkey-martyp labkey-martyp deleted the 26.3_fb_tnprc_migration branch April 28, 2026 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants