Convert Date functions to valid dates in SQL Server ?

Posted on 2005-04-30
Last Modified: 2010-03-19
Could you tell me how to convert the following 2 Date functions into valid SQL Server statements ?

1) DateAdd("m",[MthTo]*-1,Date()-DatePart("d",Date()-1)) AS DateFromC,

2) DateDiff("m",[DateLost],Date()-DatePart("d",Date()-1))>=[MthFrom]

Would it be something close to the following:

1) DateAdd('m',[MthTo]*-1,DateAdd(DD, DatePart(DD, GetDate() -1) * -1, GetDate())

2) DateDiff('m',[MthTo]*-1, DateAdd(DD, DatePart(DD, GetDate() -1) * -1, GetDate())

Question by:zimmer9
    1 Comment
    LVL 28

    Accepted Solution

    Try this:

    1) DateAdd("m",[MthTo]*-1,Date()-DatePart("d",Date()-1)) AS DateFromC

    DATEADD(M, [MthTo] * -1, DATEADD(DD, (DATEPART(DD, GETDATE()) - 1) * -1, GETDATE()))

    2) DateDiff("m",[DateLost],Date()-DatePart("d",Date()-1))>=[MthFrom]

    DATEDIFF(M, [DateLost], DATEADD(DD, (DATEPART(DD, GETDATE()) -1) * -1, GETDATE())) >= [MthFrom]

    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.

    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 …
    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    Via a live example, show how to shrink a transaction log file down to a reasonable size.
    Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

    759 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

    11 Experts available now in Live!

    Get 1:1 Help Now