Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Using DateDiff to calculate days before a Birthday


Using DateDiff to calculate days before a Birthday

Author
Message
Nadrek
Nadrek
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1047 Visits: 2673
Revenant (9/7/2010)
The correct way is to celebrate a Feb 29th birthday on the 28th, that's how the Roman (Catholic) calendar does it. In fact, it is not the 29th that is added on leap years: it is a second Feb 24th and the following four days of February are "bumped up."


The correct way to calculate age is to follow whatever regulations affect your industry or the operational definition of "age" specific to your business goal.

In the U.S., at least, different agencies calculate leap day baby ages differently.
Revenant
Revenant
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5829 Visits: 4720
Of course. I should have said "In absence of any industry rules or guidelines, ..."

However, the Common Law still says that if a debt is due on February 24th of a leap year, it may be paid on 25th (the second, inserted 24th).
kenkob
kenkob
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 64
Jeff Hi,

No matter what I try, I cannot get your code snippet to work, is there any possibilty you could expand it to show its used with code and using northwind.sdf. If I take out the --Today shown in Green in your code and having assumed it may be a comment it tells me the function Year is not recognised by the Compact Edition. When left in it gives an error parsing the query at Line 1 position 166 error = ,

But I cannot get rid of the error.
kenkob
kenkob
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 64
Jeff Hi,

No matter what I try, I cannot get your code snippet to work, is there any possibilty you could expand it to show its used with code and using northwind.sdf. If I take out the --Today shown in Green in your code and having assumed it may be a comment it tells me the function Year is not recognised by the Compact Edition. When left in it gives an error parsing the query at Line 1 position 166 error = ,

But I cannot get rid of the error.
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45382 Visits: 39938
Sorry, Ken... I was a bit asleep at the switch and forgot this was for the compact edition (heh... "CE" actually stands for "Crippled Edition", doesn't it? :-P)

There's more than one way to do the same thing so, let's check...

What do you get in CE when you run each of these individually?

SELECT YEAR(GETDATE())

SELECT DATEPART(yy,GETDATE())

--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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
kenkob
kenkob
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 64
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())"
kenkob
kenkob
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 64
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
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45382 Visits: 39938
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
kenkob
kenkob
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 64
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.
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45382 Visits: 39938
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search