|
The datetime data types are DATE, TIME, and TIMESTAMP.
Although datetime values can be used in certain arithmetic and string operations and are compatible with certain strings, they are not strings or numbers.
Os tipos de dados datetime são DATE, TIME e TIMESTAMP.
Embora os valores de data e hora possam ser usados em certas operações aritméticas e de string e sejam compatíveis com certas strings, eles não são strings ou
números.
Following is the list of commonly used Date and Time Manipulation Functions:
A seguir está a lista de funções de manipulação de data e hora comumente usadas:
| Function |
Description |
Example |
| CHAR |
Returns a string representation of its first argument.
Retorna uma representação de string de seu primeiro argumento. |
CHAR(cust_hiredate,USA) |
| DAYS |
Returns an integer representation of its argument.
Retorna uma representação inteira de seu argumento. |
DAYS(‘2008-01-01’) |
| YEAR |
Returns the year part of its argument
Retorna a parte do ano de seu argumento |
YEAR(cust_hiredate) |
| MONTH |
Returns the month part of its argument
Retorna a parte do mês de seu argumento |
MONTH(cust_hiredate) |
| DAY |
Returns the day part of its argument
Retorna a parte do dia de seu argumento |
DAY(cust_hiredate) |
| HOUR |
Returns the hour part of its argument
Retorna a parte da hora de seu argumento |
HOUR(CURRENT TIME) |
| MINUTE |
Returns the minute part of its argument
Retorna a parte do minuto de seu argumento |
MINUTE(CURRENT TIME) |
| SECOND |
Returns the second part of its argument
Retorna a parte do segundo de seu argumento |
SECOND(CURRENT TIME) |
| MICROSECOND |
Returns the microsecond part of its argument
Retorna a parte em microssegundo de seu argumento |
MICROSECOND(CURRENT TIMESTAMP) |
| DATE |
Returns the date derived from its argument
Retorna a data derivada de seu argumento |
DATE(‘2008-01-01’) |
| TIME |
Returns the time derived from its argument
Retorna a hora derivada de seu argumento |
TIME(’13:00:00’) |
| TIMESTAMP |
Returns the timestamp derived from its argument
Retorna o carimbo de data/hora derivado de seu argumento |
TIMESTAMP(CURRENT DATE) |
Let use see examples below.
Vamos ver os exemplos abaixo.
Example 1 - Exemplo 1:
How to get current date, time and timestamp?
Como obter data, hora e carimbo de data/hora atuais?
SELECT current date FROM sysibm.sysdummy1;
SELECT current time FROM sysibm.sysdummy1;
SELECT current timestamp FROM sysibm.sysdummy1;
|
Example 2 - Exemplo 2:
How to get year, month, day, hour, minutes, seconds, and microseconds from current timestamp?
Como obter ano, mês, dia, hora, minutos, segundos e microssegundos do carimbo de data/hora atual?
SELECT YEAR (current timestamp) FROM sysibm.sysdummy1;
SELECT MONTH (current timestamp) FROM sysibm.sysdummy1;
SELECT DATE (current timestamp) FROM sysibm.sysdummy1;
SELECT TIME (current timestamp) FROM sysibm.sysdummy1;
SELECT DAY (current timestamp) FROM sysibm.sysdummy1;
SELECT HOUR (current timestamp) FROM sysibm.sysdummy1;
SELECT MINUTE (current timestamp) FROM sysibm.sysdummy1;
SELECT SECOND (current timestamp) FROM sysibm.sysdummy1;
SELECT MICROSECOND (current timestamp) FROM sysibm.sysdummy1;
|
Example 3 - Exemplo 3:
How to perform date and time calculations?
Como fazer cálculos de data e hora?
SELECT current date + 2 YEAR FROM sysibm.sysdummy1;
SELECT current date + 1 YEARS + 10 MONTHS FROM sysibm.sysdummy1;
SELECT current date + 1 YEARS + 5 MONTHS + 10 DAYS FROM sysibm.sysdummy1;
SELECT current time + 5 HOURS - 3 MINUTES + 10 SECONDS FROM sysibm.sysdummy1;
|
Example 4 - Exemplo 4:
How to find number of days between two dates, you can subtract dates as below:
Como encontrar o número de dias entre duas datas, você pode subtrair as datas conforme abaixo:
SELECT days (current date) - days (date('1990-09-02')) FROM sysibm.sysdummy1;
|
Example 5 - Exemplo 5:
If you want to concatenate date or time values with other text, you need to convert the value into a character string first.
To do this, you can simply use the CHAR() function:
Se você deseja concatenar valores de data ou hora com outro texto, primeiro é necessário converter o valor em uma string de caracteres.
Para fazer isso, você pode simplesmente usar a função CHAR ():
SELECT "Current Date -" || char(current date) FROM sysibm.sysdummy1;
SELECT "Current Time -" || char(current time) FROM sysibm.sysdummy1;
SELECT "Current Date with 12 hours -" char(current date + 10 hours) FROM sysibm.sysdummy1;
|
Example 6 - Exemplo 6:
If you want to convert a character string to a date or time value, you can use as below:
Se você deseja converter uma string de caracteres em um valor de data ou hora, você pode usar o seguinte:
SELECT TIMESTAMP ('1990-09-02-12.00.00.000000') FROM sysibm.sysdummy1;
SELECT TIMESTAMP ('1990-09-02 12:00:00') FROM sysibm.sysdummy1;
SELECT DATE ('1990-09-02') FROM sysibm.sysdummy1;
SELECT DATE ('02/09/1990') FROM sysibm.sysdummy1;
SELECT TIME ('10:00:00') FROM sysibm.sysdummy1;
SELECT TIME ('10.00.00') FROM sysibm.sysdummy1;
|
Example 7 - Exemplo 7:
How to change Date/Time Format ? Let us see formatting date in various formats.
Como alterar o formato de data / hora? Vamos ver a data de formatação em vários formatos.
SELECT CHAR(DATE (CURRENT TIMESTAMP),ISO) AS date_in_iso,
CHAR(DATE (CURRENT TIMESTAMP),USA) AS date_in_usa,
CHAR(DATE (CURRENT TIMESTAMP),EUR) AS date_in_eur,
CHAR(DATE (CURRENT TIMESTAMP),JIS) AS date_in_jis
FROM sysibm.sysdummy1;
|
Result - Resultado:
| Column_name |
Value |
| date_in_iso |
1990-09-02 |
| date_in_usa |
1990/09/02 |
| date_in_eur |
1990.09.02 |
| date_in_jis |
1990.09.02 |
Example 8 - Exemplo 8:
Let us see how to convert date format using VARCHAR_FORMAT function.
Vamos ver como converter o formato de data usando a função VARCHAR_FORMAT.
SELECT
VARCHAR_FORMAT(CURRENT TIMESTAMP,'YYYY-MM-DD') AS VCHAR_FORMATED,
DATE(CURRENT TIMESTAMP) AS DATE_VALUE
FROM sysibm.sysdummy1;
|
Result - Resultado:
VCHAR_FORMATED DATE_VALUE
2013-04-11 04/11/2013
|
Example 9 - Exemplo 9:
The following SQL statement shows how to use VARCHAR_FORMAT function in where clause.
A seguinte instrução SQL mostra como usar a função VARCHAR_FORMAT na cláusula where.
SELECT *
FROM Employee_table
WHERE VARCHAR_FORMAT (date_col,'YYYY-MM-DD') = '1990-09-02'
|
Other important Date and Time functions are as follows:
Outras funções importantes de data e hora são as seguintes:
| DAYNAME |
Returns a mixed case character string containing the name of the day (e.g., Friday) for the day portion of the argument.
Retorna uma string de caracteres com maiúsculas e minúsculas contendo o nome do dia (por exemplo, sexta-feira) para a parte do dia do
argumento. |
| DAYOFWEEK |
Returns the day of the week in the argument as an integer value in the range 1-7, where 1 represents Sunday.
Retorna o dia da semana no argumento como um valor inteiro no intervalo de 1 a 7, onde 1 representa o domingo. |
| DAYOFWEEK_ISO |
Returns the day of the week in the argument as an integer value in the range 1-7, where 1 represents Monday.
Retorna o dia da semana no argumento como um valor inteiro no intervalo de 1 a 7, onde 1 representa segunda-feira. |
| DAYOFYEAR |
Returns the day of the year in the argument as an integer value in the range 1-366.
Retorna o dia do ano no argumento como um valor inteiro no intervalo 1-366. |
| JULIAN_DAY |
Returns an integer value representing the number of days from January 1, 4712 B.C. (the start of Julian date calendar) to the date value specified
in the argument.
Retorna um valor inteiro que representa o número de dias de 1º de janeiro de 4712 AC (o início do calendário juliano) até o valor de data
especificado no argumento. |
| MIDNIGHT_SECONDS |
Returns an integer value in the range 0 to 86 400 representing the number of seconds between midnight and the time value specified in the argument.
Retorna um valor inteiro no intervalo de 0 a 86.400 representando o número de segundos entre a meia-noite e o valor de hora especificado
no argumento. |
| MONTHNAME |
Returns a mixed case character string containing the name of month (e.g., January) for the month portion of the argument.
Retorna uma string de caracteres com letras maiúsculas e minúsculas contendo o nome do mês (por exemplo, janeiro) para a parte do mês do
argumento. |
| TIMESTAMP_ISO |
Returns a timestamp value based on date, time or timestamp argument.
retorna um valor de carimbo de data / hora com base na data, hora ou argumento de carimbo de data / hora. |
| TIMESTAMP_FORMAT |
Returns a timestamp from a character string that has been interpreted using a character template.
Retorna um carimbo de data / hora de uma sequência de caracteres que foi interpretada usando um modelo de caractere. |
| TIMESTAMPDIFF |
Returns an estimated number of intervals of the type defined by the first argument, based on the difference between two timestamps.
Retorna um número estimado de intervalos do tipo definido pelo primeiro argumento, com base na diferença entre dois carimbos de data/hora. |
| TO_CHAR |
Returns a character representation of a timestamp that has been formatted using a character template. TO_CHAR is a synonym for VARCHAR_FORMAT.
Retorna uma representação de caractere de um carimbo de data / hora que foi formatado usando um modelo de caractere. TO_CHAR é sinônimo
de VARCHAR_FORMAT. |
| TO_DATE |
Returns a timestamp from a character string that has been inter-preted using a character template.
TO_DATE is a synonym for TIMESTAMP_FORMAT.
Retorna um carimbo de data / hora de uma cadeia de caracteres que foi interpretada usando um modelo de caractere.
TO_DATE é sinônimo de TIMESTAMP_FORMAT. |
| WEEK |
Returns the week of the year of the argument as an integer value in range 1-54. The week starts with Sunday.
Retorna a semana do ano do argumento como um valor inteiro no intervalo de 1 a 54. A semana começa no domingo. |
| WEEK_ISO |
Returns the week of the year of the argument as an integer value in the range 1-53.
Retorna a semana do ano do argumento como um valor inteiro no intervalo de 1 a 53. |
|