We help IT Professionals succeed at work.

Calculating a value depending on current date

Hi

I'm wondering if someone can help me with the following problem for an SQL table.

Suppose I have the following items in the table:

ID_CHILD      NAME     DATE_OF_BIRTH     YEAR_GROUP

Suppose I enter a date of birth for a child in this table. The YEAR_GROUP that the child is in will depend not only on the date of birth of the child but also the current date e.g. the child may be in Year 1, Year 4 or whatever.

How can I get the SQL to automatically update the YEAR_GROUP field so when the table is examined or queried then the correct year group is allocated?

Thanks in advance

Issac
Comment
Watch Question

Better if you remove 'YEAR_GROUP' column and get the value of it dynamically whenever you write the query like :
SELECT ID_CHILD,NAME,DATE_OF_BIRTH,DATEDIFF(year,DATE_OF_BIRTH, GETDATE()) as 'YEAR_GROUP' FROM yourTable

Author

Commented:
Hi VipulKadia

I'm a newbie for SQL so not entirely sure how I could use your suggestion. I'm displaying the table in a DataGrid (VB.net). I would like to be able to display the year group in the table.

Any ideas how I could do this?

Issac
At Design Table put (datediff(year,[DATE_OF_BIRTH],getdate())) at the formula

Explore More ContentExplore courses, solutions, and other research materials related to this topic.