如何将年龄转换为月数 [英] How Do I Convert Age To Number Of Months

查看:332
本文介绍了如何将年龄转换为月数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个专栏年龄,其中某些数据是年份,某些数据是月份格式..

我需要将此年龄列转换为否几个月。

Ex:

年龄(数据库数据)----几个月的年龄

4.00 ---- 48.00

0.18 ---- 18.00



我尝试在谷歌搜索,但无法找到合适的结果。

我只需要将年龄栏目更改为月份年龄格式。



请帮助。

-

在此先感谢:)

Hi,
I have a column Age in which certain data is in Year and certain data in months format..
I need to convert this Age column to No of months.
Ex:
Age in Year(DB data) ---- Age In Months
4.00 ---- 48.00
0.18 ---- 18.00

I tried searching in google, but unable to find the appropriate result.
I just need to change the "Age in Year" column to "Age in Months" format.

Please Help.
--
Thanks in Advance :)

推荐答案

你应该尝试这样的事情:

Months = int(Age )* 12 +压裂(年龄)* 100



int是整数部分

压裂是分数部分
You should try something like this:
Months= int(Age)*12+ frac(Age)*100

int is integer part
frac is fractionnal part


首先,通过混合字段的不同值类型来破坏您的数据库。在允许数据进入数据库之前,必须在代码级别验证数据。

但是,除此之外,在数据库中保持年龄是错误的。每个生日的年龄都在变化,导致其价值过时。你会在每个生日那天更新它的价值吗?没门。您应该将出生日期(DOB)保留在数据库中,然后在需要时使用代码根据DOB计算年龄。

查看过去的年龄计算解决方案:如何使用C#计算年龄 [ ^ ]
Firstly, your database is corrupted by mixing different value types for a field. Data must be validated at code level before it is allowed to go into the database.
But that aside, it is a mistake keeping age in database. Age is changing on every birthday causing its value to be outdated. Are you going to update its value on every birthday? No way. You should just keep the Date of Birth (DOB) in the database then calculate the age based on the DOB using code when the need arises.
See past solution on age calculation: How to calculate Age using C#[^]


如前所述,您需要将出生日期存储在数据库的单个列中。根据您的问题,我了解您希望在SQL中进行计算,因此在完成此操作后,您可以将列中的值与使用 GETDATE()



请考虑以下示例:

As already pointed out you need to store the date of birth in a single column in your database. From your question I understood that you want to do the calculation in SQL so after you have done that, you can compare the value in the column to current date and time retrieved using GETDATE().

Consider the following example:
create table dobs (
   name varchar(100),
   dob date
);

insert into dobs values ('Person 1', convert(date, '07/07/1990', 101));
insert into dobs values ('Person 2', convert(date, '08/08/1991', 101));

select name,
       datediff(year, dob, getdate()) as years,
       datediff(month, dob, getdate()) as overallmonths,
       datediff(month, dateadd(year, datediff(year, dob, getdate()), dob), getdate()) as months_over_year
from dobs;



今天它将产生


On today it'll produce

name       years   overallmonths   months_over_year
--------   -----   -------------   ----------------
Person 1   25      301             1
Person 1   24      288             0


这篇关于如何将年龄转换为月数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆