Bring current date and time

2

I want to put the current date and time in this script

select convert(datetime, cast(year(getdate()) as varchar(4)) + '-' 
+ cast(month(getdate()) as varchar(2)) 
+ '-' + cast(day(getdate()) as varchar(2)) + ' 12:00:00.000');

Can someone help me?

    
asked by Rass 25.05.2018 в 14:07
source

2 answers

2

If you are only interested in a chain with the current date / time, you can do it like this:

  • SQL Server 2012 +

    SELECT FORMAT(CURRENT_TIMESTAMP, 'yyyy-MM-dd HH:mm:ss') hora_actual;
    

The interesting thing here is that you can use any of the representations of FORMAT to give the style you want to the exit.

    --Salida:

    hora_actual
    2018-05-25 15:55:40
  • SQL Prev 2012:

    SELECT CONVERT(VARCHAR(19), CURRENT_TIMESTAMP, 120) hora_actual;
    
    --Salida:
    
    hora_actual
    2018-05-25 15:55:40
    

NOTE: CURRENT_TIMESTAMP does not actually throw a date object, if you want to work with date object as such you should use SYSDATETIME() or GETDATE() .

For example:

SELECT FORMAT(SYSDATETIME(), 'yyyy-MM-dd HH:mm:ss') hora_actual;   -- 2012+
SELECT CONVERT(VARCHAR(19), SYSDATETIME(), 120) hora_actual;       -- 2012-


SELECT FORMAT(GETDATE(), 'yyyy-MM-dd HH:mm:ss') hora_actual;       -- 2012+
SELECT CONVERT(VARCHAR(19), GETDATE(), 120) hora_actual;           -- 2012-

The departures will always be the same.

    
answered by 25.05.2018 в 16:05
1

You should do it like that

select 
convert(datetime, cast(year(getdate()) as varchar(4))
+ '-' + cast(month(getdate()) as varchar(2)) 
+ '-' + cast(day(getdate()) as varchar(2))) + 
convert(time,getdate());
    
answered by 25.05.2018 в 14:18