How to Calculate age by birthday, including month, day & Year? - visual-foxpro

What is the code for calculating the age by birthday?
How to Calculate age by birthday, including month, day & Year?

SELECT (DATE()-bdate)/365 FROM table1
where bdate is Date or DateTime field containing date of birth.

One of these articles or a combination of both should give you what you need:
http://www.tomorrowssolutionsllc.com/Articles/Calculate%20Age%20in%20Years%20and%20Months.pdf
http://www.tomorrowssolutionsllc.com/Articles/Computing%20Exact%20Age.PDF

Related

How do control the start date in DATESYTD and DATESQTD (for example: whether it is calendar year, ISO year and fiscal year)?

Here are my DAX formula's for YTD and QTD calculations.
Sales YTD := CALCULATE( [Total Sales], DATESYTD( 'Date'[Date] ) )
Sales QTD := CALCULATE( [Total Sales], DATESQTD( 'Date'[Date] ) )
The date table has dates from 01-01-2020 to 31-12-2040
What is the logic used by the DATESYTD and DATESQTD to derive the 1st date of the year (DATESYTD) and first date of the quarter (DATESQTD)? For example - How do I make it use the start date based on either of the following: calendar year, ISO year and fiscal year ?
For the DATESYTD, you can control the start of the year, by setting the year end date. The default with out this option will be the normal calendar year.
DATESYTD('Date'[Date]', "03-31")
This will start the YTD calculation from the "01-04" (Normal UK tax year)
For variations between Calendar, ISO and Fiscal years, you'll have to create individual measures, a switch based on a selection, or use a calculation group to drive your YTD measures.
DATESQTD doesn't have the same option. You'll need a well-formed calendar table for the other 'To Date' options.
Update: for week related start/end year, best to check out the dax patterns approach

Next week in Oracle

I'm using oracle dbms and I have in Employe table a column Birthdate. I want to write a query that shows the employees who has a birthday next week.
Is this correct ?
select name
from employe
where to_char(birthdate,'DD-MM')=to_char(next_day(sysdate,1)+7,'DD-MM');
That is not the correct usage of next_day(): that function returns the date of the the next instance of a day. For example, to find the date of next Friday:
select next_day(sysdate, 'FRIDAY') from dual;
To find employees whose birthday is seven days from now, you need to just tweak your query a bit:
select name
from employe
where to_char(birthdate,'DD-MM') = to_char(sysdate+7,'DD-MM');
The correct solution would be
SELECT name
FROM employe
WHERE to_char(birthdate
/* "move" the birthdate to the current year
to get a reliable week number */
+ CAST((EXTRACT(year FROM current_date)
- EXTRACT(year FROM birthdate)) || '-0'
AS INTERVAL YEAR TO MONTH),
'IW')
= to_char(current_date + 7, 'IW');
The IW format returns the ISO week containing the date, which is probably what you are looking for. If you start your week on Sunday, add one to both dates.

pl sql query for average by month

It's for a school project. I got a table Consultation with the following data :
DoctorId integer,
PatientFile varchar2(20),
visitDate date,
Diagnostic varchar2(20) and
Prescription varchar2(20).
I want to create a query that will show the average number of consultation by month. I try :
SELECT AVG(count(*)) AS count, MONTH(dateVisit) as month
FROM consultation
GROUP BY month
I doesn't work : I can't use the month fonction on dateVisit.
My questions : how would you do a query that will show the average number of consultation by month ?
Many thanks in advance for your help
I found the solution :
select avg (distinct (extract(month from visitDate))) as month from
consultation;
So here's how it's working :
1- extract(month from table_name) as month from table_name. You can
also put year or day instead of month.
2- distinct = will count the total for each month (instead of showing every record).
3- avg = average of each month.

extract week number from date postgres

I would like to extract the week number as:
2015-52
from a date formatted as:
2015-12-27
How can I perform this in postgres?
my weeks are calculated from monday to sunday.
To get the year and the week in a single character value, use to_char()
select to_char(current_date, 'IYYY-IW');
IW returns the year and the week number as defined in the ISO standard and IYYY returns the corresponding year (which might be the previous year).
If you need the year and the week number as numbers, use extract
select extract('isoyear' from current_date) as year,
extract('week' from current_date) as week;
I have done like this
extract(week from cast(current_date as date))
extract(year from cast(current_date as date))
As same with #a_horse_with_no_name, but be careful there is a little difference between extract('isoyear' from current_date) and extract('year' from current_date) if current_date is within week0 in new year(the last week(53) of last year), especially when you extract week as well.
For example:
The follow output is 2020 not 2021
select EXTRACT('isoyear' from to_timestamp('2021-01-02 17:37:27', 'YYYY-MM-DD hh24:mn:ss')) as year
The follow output is 2021
select EXTRACT('year' from to_timestamp('2021-01-02 17:37:27', 'YYYY-MM-DD hh24:mn:ss')) as year

Get records from database ordered by year > month > day

I have an Item model.
There are many records in the database with column created_at filled in.
I want to generate a view with such a hierarchy:
2014
December
31
items here
30
items here
29
items here
...
November
31
...
...
2013
...
What's the most elegant way to do that?
EDIT: Thank you so much for queries. How do I get that worked in Ruby on Rails?
To achieve this, we will order the records by the parts of date. Sample query below
SELECT
ItemDescription,
Year(DateField) AS Year,
Datename(mm, DateField) AS Month,
Day(DateField) AS Day
FROM tblName
ORDER BY
Year(DateField) DESC,
Month(DateField) DESC,
Day(DateField) DESC
This will provide you the data in the order expected. Now you can either create a stored procedure to modify the output to the format you need.
SELECT DATEPART(Year, PaymentDate) Year, DATEPART(Month, PaymentDate) Month, DATEPART(day, PaymentDate) Day,item_name
FROM Payments
GROUP BY DATEPART(Year, PaymentDate), DATEPART(Month, PaymentDate),DATEPART(day, PaymentDate) desc
ORDER BY Year, Month,day

Resources