# Insert the date and time separately into an SQL query

**URL:** <https://discourse.nodered.org/t/insert-the-date-and-time-separately-into-an-sql-query/97615>\
**Category:** General\
**Tags:** database, mysql\
**Created:** [13 June 2025 20:50 UTC](https://discourse.nodered.org/t/insert-the-date-and-time-separately-into-an-sql-query/97615 "2025-06-13T20:50:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![geronimo-78](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/geronimo-78/32/101435_2.png) [@geronimo-78](https://discourse.nodered.org/u/geronimo-78)\
**Post date:** [13 June 2025 20:50 UTC](https://discourse.nodered.org/t/insert-the-date-and-time-separately-into-an-sql-query/97615/1 "2025-06-13T20:50:05Z")

</div>

Hello,  
Excuse me for this naive question, but I'm a beginner.  
Here's my problem: I have a sensor that sends me speed information when an object passes in front of it. I want to record the date, time (in different columns), and speed in a MySQL database. Currently, I record the speed and the date and time with curtime (but in the same column).  
My question: how can I have two variables with the date and time to insert them into my SQL query?  
Thanks for your help.  
Jean

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [13 June 2025 20:59 UTC](https://discourse.nodered.org/t/insert-the-date-and-time-separately-into-an-sql-query/97615/2 "2025-06-13T20:59:52Z")

</div>

You can route the speed info through a function node, and then use standard JavaScript to get the current date & time.  
for example:

```auto
const now = new Date();
const currentDate = now.toLocaleDateString(); // Gets the current date
const currentTime = now.toLocaleTimeString(); // Gets the current time

```

Alternatively, you can use a change node with JSonata expressions:

```auto
$fromMillis($millis(), "[Y0001]-[M01]-[D01]")
$fromMillis($millis(), "[H01]:[m01]:[s01]")

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [13 June 2025 22:23 UTC](https://discourse.nodered.org/t/insert-the-date-and-time-separately-into-an-sql-query/97615/3 "2025-06-13T22:23:16Z")

</div>

And don't forget you can store date and time as you do now but extract date and time seperately in a SELECT statement.  
In my database, the timestamp field has a default value of CURRENT\_TIMESTAMP

```auto
MariaDB [espresso]> SELECT timestamp, DATE(timestamp) as date, TIME(timestamp) as time FROM test;
+---------------------+------------+----------+
| timestamp | date | time |
+---------------------+------------+----------+
| 2024-12-27 17:15:00 | 2024-12-27 | 17:15:00 |
| 2024-12-28 05:45:00 | 2024-12-28 | 05:45:00 |
| 2024-12-28 18:15:01 | 2024-12-28 | 18:15:01 |
| 2024-12-29 00:30:01 | 2024-12-29 | 00:30:01 |

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [14 June 2025 10:44 UTC](https://discourse.nodered.org/t/insert-the-date-and-time-separately-into-an-sql-query/97615/4 "2025-06-14T10:44:05Z")

</div>

> [@geronimo-78](#):
>
> sends me speed information when an object passes in front of it

I'd be interested in what sensor you are using for that?

---

<div class="post-metadata">

**Author:** ![geronimo-78](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/geronimo-78/32/101435_2.png) [@geronimo-78](https://discourse.nodered.org/u/geronimo-78)\
**Post date:** [14 June 2025 20:21 UTC](https://discourse.nodered.org/t/insert-the-date-and-time-separately-into-an-sql-query/97615/5 "2025-06-14T20:21:50Z")

</div>

> [@jbudd](#):
>
> `SELECT timestamp, DATE(timestamp) as date, TIME(timestamp) as time FROM test`

Thanks, so no need to memorize date and time separately.

---

<div class="post-metadata">

**Author:** ![geronimo-78](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/geronimo-78/32/101435_2.png) [@geronimo-78](https://discourse.nodered.org/u/geronimo-78)\
**Post date:** [14 June 2025 20:25 UTC](https://discourse.nodered.org/t/insert-the-date-and-time-separately-into-an-sql-query/97615/6 "2025-06-14T20:25:05Z")

</div>

It's an Omnipresense OPS243

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [13 August 2025 20:26 UTC](https://discourse.nodered.org/t/insert-the-date-and-time-separately-into-an-sql-query/97615/7 "2025-08-13T20:26:03Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
