Using DateDiff to calculate days before a Birthday

  • Jeff,

    Year = A similiar error, Error Parsing Line 1 at 29 error = (

    DatePart = error Parsing line 1 at 33

    I am using Northwind.sdf Employees table which has a Birthdate field for the example

    cmd.CommandText = "SELECT * FROM Employees YEAR(GETDATE())"

    cmd.CommandText = "SELECT * FROM Employees DATEPART(yy,GETDATE())"

  • Jeff,

    Just entering SELECT YEAR(GETDATE()) give the error Function is not recognized by SQL Server Compact Edition of Function [Year]

    Running DATEPART returned 2010

  • kenkob (9/10/2010)


    Jeff,

    Just entering SELECT YEAR(GETDATE()) give the error Function is not recognized by SQL Server Compact Edition of Function [Year]

    Running DATEPART returned 2010

    Then doing a little substitution in my orginal query should do the trick... (I'll let you change the DB name/schema if necessary)

    SELECT *

    FROM AdventureWorks.HumanResources.Employee

    WHERE DATEDIFF(dd,

    DATEADD(yy, -(DATEPART(yy,GETDATE())-1900),GETDATE()), --Today

    DATEADD(yy, -(DATEPART(yy,BirthDate)-1900),BirthDate)

    ) BETWEEN 1 AND 10

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff,

    After a little trouble I have got it working. When I attempted to use it as a SqlCe CommandText string surrounded by inverted commas, I was always getting an error. However, by adding the code copied directly from the web forum page into a multiline textbox it worked fine.

    Thank you very much for your help.

  • kenkob (9/12/2010)


    Jeff,

    After a little trouble I have got it working. When I attempted to use it as a SqlCe CommandText string surrounded by inverted commas, I was always getting an error. However, by adding the code copied directly from the web forum page into a multiline textbox it worked fine.

    Thank you very much for your help.

    Thanks for the feedback, Ken. I usually end up pasting the copy from the forum into MS Word which "fixes" a lot of stuff from forums and then copy/paste to my application. Almost the same thing as you did.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 5 posts - 16 through 19 (of 19 total)

You must be logged in to reply to this topic. Login to reply