SQL Date diff function

Posted on 2009-04-29
Last Modified: 2012-05-06
I am trying to do a datefiff function in SQL, and I keep getting the error: 'Date' is not a recognized built-in function name.

What am I doing wrong? How can I write this in SQL?

			Sum(IIf(DateDiff("d",[OpenedDate],Date())>7,1,0)) AS [Over 7 days],  

			Sum(IIf(DateDiff("d",[OpenedDate],Date())>=2 And DateDiff("d",[OpenedDate],Date())<=7,1,0)) AS [2-7 days], 

			Sum(IIf(DateDiff("d",[OpenedDate],Date())<=1,1,0)) AS [1 day or Less] 


Open in new window

Question by:saved4use
    LVL 28

    Expert Comment

    by:Bill Bach
    DATE() is not a function in SQL Server.  Perhaps you want to use GetDate() instead?
    LVL 75

    Accepted Solution

    Hello saved4use,

      Sum(CASE WHEN DateDiff(D,[OpenedDate],GETDATE() ) >7 THEN 1 ELSE 0 END )  AS [Over 7 days],  
      Sum(cASE WHEN DateDiff(D,[OpenedDate],GETDATE()) >=2 And DateDiff(D,[OpenedDate],GETDATE())<=7 THEN 1 ELSE 0 END )  AS [2-7 days],
      Sum(cASE WHEN DateDiff(D,[OpenedDate],GETDATE())<=1 THEN 1 ELSE 0 END ) AS [1 day or Less] FROM urTable


    LVL 6

    Expert Comment

    Shouldn't have the [] brackets in either spot.  Your name "Over 7 Days" should be just one word - see below.  Just try debugging ONE of them then apply the working code to the rest.

    Sum(IIf(DateDiff("d",OpenedDate,Date())>7,1,0)) AS Over7Days

    Try running that query.  Another good way to compare dates is like this:

    SELECT  COUNT(*) AS Butt, CONVERT(varchar, LoginDateTime, 101) AS Another
    FROM     kpmLoginLog
    GROUP BY CONVERT(varchar, LoginDateTime, 101)
    ORDER BY Another DESC

    Grouping by just the date piece in DateTime field from SQL.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
    Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now