Query with date and time in sql server

Asked

Viewed 549 times

0

I have a field in the "char" type bank, I am returning the dates this way:

select
   CONVERT(VARCHAR(20),HORA_FECHAMENTO) as HORA_FECHAMENTO,
   CONVERT(VARCHAR(20), DATEADD(hour, +0, getdate()), 120) as HORA_ATUAL 
  from TB_ESTRACAO   
where IDEXTRACAO = 5

inserir a descrição da imagem aqui

how could I bring the two equal dates along with the time

  • There is a problem in the current logic, closing time may be related to any day

  • @itasouza: There is no 24:00. From 23:59 it goes to 0:00. In the example you posted, it would be 2017-01-28 23:59:59 or so 2017-01-29 0:00:00. Which prefer?

  • Suggestion, do not change the question like this, add information at the end of it, not to misread the initial question.

1 answer

0


I’m not sure that’s what you’re looking for, but you can work that way:

Schema test:

CREATE TABLE teste
    ([HORA] VARCHAR(20))
;

INSERT INTO teste
    ([HORA])
VALUES
    ('24:00'),
    ('10:00'),
    ('5:20')
;

SELECT
CONCAT(CONVERT(CHAR(10), getdate(),126),' ',HORA,':00') AS HORA_FECHAMENTO,
CONVERT(VARCHAR(20), DATEADD(hour, +0, getdate()), 120) AS HORA_ATUAL 
FROM teste

Explaining the line: CONCAT(CONVERT(CHAR(10), getdate(),126),' ',HORA,':00') I only take the date in the format yyyy-mm-dd and concateno with the time that is in the comic, along with the seconds, which also were not defined.

Note that there are logical errors in your comic book, the ideal would be to work the two dates with the same format, yyyy-mm-dd hh:mm:ss, the way you are "forcing" the closing day to be today.

See working on Sqlfiddle


The function concat is not implemented in sql-server 2008, how output the solution can be:

SELECT
CONVERT(CHAR(10), getdate(),126)+' '+HORA+':00') AS HORA_FECHAMENTO,
CONVERT(VARCHAR(20), DATEADD(hour, +0, getdate()), 120) AS HORA_ATUAL 
FROM teste
  • i made your example and had an error: Message 195, Level 15, Status 10, Line 2 'CONCAT' is not a recognized built-in Function name.

  • Sql Server 2008 R2

  • I made the change, posted the answer, see that the date and time is all together

  • posted the result of the test I did with the adjustment

  • 1

    All right, I really appreciate your help!

Browser other questions tagged

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