SQL SERVER – Retrieve Current DateTime in SQL Server CURRENT_TIMESTAMP, GETDATE(), {fn NOW()}

There are three ways to retrieve the current DateTime in SQL SERVER. CURRENT_TIMESTAMP, GETDATE(), {fn NOW()}

SQL SERVER - Retrieve Current DateTime in SQL Server CURRENT_TIMESTAMP, GETDATE(), {fn NOW()} currentdatetime

CURRENT_TIMESTAMP
CURRENT_TIMESTAMP is a nondeterministic function. Views and expressions that reference this column cannot be indexed. CURRENT_TIMESTAMP can be used to print the current date and time every time that the report is produced.

GETDATE()
GETDATE is a nondeterministic function. Views and expressions that reference this column cannot be indexed. GETDATE can be used to print the current date and time every time that the report is produced.

{fn Now()}
The {fn Now()} is an ODBC canonical function which can be used in T-SQL since the OLE DB provider for SQL Server supports them. {fn Now()} can be used to print the current date and time every time that the report is produced.

If you run following script in Query Analyzer. I will give you the same results. If you see the execution plan there is no performance difference. It is the same for all the three select statements.

SELECT CURRENT_TIMESTAMP 
GO 
SELECT {fn NOW()} 
GO 
SELECT GETDATE() 
GO

Performance:
There is absolutely no difference in using any of them. As they are absolutely the same.

My Preference:
I like GETDATE(). Why? Why bother when they are the same!!!

Quick Video on the same subject about datetime

[youtube=http://www.youtube.com/watch?v=BL5GO-jH3HA]

Reference: Pinal Dave (https://blog.sqlauthority.com)

SQL DateTime, SQL Function, SQL Scripts, SQL Server
Previous Post
SQL SERVER – Find Length of Text Field
Next Post
SQLAuthority.com News – iGoogle Gadget Published

Related Posts

458 Comments. Leave new

  • Dharmendra Dixit
    September 16, 2009 3:30 pm

    @Kanika,

    you can do that by using Datetime conversion functions available in SQL..
    like
    select convert(varchar(10),getdate(),105)

    Reply
  • Can we select any of them as field’s Default Value if field type is DateTime?

    Or

    Is there any solution for that?

    I need a column if it doesn’t specified on insert it’s default value should be current date time!

    Reply
  • Sir,

    Will you please tell me how to format Date as “mmm-dd-yyyy hh:mm” in SQL server 2005 & ms-access

    Reply
  • Sir,

    Will you please tell me how to format Date as “mmm-dd-yyyy hh:mm” in SQL server 2005 & in Ms-Access.

    Reply
    • Formation matters only if you want to show dates in front end application. If you use front end application, use format function there. Otherwise read abot CONVERT function in SQL Server help file

      Reply
  • Sultan Ibrahim
    October 13, 2009 7:56 pm

    Converting UTC datetime to a desired format like 2009-01-08 18:26:54.923 instead of 2008-06-18T13:51:46.7-05:00

    Reply
  • Thanks for your post.

    BTW the only one of the three that works within a DTS package against a legacy KB-SQL ODBC datasource is {fn NOW()}.

    I’m a newbie, and am guessing it is because the others aren’t understood by the KB-SQL’s flavour of SQL?

    Anyway, very glad that I remembered your post (which I’d already referred to for straight MSSQL queries) and found that one out of three worked in the context of what I was doing.

    So, while they’re all the same performance-wise, there is a crucial difference in breadth of application.

    Thanks again.

    Reply
  • hi
    i have problem in inserting the record depending on the current date. if current date change then record has to be inserted automatically.plz tel me how to write trigger or jobs in sql server2000

    Reply
  • Actually i’m finding that

    CURRENT_TIMESTAMP is correct, it returns the full date and time :
    2009-10-26 04:58:24

    GEDATE returns always return a time of 0 ( the date is correct though )

    example : 2009-10-26 00:00:00

    Si in my case i prefered to use CURRENT_TIMESTAMP because i sort my news by time, and i might have several news an hour.

    Pat.

    Reply
  • Hi,

    I need help on writing a query for both these scenarios :

    1. The current date is equal to or greater than 92 days past a semester start date.
    2. The current date is not greater than 92 days past a semester start date.

    Semester start date is a column..
    Thank you..

    Reply
  • Hi,
    I am new in sql server,
    i have some problem ,
    like when i write this code

    select * from [some table] where
    convert (datetime,’1/5/2009′,103′) >= getDate()

    when return the record is lose some of them .

    i hope help me .

    thanks

    Reply
    • The correct way od foing it is

      select * from [some table]
      where
      date_col>= dateadd(day,datediff(day,0,getDate()),0) and
      date_col< dateadd(day,datediff(day,0,getDate())+1,0)

      Reply
  • Hi all,

    I have table in which hiredate is column and i want to calculate, Using HireDate of an employee the time they have worked. Basically i want to calculate the difference between hire date and current date of my system.

    Thanks a lot in advance.

    Jay

    Reply
  • Sultan Ibrahim
    October 28, 2009 7:16 pm

    print datediff(day, ‘2009-10-01’, getdate())

    will give you 27, because I used ‘day’ as the first parameter.

    https://docs.microsoft.com/en-us/sql/t-sql/functions/datediff-transact-sql?view=sql-server-2017

    Reply
  • Select Replace(Convert(Varchar,GetDate(),106),’ ‘,’-‘)

    Reply
  • how to find last 15 days, last 2 month date

    Reply
  • how to find day on end of month

    Reply
  • for my report I specify the following:
    DECLARE
    @StartDate datetime,
    @EndDate datetime
    SET @StartDate = ‘2009-12-10 21:00′
    SET @EndDate = ‘2009-12-11 07:00′

    SELECT ……
    WHERE Datecolumn >= @StartDate
    and Datecolumn < @EndDate.

    For the report, instead of specifying date and time ('2009-12-10 21:00'), I want the users to only specify the time (21:00). Which is the best way to do this ?

    Reply
  • Hello Clive,

    If you are using SQL Server 2008 then you can convert the date variables to TIME data type.

    In earlier version use the following syntax:

    WHERE Datecolumn >= CONVERT (VARCHAR(5), @StartDate,108)
    and Datecolumn < CONVERT (VARCHAR(5), @EndDate,108)

    Regards,
    Pinal Dave

    Reply
  • é o seguinte precisava de uma ajuda vossa , quero visuallizar registos somente da semana passada , ou entao de 8 dias atraz mas nao queria q aparecesse o dia actual
    tenho usado estes commandos :
    DATEPART(week, dvl.DATa) = DATEPART(week, GETDATE()) AND
    DATEPART(year, dvl.DATa) = DATEPART(year, GETDATE())
    e este :
    dvl.data > = getdate () -8

    Reply
  • m.kamala kannan
    January 4, 2010 11:02 am

    below query returns no rows
    select max(hmp_effectivefrom) from mos_HandlingMarkupPercentage with(nolock) where
    convert(varchar(10),hmp_effectivefrom,101)<=convert(varchar(10),getutcdate(),101) and hmp_Discontinued=0
    group by hmp_EntityId

    i am using convert(varchar(10),getutcdate(),101) format

    below query returns the rows what is the diff between both
    select max(hmp_effectivefrom) from mos_HandlingMarkupPercentage with(nolock) where
    convert(varchar(10),hmp_effectivefrom,102)<=convert(varchar(10),getutcdate(),102) and hmp_Discontinued=0
    group by hmp_EntityId

    i am using convert(varchar(10),getutcdate(),102) format

    Reply
    • Have you tried this?

      select max(hmp_effectivefrom) from mos_HandlingMarkupPercentage with(nolock)
      where
      hmp_effectivefrom<=dateadd(day,datediff(day,0,getutcdate()),0) and hmp_Discontinued=0
      group by hmp_EntityId

      Reply
  • m.kamala kannan
    January 4, 2010 11:26 am

    pls reply me as soon as possible

    Reply

Leave a Reply