Answer the question
In order to leave comments, you need to log in
How to make the field get the value automatically?
There is a db list of athletes in it, it is necessary to calculate his category, that is, if his age is between 0-15 years old, then category C, if between 15 and 17, then category A. All this is necessary to generate automotility based on age (if an athlete’s age has changed, then category too). Through php it's all easy but I need to do it through sql.
I found commands under sql server but they have a different syntax.
alter table PATIENT add AgeCategory as
CASE
WHEN DateAdd(Year,1,DOB) > Current_Timestamp then 'infant'
WHEN DateAdd(Year,4,DOB) > Current_Timestamp then 'toddler'
WHEN DateAdd(Year,10,DOB) > Current_Timestamp then 'children'
WHEN DateAdd(Year,14,DOB) > Current_Timestamp then 'tween'
WHEN DateAdd(Year,18,DOB) > Current_Timestamp then 'teenager'
WHEN DateAdd(Year,65,DOB) > Current_Timestamp then 'adult'
ELSE 'elderly'
END;
Answer the question
In order to leave comments, you need to log in
Use a trigger before adding an entry, or a default value from a function.
So what's the question? It is perfectly normal for SQL to add a calculated field
to the table , as shown in the example.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question