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

  • Skaters take tests to quantify advancement in their chosen discipline. Upon completion of each of these exams, a congratulatory email will be sent to the skater to acknowledge his or her achievement.
    Test data is stored in a database; this application will run daily. It will query the database for any tests completed the day before and will send emails to those who have completed their exams. It will also send a notification email to a Test Administer listing each of the skaters, the exam completed, and the date.

    Requirements
    Create a windows console application that retrieves data from a database and sends an email upon successful completion of an exam.
    1. Application will be scheduled to run daily. Data retrieved will be from the day previous. (a SQL mdb file is acceptable)
    2. Email body is to be in HTML format. Text is provided below.
    a. Email will greet each skater by name
    b. Email will specifically mention the Exam name
    c. Email will indicate the date on which the skater passed the exam
    d. Email will be ‘signed’ by the developer (See Text section below)
    3. Administrator notification email body is to be in HTML.
    4. Application must demonstrate at least two different exams (exam names)
    5. Application will contain at least one user defined class. It is not acceptable to demonstrate the concept within the default Program class.
    6. While demonstration of ability to send the email from the application is mandatory in lieu of actually sending an email, application will output to the console.
    7. Application source will be sent in a compressed file to the person delivering these requirements for evaluation. Time spent in development should be included in the message or with the project.
    need help with this. Thank you

    Reply
  • I have a Special Offer Product table in this one Product name,
    Offer Start date and Offer End date having column names

    how to write a query to
    when User enter a date

    to get the Product Name when that date is must should between Start date and End date of that table

    Please tell me how write the query the above the condition?

    Reply
  • sir i have to select the month from the date like

    20-Jan-11
    select the Jan from it
    plz help

    Reply
  • HIII

    I want to set the default date/time in sql server without using anyquery.. plzzz help

    Reply
  • my application is host in USA means the server is in USA but i want datetime of client machine. my client can be anywhere in world. it is return datetime of server but i want client machine datetime. please reply me……

    Reply
    • As long as you use proper DATETIME datatype and unambigious date format for date values, it does not matter. Refer this post for more informations

      Reply
  • hai ,
    how can i get the date beyond the limit(i guess the limit is around 1753 ) that had been defined by the sql. and that should be useful for different functionalities .

    Reply
  • how to convert UTC time to local time

    Reply
  • “ALTER SESSION SET TIME_ZONE” is in oracle. what is the command
    SQlserver
    for changing the timezone from EST to IST

    Reply
  • HI PINAL

    “ALTER SESSION SET TIME_ZONE” is in oracle. what is the command IN

    SQlserver
    for changing the timezone from EST to IST

    Reply
  • Hello Every one

    How can i display Date in

    20th July 2011 form in SQL SERVER

    i know in oracle we used date format

    DDth MON YYYY

    Don;t know how to do in SQL SERVER 2008 .

    Regards

    Qazi

    Reply
  • I am storing Created date and time in all the tables. The problem is that webserver and sql server is in US and it is storing US date time as the server is in US. How we will get indian date and time corresponding to US date and time

    Reply
  • yogesh101982
    July 30, 2011 6:35 pm

    i have a table1 with columns empid ,name,age and other table2 with columns empid ,salary ,here in table2 empid used as foreign key
    so pls anybody give me solution to find the highest salary of the employee

    Reply
    • select t1.empid from table1 as t1 where empid=(select top 1 empid from table2 where empid=t1.empid order by sal desc)

      Reply
  • want to know how can I get the YEAR,Month into database.. example .Suppose it’s August, 2011 now the output that i need is like this: CAB 11 08 001 .. (CAB + YEAR + MONTH + CURRENT NO. in tracking number. )

    this is my sql ..

    –ALTER PROCEDURE [dbo].[generateTrackNo] AS –Declare @tempYear VARCHAR(5),@tempMonth VARCHAR(5) –Set @tempYear = Year(GetDate()) –Set @tempMonth = Month(GetDate()) –SELECT ‘CAB’ + Right(Cast(Year(GetDate()) as varchar(10)),2)+ Right(Cast(Month(GetDate()) as varchar(10)),2) + Right(‘000000’+ Cast(CurrentNo as varchar(10)), 5) from tblTrackNo where GenYear = @tempYear –UPDATE tblTrackNo SET CurrentNo = CurrentNo + 1 where GenYear = @tempYear the output for this is CAB1180035 .. i need CAB1108035 .. i need to put Zero(0) 08 like this for Month..

    in my table i have only genYear and Current No. do i need to add another column for MONTH? thank you so much

    Reply
  • Hello All
    In oracle i can pass parameters to query in run time like
    select * from dept where deptno =&deptno
    when i run this query it prompts me for deptno at run time
    .
    how do i do this in sql server .

    waiting for reply thanks in advance.
    Yahya

    Reply
    • You need to use a variable and assign the value to it

      declare @depno int
      set @depno=345
      select * from dept where deptno =@depno

      Reply
  • hii
    i want to display only the time in sql server 2005
    so what will be the query????

    Reply
  • Hi,
    i have a table in which date is a field,this value gets entered once the user specify date in front end, now
    i need to intimate user after 3 months from this date stored?
    how to do it ? Do i need to give separate field for it?Please help

    Reply
  • I tried using datediff ,but it is not working .. so what to do?

    Reply
  • i have declared date as varchar, is that a problem?

    Reply
    • Yes. It will be a problem. You can not do any date related operations on varchar. You should always use proper DATETIME datatype. Refer this

      Reply
  • I changed datatype to datetime, and tried with the code, but it does not work, please say what to do?

    Reply
  • –Getting todays date record

    select * from tablename where datecolumn between ‘2011-10-25 00:00:00’ And ‘2011-10-25 23:59:59’

    Reply
    • Dont specify the date. It will work work if you run later. The general approcah is

      select * from tablename where datecolumn>=dateadd(day,datediff(day,0,getdate()),0) and datecol<dateadd(day,datediff(day,0,getdate()),1)

      Reply

Leave a Reply