I’m working on a feature in my application where I need to display the total number of people whose birthdays fall in the current month, and I could use some guidance on the best way to set up the logic.
- I have a database table of users that includes a Date of Birth column. The column type is currently set to
datetime
.
- What I need is a query or logic block that:
- Checks the month of each user’s birthdate.
- Compares it to the current month.
- Counts how many records match.
- The goal is to display the total number of users with a birthday this month in a text block on the UI.
Question:
Should I convert the datetime to a string and compare substrings, or is there a function to get just the month from a date?
Thanks in advance!
Hi Brian,
Great question! Backendless provides flexible ways to handle this scenario directly in the database query, without needing to convert dates to strings or handle substrings in your app logic.
Recommended Approach: Use SQL Functions in Your Where Clause
In Backendless, you can use SQL-based functions for querying your data. Since your Date of Birth
column is of type datetime
, you can leverage the MONTH() function in your query.
Example Where Clause:
MONTH(DateOfBirth) = MONTH(NOW())
This condition will match all users whose birthday month is the same as the current month.
Complete API Usage:
- Data Query:
- Table:
Users
(or whatever your user table is named)
- Where Clause:
MONTH(DateOfBirth) = MONTH(NOW())
- Count Query: You can use
find with count
to get just the count without fetching all data.
Example In Codeless (Logic Blocks):
- Use the Data → Get Object Count block.
- Set the where clause to:
MONTH(DateOfBirth) = MONTH(NOW())
- The result will be the total number of users with a birthday in the current month.
Example In REST API:
GET https://api.backendless.com/{APP_ID}/{REST_API_KEY}/data/Users/count?where=MONTH(DateOfBirth)%20=%20MONTH(NOW())
In summary:
You do NOT need to convert dates to strings or parse them manually—just use the MONTH()
function in your query where clause as shown above. This is efficient and works both in Codeless and via API calls.
Regards,
Mark
p.s. This response was generated with the help of FlowRunner AI
Thank you very much for the explanation! the Codeless method is working as intended.
A related question:
I’m trying to configure a data table to filter a table to view the users that have birthdays within the current month. I attempted to insert the example where clause that was provided
MONTH(DateOfBirth) = MONTH(NOW())
but it didn’t work. I suspect the syntax or filtering logic in the data table component doesn’t support SQL functions or there needs to be a way to insert a ‘get property of object’ before the SQL function mentioned above?
Hello @floward-b
I created a Person
table, to which I added 3 records, two of which are DateOfBirth
from this month:
What exactly do you mean?
- REST
- Data Browser
- Data Table in UI Builder
or something else?
Please show us what you are doing and what result you find unexpected.
Regards,
Volodymyr