Reportman.AI Logo Reportman.AI

← All articles

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

Without today's date The AI doesn't know which month it is: it puts in the months it assumes
today: whatever it assumes in December: the same
With the date, no request for relative dates It may write "from 1 September 2026"
today: Sep to Nov in December: Sep to Nov
With the date and a request for relative dates "From the 1st of the month two months ago", worked out by the database
today: Sep to Nov in December: Oct to Dec

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

Toni Martir, arquitecto de software · · sources checked on