Doubt with date and time in sql server

0

I have a field in the bank of type "char", 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

How could I bring the two dates together with the timetable

    
asked by anonymous 28.01.2017 / 04:14

1 answer

0

I'm not sure if this is what you are looking for, but you can work this 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 get the date in the format yyyy-mm-dd and concatenate with the time it is in the bd, along with the seconds, which have not been defined either.

Note that there are logical errors in your bd, ideally you should work on the two dates in the same format, yyyy-mm-dd hh:mm:ss , the way you are "forcing" the closing day so that it is today. / p>

See working in SQLfiddle

The concat function is not implemented in sql-server 2008 , as 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
    
28.01.2017 / 19:59