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

  • VishwanathReddy
    June 2, 2008 7:34 pm

    Hi Everyone
    can any one find solution for this qury

    I have one column which contains Date field. and I want date difference between two rows like (1st row- 2nd row),(2st row- 3nd row) 3-4,4-5 like that.

    Thanks in Advance
    VishwanathReddy

    Reply
  • Hi
    can we get only time in datetime(),if anyone knows plz update………

    ex:

    I need it to return 01:34:30
    instead it returns 1901/01/01 01:34:30

    Thanks
    Kathir

    Reply
  • Darshan shah
    June 9, 2008 5:30 pm

    Hi S. Ramkumar,
    u can use convert function and u will get the value as u want

    convert(datetime,getdate(),101)

    Reply
  • Darshan shah
    June 9, 2008 5:39 pm

    Hi Kathir,

    Can u please use this ?

    convert(datetime,getdate(),108)

    Reply
  • Darshan shah
    June 9, 2008 5:41 pm

    Hi esam,

    can u plz use between clause?

    Reply
  • Darshan shah
    June 9, 2008 5:44 pm

    Hi kpn,

    can u please use datepart function and thn use ‘=’ condition.

    datepart(yyyy,date)

    Reply
  • can u tell me, how to dispay time like this “1994-11-05T08:15:30-05:00” corresponds to November 5, 1994, 8:15:30 am, US Eastern Standard Time.using sql server 2005

    Reply
  • Hi,
    can any one help me Please. i have a date column “DateTime” populated with 2008-05-01 11:45:58.380 with thousands of dates , when i run report /Excute i would like to get result ” DataTime” as 2008-05-01 only, not 2008-05-01 11:45:58.380. Is there any way to truncate the date field??

    Reply
    • Have a look at Convert function in SQL Server help file. Also formation is the job of front end application

      Reply
  • Hello,

    I have entered this trying to get the current date into my table with the field named InvoiceSentDate. Is this the correct usage b/c the column keeps coming back null.

    select @InvoiceSentDate = GETDATE()

    Thanks,
    Nathan

    Reply
  • This worked for me: –

    SELECT dbo.AuditTrail.ATTime,
    CONVERT(char(12), dbo.AuditTrail.ATTime, 3) ,
    CONVERT(char(12), dbo.AuditTrail.ATTime, 8) ,
    CONVERT(char(12), dbo.AuditTrail.ATTime, 10) FROM dbo.AuditTrail WHERE CONVERT(char(12), dbo.AuditTrail.ATTime, 3) = ’01/05/08′

    Reply
  • Hello,

    I would like to format the CURRENT_TIMESTAMP. Suppose CURRENT_TIMESTAMP return ‘2008-02-09 11:46:17.827′. But I want ’09-02-2008’. Is it possible.

    With Regards.
    Biplab

    Reply
    • It is the job of presentation layer
      If you dont have any other option, use format function

      select replace(convert(varchar(10),CURRENT_TIMESTAMP,103) ,'/','-')

      Reply
  • Dear Friends,

    I want to find only the differnece in the time in minutes and hours can u suggest me how can I do it

    Reply
  • Hi Kabir/Biplab,

    Did you guys find any solution to the problem.

    Thanks,
    Sanjana

    Reply
  • Hello, i have a problem witn a stored procedure which uses datetime variables. The procedure looks like :

    BEGIN TRANSACTION

    DELETE FROM dbo.station
    WHERE stationId = @stationId
    AND auditDate = @time_stamp

    auditDate and @time_stamp are both DATETIME

    When I try to execute the procedure, it gives the following error : “Error converting data type nvarchar to datetime”

    If i try to use another date format , like mdy not dmy , there is no row updated. Any idees ?

    Thanks

    Reply
  • Imran Mohammed
    July 23, 2008 5:58 am

    @tylo

    Its not the problem of datetime format, the problem is with Nvarchar and datetime. Either auditdate or stationid must be nvarchar datatype and you are trying to compare a datetime with a nvarchar datatype… which is not possible.

    Try using a convert function and then compare, it would be helpful if you post your complete query….

    Thanks,
    Imran.

    Reply
  • i am struggling from one concept.let me any one help for me?

    how to find difference b/w two dates with timings….

    Reply
  • Imran Mohammed
    July 29, 2008 7:57 am

    @ sathish,

    select datediff ( dd, ‘01.23.1985’, ‘02.23.1985’)

    Result: 31

    Hope this helps.

    Thanks,
    Imran.

    Reply
    • Note that you should always express dates in unambigious formats like YYYYMMDD. Othwerwise depends on the Server’s date format, you may get error

      Reply
  • jitender Pal
    July 29, 2008 8:39 pm

    hi all
    Can anybody help me out to calculate the age of a person…in
    years-month-days like 23 years-9 months-10 days
    we are having a column as DOB(date of birth) in one table….
    I want to calculate using current date….

    thanx in advance

    Reply
  • Imran Mohammed
    July 30, 2008 1:04 am

    @jitender Pal,

    I would use some thing like this for your question,

    declare @date varchar(100)
    set @date = ‘2.23.2005’
    select (convert (varchar(10), datediff (yy, convert (datetime , @date), getdate()))+ ‘ Years- ‘ + convert (varchar(10), datepart (mm, (getdate())- convert (datetime , @date) ))+ ‘ Months- ‘ + convert (varchar(10),datepart (dd, (getdate())- convert (datetime , @date) ))+’ Days’) AS DOB

    You might want to change this code accordinly to your needs.

    Hope this helps.
    Thanks,

    Reply
  • hi
    I need to generate an expiry date baised on the date entered in one coloumn
    can you help me out with this

    Reply

Leave a Reply