Results 1 to 3 of 3

Thread: Date in tsql

  1. #1
    Join Date
    Feb 2008
    Posts
    4

    Unanswered: Date in tsql

    Hi,

    I wanna retrieve records for today. But not working?
    Any idea?

    select freedate from holidays where freedate = (getdate())

    how can I retrieve for specific date?

  2. #2
    Join Date
    Jan 2009
    Location
    United Kingdom
    Posts
    77
    getdate() will return the current time such as : 2010-08-21 13:28:29.570
    whereas if you want all records for today : you could do something like :


    select freedate from holidays where CONVERT(VARCHAR(8),freedate,1) = CONVERT(VARCHAR(8), GETDATE(), 1)

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    do not apply any function to the table column, as this will prevent an index from optimizing the query, you will get a table scan

    instead, do it this way --
    Code:
    SELECT freedate 
      FROM holidays 
     WHERE freedate >= DATEADD(D,DATEDIFF(D,0,GETDATE()),0)
       AND freedate  < DATEADD(D,DATEDIFF(D,0,GETDATE()),1)
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •