Click here to Skip to main content
15,891,672 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to make a query to calculate the age for the client auto in a column i have the Civil number for him like (286063001795)

Quote:
286063001795
((2 = 19 , 86 = 86 )(1986 year)) , 06 = 6 month , 30 = 30 days , 01795 = don't need them
(1996/6/30)
-
(today)
=
AGE


how can i make something like this

What I have tried:

i try many thing bout what i found is making it manual so i want to make it by query any help
Posted
Updated 3-Jan-19 0:26am
v3
Comments
Maciej Los 2-Jan-19 6:17am    
What Sql Server version?
If i understand you well, the hardest part is to "convert" civil no. into a date.
el_tot93 2-Jan-19 6:18am    
2017
BillWoodruff 2-Jan-19 6:23am    
so, focus first on proof-of-concept: write a method that takes a "civil" and returns a DateTime ... then figure out the SQL stuff.
el_tot93 2-Jan-19 6:25am    
thx for your answer bout im new in coding so i don't know how to do that
BillWoodruff 2-Jan-19 7:05am    
We were all new in coding, once :) For a newcomer, you are dealing with some advanced challenges here.

First, make a DATETIME value - that's pretty easy:
SELECT 
   Civil,
   CONVERT(DATETIME, 
      CASE WHEN SUBSTRING(Civil, 1, 1) = 2 THEN '19' ELSE '20' END + 
      SUBSTRING(Civil, 2, 2) + 
      '/' + SUBSTRING(Civil, 4, 2) +
      '/' + SUBSTRING(Civil, 6, 2), 
      120) AS DOB,
FROM MyTable
Now comes the difficult bit ... Age is a difficult concept, because it's not a fixed value, it's relative to the current date and it also has a number of components.
Getting it relative to the current date isn't too difficult - GETDATE will give that to you and you can easily subtract one from the other. The problem is the parts: x years, y months, and z days. You would need to calculate the timespan, and then work out the parts separately. See here: sql server - How to calculate age (in years) based on Date of Birth and getDate() - Stack Overflow[^] which gives a solution.

Me? I'd return the DOB from SQL using the code above, and then process the date in C# to give the age: Working with Age: it's not the same as a TimeSpan![^]

Actually, no, I wouldn't: I'd scrap the "Civil" format and store the DOB in a DateTime value instead where it's a lot easier to process. The "Civil" format can be generated from the "real" DOB pretty easily.
 
Share this answer
 
Comments
CHill60 2-Jan-19 6:32am    
"The 'Civil' format can be generated from the 'real' DOB" - only if the 01795 has been stored as well. I wholeheartedly agree with the store the DOB not the age though!
OriginalGriff 2-Jan-19 6:41am    
I'd probably want to know what that encodes - probably the time of birth? - and store that in a sensible format as well if it is at all relevant.

But working with "semi-encrypted" values in SQL is a PITA at the best of times ...
Maciej Los 2-Jan-19 6:36am    
You are pretty fast, Paul!
5ed!
el_tot93 2-Jan-19 6:47am    
Argument data type float is invalid for argument 1 of substring function.
OriginalGriff 2-Jan-19 6:54am    
You are storing the Civil value in a FLOAT?
You are mad.

That complicates matters a lot: instead of SUBSTRING, you will have to convert your FLOAT value to an INT, then use divide and modulus to extract each digit / digit pair prior to building your DATETIME value.

Seriously, dump it and use DOB as a DATETIME. It's a whole load easier to work with...
If i understand you well, the hardest part to resolve your issue is to "convert" civil no. into a date.

Well, MS SQL-Server 2017 has got a built-in function which can return date from parts: DATEFROMPARTS (Transact-SQL) - SQL Server | Microsoft Docs[^]
All what you have to do is to pass proper values: year, month and day. Finally, you can get result in years, months, etc. by using: DATEDIFF (Transact-SQL) - SQL Server | Microsoft Docs[^] function.
SQL
--CivilNo to date parts:
SELECT CASE WHEN SUBSTRING(CivilNo, 1, 1) = '2' THEN 19 ELSE 20 END AS YearPart, CONVERT(INT, SUBSTRING(CivilNo, 2, 2)) AS MonthPart, CONVERT(INT, SUBSTRING(CivilNo, 4, 2)) AS DayPart
FROM YourTableName


Now, you know how to "convert" Civil no. into date parts. The rest belongs to you! Good luck!

For further details, please see:
SUBSTRING (Transact-SQL) - SQL Server | Microsoft Docs[^]
CAST and CONVERT (Transact-SQL) - SQL Server | Microsoft Docs[^]

[Note]: Seems that OriginalGriff and me, we have the same idea about converting Civil no. into date, but i'm not so fast in typing and expressing myself as He is!
 
Share this answer
 
v2
Comments
el_tot93 2-Jan-19 6:47am    
Argument data type float is invalid for argument 1 of substring function.
BillWoodruff 2-Jan-19 6:59am    
+5
Maciej Los 2-Jan-19 7:19am    
Thank you, Bill.
In addition to OG's solution above, I assumed you would be storing the Civil number as … a number so I came up with something like this...

Get the date of birth in xMMDD format by dividing the civil number by 100000 (where x is 1, 2 as per your rules).

Convert that to a date in CCYYMMDD format by adding 17000000

Convert that number to a real date.

SQL
declare @demo table (id int identity (1,1), civicNo bigint, dob date)
insert into @demo (civicNo) values (286063001795),(260102901794)

update @demo set dob = CONVERT(datetime, convert(varchar(10), 17000000 + (civicNo / 100000)))
As you will see from the link in solution 1 there are lots of ways of calculating age - I tend to use the following method but be aware that I haven't tested it across all combinations of leap year in birth year, leap year in "today", day of date of birth etc.
SQL
select *, datediff(Month, dob, getdate())/12 as age, (civicNo / 100000),17000000 + (civicNo / 100000)
from @demo
I completely agree with @OriginalGriff though - don't store data that is that dynamic on the database, calculate it when required.
 
Share this answer
 
Comments
BillWoodruff 2-Jan-19 7:03am    
Ahoy: magic numbers sighted :)
CHill60 2-Jan-19 7:48am    
:blush:
Maciej Los 2-Jan-19 7:24am    
Caroline, i don't know how you do that, but you're awesome when you play with sql!
5ed!
el_tot93 2-Jan-19 7:49am    
bro how can i do it with my database im new in coding and i try muny time
table ( tabl )
civicNo ( CIVILIDD )
dob ( dob )
CHill60 2-Jan-19 7:51am    
How can you do what? I don't understand the rest of your comment - what is a tabl, a CIVILDD or a dob

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900