How do you use dates in NLTOSQL (Natural Language To SQL)?
· Toni Martir, arquitecto de software
In NLTOSQL (Natural Language To SQL) you type a question and the AI translates it into an SQL statement, the technical instruction that pulls the data out of the database. A typical question: "give me a list of sales by month for the last 3 months". Asked on 22 November, the list runs from September to November; and if the statement is saved, in December it should run from October to December.
Whether the AI gets this right depends on the instructions the program sends it with your question, what the trade calls the prompt. They need two basic things: today's date, and an order to use relative dates when the question gives no specific date.
"Sales by month for the last 3 months": what you get depending on the instructions
The question is asked on 22 November 2026, counting the current month, and the same statement is run again in December.
Today's date goes in the instructions
An AI model learns from text gathered up to its cutoff date. For Claude Sonnet 5.5 that is June 2026, according to Anthropic's table of models, and Google's Gemma 4, which you can install on your own computer, learned from data up to January 2025, according to its model card. Today's date is not something it learns.
Even the Claude app keeps a placeholder for the date and time in its opening instructions. An NLTOSQL program has to put the date in its own instructions, as Anthropic recommends in its guide to Claude Haiku 5.5 for when the model searches for recent information: a line saying "The current date is…".
Relative dates when the question gives none
With the date, the AI knows which months are the last three. But if nobody tells it how to write them, it may type them in: "from 1 September 2026". Today the list comes out right; saved and run again in December, it still covers September to November, and nothing warns you. And the figure you check every month is best pulled with the same saved statement, as I explained in why the AI gives you a different figure.
So the instructions have to ask for relative dates: the statement should work
out the months from the date the database supplies when it runs. PostgreSQL
supplies it with
CURRENT_DATE,
a standard SQL function, and SQL Server with
GETDATE(),
which takes it from the computer it is installed on. "The last 3 months" becomes
"from the 1st of the month two months ago", and the same statement still works
next month.
The order applies when the user doesn't give a specific date. If they ask for the sales of October 2025, a fixed date is the right answer.
How to tell whether the dates are relative
All it takes is for someone to explain the statement in plain words without
knowing your question, as I described in
how do you know the AI figure is right.
If the explanation says "from 1 September 2026", the date is absolute; if it
says "from the 1st of the month two months ago", it is relative. In the
statement, a date in quotes, like '2026-09-01', is absolute. It is
worth checking before you save the statement.
So how do you use dates in NLTOSQL?
With two things in the instructions the AI receives: today's date, and an order to use relative dates when the question gives no specific date, so the database works out the months when it runs the statement. That way the one for "the last 3 months" is right today and next month. And before you save it, you check that its dates are relative.
At Reportman AI, the SQL statement the AI writes is in plain view and can be reviewed, changed and run. It also goes through a blind audit, optional and on by default: a separate call to the AI that, without seeing your question, explains in plain words what the statement does and confirms whether its dates are relative or absolute.
Sources
- Anthropic, Models overview: in the table, row "Reliable knowledge cutoff", column Claude Sonnet 5.5: "Jun 2026", which the page defines as "The date through which the model’s knowledge is most extensive and reliable".
- Google, Gemma 4 31B (model card), section "Training Dataset": "Our pre-training dataset is a large-scale, diverse collection of data […] with a cutoff date of January 2025"; and the models are "deployable in environments ranging from high-end phones to laptops and servers".
- Anthropic, System prompts: Claude's web interface and apps "use a system prompt at the start of every conversation". In the one for Claude Sonnet 5.5: "It answers the way a highly informed individual in Jun 2026 would if talking to someone from {{currentDateTime}}".
- Anthropic, Prompting Claude Haiku 5.5: "When you give Claude Haiku 5.5 a search tool, also give it today’s date. […] You can put the date in the system prompt or in the search tool’s description", with the example "The current date is {{current_date}}."
- PostgreSQL, Date/Time Functions and Operators,
section 9.9.5 "Current Date/Time": "These SQL-standard functions all return values based on
the start time of the current transaction: CURRENT_DATE…". With
date_truncand subtracting an interval (table 9.32, "timestamp - interval → timestamp"), the last 3 months start atdate_trunc('month', CURRENT_DATE) - interval '2 months'. - Microsoft, GETDATE (Transact-SQL):
"Returns the current database system timestamp […]. This value is derived from the operating
system of the computer on which the instance of SQL Server is running." With
DATETRUNC (from
SQL Server 2022 on) and DATEADD,
the last 3 months start at
DATEADD(month, -2, DATETRUNC(month, GETDATE())). - Reportman AI: home page (the Normal Response, with the blind audit, optional and on by default, which "without seeing your question, explains in plain words what the SQL statement does") and developers ("Every query generated by the AI can be reviewed, modified, and audited by an expert before execution").
Toni Martir, arquitecto de software · · sources checked on