R
R
rubtsoff2018-03-14 19:01:51
SQL Server
rubtsoff, 2018-03-14 19:01:51

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

4 answer(s)
K
Konstantin Tsvetkov, 2018-03-14
@tsklab

Use a trigger before adding an entry, or a default value from a function.

D
d-stream, 2018-03-15
@d-stream

So what's the question? It is perfectly normal for SQL to add a calculated field
to the table , as shown in the example.

R
rubtsoff, 2018-03-21
@rubtsoff

Everything was decided through cross join

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question