help with datedif

Posted on 2009-04-24
Last Modified: 2012-05-06
Hello, I need to delete a row if the datecreated is greater than 30 days.

Declare @Today as datetime set @Today = GetDate()
Delete Friends
where dateCreated = ?
Question by:gogetsome
    LVL 11

    Accepted Solution

    I assume you mean, if the datecreated value is more than 30 days in the past?  If so...

    WHERE DateCreated < DATEADD(DAY,-30,GETDATE())
    LVL 59

    Expert Comment

    by:Kevin Cross
    Aaron is correct.  If you have timestamps and want to ensure that you don't get different results based on time you run query do this to force date to midnight.

    DELETE Friends
    WHERE DateCreated < DATEADD(DAY, -30, DATEDIFF(DAY, 0, GETDATE()))

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
    Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
    Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
    This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

    761 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

    8 Experts available now in Live!

    Get 1:1 Help Now