In MariaDB, TIMESTAMP() è una funzione di data e ora incorporata che restituisce un valore datetime, basato sui suoi argomenti.
Può essere utilizzato con uno o due argomenti, come segue:
- Quando viene utilizzato con un argomento, restituisce quell'espressione di data o ora come valore di data e ora.
- Se utilizzato con due argomenti, aggiunge il secondo argomento (ora) alla prima espressione (data o data/ora), quindi restituisce il valore data/ora risultante.
Sintassi
Può essere utilizzato nei seguenti due modi:
TIMESTAMP(expr)
TIMESTAMP(expr1,expr2)
Dove expr1 è un'espressione di data o data/ora e expr2 è un'espressione temporale da aggiungere a expr1 .
Esempio
Ecco un esempio da dimostrare:
SELECT TIMESTAMP('2030-02-01'); Risultato:
+-------------------------+
| TIMESTAMP('2030-02-01') |
+-------------------------+
| 2030-02-01 00:00:00 |
+-------------------------+ Valori di data e ora
Ecco un esempio di passaggio di un valore datetime:
SELECT TIMESTAMP('2030-02-01 10:30:45'); Risultato:
+----------------------------------+
| TIMESTAMP('2030-02-01 10:30:45') |
+----------------------------------+
| 2030-02-01 10:30:45 |
+----------------------------------+ Secondo argomento
Ecco un esempio che aggiunge il secondo argomento al primo:
SELECT TIMESTAMP('2030-02-01 10:30:45', '02:15:15'); Risultato:
+----------------------------------------------+
| TIMESTAMP('2030-02-01 10:30:45', '02:15:15') |
+----------------------------------------------+
| 2030-02-01 12:46:00 |
+----------------------------------------------+ Microsecondi
Ecco un esempio che aggiunge microsecondi:
SELECT TIMESTAMP('2030-02-01 10:30:45', '00:00:00.123456'); Risultato:
+-----------------------------------------------------+
| TIMESTAMP('2030-02-01 10:30:45', '00:00:00.123456') |
+-----------------------------------------------------+
| 2030-02-01 10:30:45.123456 |
+-----------------------------------------------------+ Tempi negativi
I tempi negativi sono validi:
Esempio:
SELECT TIMESTAMP('2030-02-01 10:30:45', '-09:20:00'); Risultato:
+-----------------------------------------------+
| TIMESTAMP('2030-02-01 10:30:45', '-09:20:00') |
+-----------------------------------------------+
| 2030-02-01 01:10:45 |
+-----------------------------------------------+ Data attuale
Possiamo passare NOW() come argomento datetime per utilizzare la data e l'ora correnti:
SELECT
NOW(),
TIMESTAMP(NOW(), '10:30:45'); Risultato:
+---------------------+------------------------------+ | NOW() | TIMESTAMP(NOW(), '10:30:45') | +---------------------+------------------------------+ | 2021-05-28 09:25:09 | 2021-05-28 19:55:54 | +---------------------+------------------------------+
Argomenti non validi
Quando viene passato un argomento non valido, TIMESTAMP() restituisce null con un avviso:
SELECT TIMESTAMP('Ten Thirty AM'); Risultato:
+----------------------------+
| TIMESTAMP('Ten Thirty AM') |
+----------------------------+
| NULL |
+----------------------------+
1 row in set, 1 warning (0.004 sec) Controlla l'avviso:
SHOW WARNINGS; Risultato:
+---------+------+---------------------------------------+ | Level | Code | Message | +---------+------+---------------------------------------+ | Warning | 1292 | Incorrect time value: 'Ten Thirty AM' | +---------+------+---------------------------------------+
Argomento mancante
Chiamando TIMESTAMP() con il numero errato di argomenti, o senza passare alcun argomento, genera un errore:
SELECT TIMESTAMP(); Risultato:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1
E un altro esempio:
SELECT TIMESTAMP('2020-12-09', '06:30:45', '06:30:45'); Risultato:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' '06:30:45')' at line 1