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

  • I am using (right(CONVERT([char](8),getdate(),(108)),(10))) to get only the time part,however it is not showing the AM,PM part.How can I get the AM,PM part.
    Please suggest.

    Thank You

    Reply
  • Somalia, look at the documentation on CONVERT to see what formats support am/pm. I see 100, 109, 130, and 131.

    Reply
  • hi,

    I want to fetch todays date from the whole database , not from any particular Table and that is already in database. please help me…!!!

    Reply
  • I think GETDATE() works more quckly than {fn Now()}

    Reply
  • hi,
    I want to find the difference between the current date and the date stored in the database(I used datetime datatype to store the dates in the database).
    I m new to the sql server pleeeeeeeeeeeeese help!!!!

    Reply
  • Hi!

    I need a bit of unique help.
    Is there any way to retrieve the last updated records (for the last week say) if you DO NOT have the date stored anywhere in your table?

    I know this is a dumb question but then again it’s kind of a silly mistake to make when creating the table! N what the hell… it’s worth a shot rite?

    Could someone please tell me if they think this is at all remotely possible?

    Reply
  • am pretty new to sql queries…

    in the below query
    ‘———————————–
    SELECT USERS.USERS_NAME, COMPANY.COMPANY_NAME, LOGS.LOGS_DATETIME, LOGS.LOGS_ACTION
    FROM LOGS INNER JOIN
    COMPANY ON LOGS.LOGS_COMPANY_ID = COMPANY.COMPANY_ID INNER JOIN
    USERS ON LOGS.LOGS_USERS_ID = USERS.USERS_ID
    WHERE (LOGS.LOGS_COMPANY_ID = 11) AND (LOGS.LOGS_ACTION LIKE ‘uploaded%’) AND (LOGS.LOGS_DATETIME = GETDATE()) OR
    (LOGS.LOGS_ACTION LIKE ‘downloaded%’)
    ORDER BY LOGS.LOGS_DATETIME DESC
    ‘———————————–
    —i need to filter records from the log table which as uploaded and downloaded in logs_action field.

    this query should be run on the current date and teh cutoff time is 5.30pm for e.g. the query should run for today date and any file uploaded or downloaded before 5.30pm today should be shown.
    thanks a ton

    Reply
  • Maegen Wilson
    July 6, 2009 7:57 pm

    Hello,

    I am trying to get the number of days between a date span.
    If I have a span of three months I would like it to appear like the example down below. I would have the parameter as 01/01/2009 – 03/31/2009, but want the results split out. Can anyone help?

    Jan xxhours
    Feb xxhours
    March xxhours

    Reply
  • Brian Tkatch
    July 6, 2009 10:37 pm

    @Maegen

    Look at DATEDIFF() to find the intervals. If you have difficulty, post what you have tried and perhaps we can help.

    Reply
  • vishnu vardhan
    July 13, 2009 10:17 am

    Hi,
    I need to get details from attandence table
    date,timein,timeout by chid id.

    In this table stroring one day n times timein and time out. But i need day first time in and day last time out
    plz send the result for this if any one know this is my mail id

    Reply
  • Dexter V. dela Paz
    July 23, 2009 1:15 pm

    Hello to everyone

    SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS SystemDate, LEFT(RIGHT(CONVERT(varchar, GETDATE(), 0), 8), 6) + ‘ ‘ + RIGHT(CONVERT(varchar, GETDATE(), 0), 2) AS SystemTime

    Just copy the code above to obtain the Date and Time separate in each column the time format is HH:MM AM/PM that will vary in your system time and the date format is MM/DD/YYYY.

    Sample Output:

    SystemDate SystemTime
    07/23/2009 3:59 PM

    I hope you find it useful.

    God Bless to all :)

    Reply
  • Dexter V. dela Paz
    July 23, 2009 1:19 pm

    Hello to everyone

    SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS [SystemDate], LEFT(RIGHT(CONVERT(varchar, GETDATE(), 0), 8), 6) + ‘ ‘ + RIGHT(CONVERT(varchar, GETDATE(), 0), 2) AS [SystemTime]

    Just copy the code above to SQL Query or View to obtain the Date and Time separate in each column the time format is HH:MM AM/PM that will vary in your system time and the date format is MM/DD/YYYY. There’s so many person want to know how will you format time in HH:MM AM/PM now here’s the answer. luckily i find it out. :)

    Sample Output:

    SystemDate SystemTime
    07/23/2009 3:59 PM

    I hope you find it useful.

    God Bless to all :)

    Reply
  • If you want UTC date time use:

    SELECT GETUTCDATE();

    Gareth.

    Reply
  • Dear all
    i would like query from my table but we have many data record the same infor but i need query only on colum
    example depent only one type
    in my table have

    Row —mobile name—-type—–creae date—–install date
    1 mobile1 A 03/05/2009 03/04/2009
    1 mobile1 A 07/06/2009 07/06/2009
    1 mobile1 A 05/07/2009 05/07/2009
    1 mobile1 A 12/08/2009 12/08/2009

    for user normall i use
    mobile1 A 12/08/2009 12/08/2009

    Pleae help me because we have more information

    Reply
  • dear all

    I want to make query for the same minute of transactions table, does any one help me

    thanks

    Reply
  • Hi,

    I want to create database backup file with current date and time.
    For example : database name is : RemoteDatabase
    Current Date Time is : 10 August 2009 11:29 AM

    How can I give this name to my .bak file?

    Or if it is not possible can I provide time only like
    RemoteDatabase_11:29AM.bak

    Thanks
    Vineeta

    Reply
  • Is it possible to put GetDate() or CURRENT_TIMESTAMP as a field’s default value? If yes, when I update a row, will this field update itself with the the timestamp of that update moment?

    Reply
  • Imran Mohammed
    August 11, 2009 8:22 am

    @Lucas

    1. First question, Can Getdate() be used as default value to a column ?
    Ans: Yes, GetDate() can be used as a default value for a column.

    2. Second Question, when I update a row, will this field update itself ?
    Ans: No. It will not update by itself, you will have to update this manually.

    Consider below script as example.

    create table #Example1 ( Eid Int, Ename varchar(40), Date_Time datetime default (Getdate()))

    Insert into #Example1 (Eid, Ename) values ( 1, ‘SQL’ )
    Insert into #Example1 (Eid, Ename) values ( 2, ‘SQQ’ )

    Select * from #Example1

    — Now update statment.

    Update #Example1
    Set Ename = ‘SQL’
    , Date_Time = Getdate() — updating manually.
    Where Eid = 2

    Select * From #Example1.

    Drop table #Example1.

    How ever, this could be done automatically as well,

    You can have a FOR UPDATE or AFTER UPDATE Trigger created on the table.

    This trigger will fire when there is any update on the table, and inside this trigger, you can update your datetime column with the current datetime.

    You can also use, if update statement inside trigger, to be specific, mentioning if there is any update for these specific columns only then update date_time column.

    I dont suggest using trigger though, if you see many updates on the table. This could decrease your application performance significantly.

    ~ IM.

    Reply
  • HI all,

    can i retrieve the values for time between 5:00 pm and 8:00 pm from apptime column in database, does any one help me

    Reply
  • Hi All,

    Can anyone tell me how to find the difference of (year/month/day) birth date to till date.

    i see the imran code but it is not perfect. I have to count actual year,month and date.

    thanks,
    Mazhar

    Reply

Leave a Reply