Return only the time in Select

Asked

Viewed 124 times

2

How do I return only the time in Select?

I declared the time as TIMESTAMP, in this case returned the date and time, but I want to return only the time. So I declared only DATE and now the time does not come, only the date comes.

the code is like this:

INSERT INTO locacao
  (loc_codigo
  ,cli_codigo
  ,vei_placa
  ,datalocacao
  ,horalocacao
  ,datadevolucao
  ,horadevolucao
  ,qtddias
  ,valordia
  ,finalizado)
VALUES
  (01
  ,1
  ,'ABC1234'
  ,(TO_DATE('01/02/2016','DD/MM/YYYY'))
  ,TO_DATE('08:30','HH24:MI:SS')
  ,TO_DATE('06/02/2016','DD/MM/YYYY')
  ,TO_DATE('07:00','HH24:MI:SS')
  ,5
  ,100.00
  ,1);
  • Elaine, you can post the statement of the table?

  • ... Date location DATE NOT NULL, Time location DATE NOT NULL, Dated DATE, Horadevolucao DATE, Qtddias INTEGER , Value NUMBER(8,2) NOT NULL, Finished NUMBER(3) DEFAULT(0) NOT NULL);

3 answers

1

Thus:

SELECT TO_CHAR(datahora,'HH24') AS HORA FROM tabela
  • I posted the code, like this one. When I put SELECT * FROM 'TABLE' The time data does not come, in place comes a date.

  • To run the field has to be of the TIMESTAMP type. You cannot select the time of a field that does not save the time.

  • great, good tip. Thank you very much Reginaldo.

1

Elaine, I don’t think you need to create the horalocacao and horadevolucao, can do as follows:

Based on the data you passed, the table creation would look like this:

CREATE TABLE LOCACAO (loc_codigo number(11)
  ,cli_codigo number(11)   NOT NULL PRIMARY KEY
  ,vei_placa varchar(7)  NOT NULL
  ,datalocacao date  NOT NULL
  ,datadevolucao date  NOT NULL
  ,qtddias integer(11)
  ,valordia number(8,2)  NOT NULL
  ,finalizado number(3) DEFAULT(0) NOT NULL
  );

The Inclusion:

INSERT INTO locacao
  (loc_codigo
  ,cli_codigo
  ,vei_placa
  ,datalocacao
  ,datadevolucao
  ,qtddias
  ,valordia
  ,finalizado)
VALUES
  (01
  ,1
  ,'ABC1234'
  ,TO_DATE('01/02/2016 08:30:00','DD/MM/YYYY HH24:MI:SS')
  ,TO_DATE('06/02/2016 07:00:00','DD/MM/YYYY HH24:MI:SS')
  ,5
  ,100.00
  ,1);

And the query, in case you wanted the fields of horalocacao and horadevolucao converted to hours, would be like this:

SELECT loc_codigo
      ,cli_codigo
      ,vei_placa
      ,TO_CHAR(datalocacao,'DD/MM/RRRR') datalocacao
      ,TO_CHAR(datalocacao,'HH24:MI:SS') horalocacao
      ,TO_CHAR(datadevolucao,'DD/MM/RRRR') datadevolucao
      ,TO_CHAR(datadevolucao,'HH24:MI:SS') horadevolucao
      ,qtddias
      ,valordia
      ,finalizado
  FROM locacao

This solution would be better, because the DATE saves date and time values, so you don’t need to create the horalocacao and horadevolucao, and if wanted to display only the time or date makes through the consultation.

  • great. Thank you so much! I’ll do it like this!

  • Elaine, if you answer, mark next (symbol of right), as you accept the answer. :)

  • great, I did just like that. Just changing some types. And for the SELECT I wanted to use tb this that you indicated, but also used this. SELECT LOC_CODIGO,CLI_Codigo, VEI_Placa, TO_CHAR(DataLocacao, 'DD/MM/YYYY') as DATALOC, TO_CHAR(DataLocacao, 'HH24:MI:SS') AS HORALOC, TO_CHAR(DataDevolucao, 'DD/MM/YYYY')
AS DATADEV, TO_CHAR(DataDevolucao, 'HH24:MI:SS') AS HORDEV, QtdDias, ValorDia, Finalizado FROM LOCACAO;

  • Any other questions Elaine, just ask a question we’ll help you.

0

Try this:

SELECT 
  TO_CHAR(SYSDATE, 'HH24') Hora,
  TO_CHAR(SYSDATE, 'HH24:MM:SS') Completo
FROM dual;

inserir a descrição da imagem aqui

  • Cleber, I will look at this solution. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.