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

  • hello sir

    getdate result ime and date show

    select getdate as currenttime
    time and date will be show

    but i have a one problem in create table
    create table employee
    (
    emp_id int,
    emp_name varchar(30),
    emp_time datetime default ‘getdate()’
    );

    insert into employee tables(01,’happy’,”);
    but output is different emp_time 1900-01-01
    i want to say that default date and time come in emp_time
    plz sir help me about this query
    by

    Reply
    • If you want default datetime to be added, omit that column during insertion

      insert into employee (emp_id,emp_name) (01,’happy’);

      Reply
  • How to get difference between two times for example :

    ’28/10/2010 00:05:45′ and ’28/10/2010 00:07:50′

    i want to know how to get difference between only time ???

    answer must be in the format : ’00:02:05′

    Reply
  • Hello,

    can i date populated with the table dd-mon-yyyy format ?

    Reply
  • I want to insert a date as 19-Apr-2010 …..?
    can i inserting this format in table?

    Reply
  • Hi All,
    Pls any one help me.

    I want to calculate, How to calculate Year to Date And Month to date Records.

    Reply
  • How to retrieve current datetime “2/16/2008 12:15:12 PM” from sql query “SELECT GETDATE()” will come “2010-10-21 15:25:31.437”

    I need below format.

    “2/16/2008 12:15:12 PM”

    Can any one please help

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

    Very wrong. You said yourself, getdate() is a nondeterministic function and cannot be indexed in views.

    It makes a big difference in views with large tables, where you use the date in a where clause to filter for a reporting date.

    SELECT {fn NOW()}
    is the one you should choose, and getdate() is the one you should avoid.

    Reply
  • But this is very wrong.

    You wrote yourself:
    GETDATE is a nondeterministic function. Views and expressions that reference this column cannot be indexed.

    So if you have a view with a few joins, how do you think getdate() will influence the performance. I can tell you: getdate()’s performance is so very bad, you are a lot faster using the canonic ODBC escape sequence functions, such as fn now() …

    Reply
  • dear sir , i m software trainee , i inserted system date and time in table but i cant update that systemdate and time , i cant identify problem please give me a solutions..

    what query used in sql server to update?

    Reply
  • Pinal Dave-

    Just a quick thanks, this post has helped me a couple of times as a reference!

    Tom

    Reply
  • I have a Sql Sever 2008 DB with a list of dates and events associated with those dates. I’d like to return only those events that are happening on the current date.

    Something like
    SELECT [event] FROM [new database] WHERE ([date] = @currentdate) ..I know this isn’t right.
    Any advise would be appreciated

    Reply
  • Hi,

    Is there a way to get table record created date?

    Reply
  • Hi Nadeeka

    If u want get ‘creation date’ , ‘modification date’ for all tables then , u can use this …….
    …………………………………………………………….
    Select name as ‘Table Name’, create_date as ‘Creation Date’, modify_date as ‘Modification Date’ from sys.objects

    where type_desc= ‘USER_TABLE’
    ………………………………………………………….

    Satender

    Reply
  • i have a sql server 2005 database.
    on which a table cntains a filed datetime.
    now i need to fetch value filtered by date. but when i am executing query, it returns NULL. i think sql server stores date as datetime field, thats why i am not getting actual resuls. Please help what i will do.

    Indranil

    Reply
  • can you help me if there is any Facility or command in SQL Server 2005 ..
    to Execute the stored procedure on particular time span..

    I want to execute stored procedure /Function @ 00:01AM (Automatically) on daily basis..

    i m using ASP.Net

    I m fresher …Plz kindly tell me if possible..

    Reply
  • how to set the date modifiers to put the date with no year i.e. 12th March ??

    Reply
  • IN MS-SQL which function is equivalent to mysql’s “UNIX_TIMESTAMP()”

    Reply
  • Hi,

    Can someone tell me how to call a function/procedure from Oracle

    Thanks

    Sharath

    Reply
  • Hi,

    Can someone tell me how to call a function/procedure from Oracle in SQL SERVER

    Thanks

    Sharath

    Reply
  • Dear Sir,
    I create a login form in which if you insert password 3 times wrong then u r account is locked for next 15 minute.
    and we send a random password on referenced emailid which is sent after 15 minute.
    For these 15 minute the account isactive=’false’.

    So can u please help me to give code for a stored procedure or triggers which is fired after 15 minute,which update this account isactive = ‘true’.

    Please help me.

    Thanks In Advance

    Reply
    • When a user logins in, use this code

      if exists(select * from table where datediff(minute,datecol,getdate())>15
      update…….

      Reply

Leave a Reply