This question already has answers here:
calculating age in years and months in Microsoft Access (2010)
(2 answers)
Closed 7 years ago.
On the Microsoft Access form I have created for patient data at a hospital I want to use the field DATE OF BIRTH and the field DATE OF FORM (current date) in order to calculate the three fields
age(yr)
age(mo), and
age(days).
Is it possible to do this after just being given two dates?
Yes, those two dates are enough to calculate age. So you can use the DateDiff function in the Expression builder of the Age textboxes.
Below, I assume Age(yr) means age using years, Age(mo) means age using months, and Age(days) means age using days.
In the control source of Age(yr) textbox use:
=DateDiff("yyyy",[DateOfBirth],Date())
For Age(mo):
=DateDiff("m",[DateOfBirth],Date())
For Age(days):
=DateDiff("d",[DateOfBirth],Date())
Related
This question already has answers here:
Add days Oracle SQL
(6 answers)
Closed 12 months ago.
I am having an issue trying to add a portion to my WHERE clause to get only the last 3 days of data rather than grab the whole table. The date format comes through as 'dd-Mon-yy'
Here is my current WHERE clause
WHERE ("IAINVN00"."REF_LOCN" LIKE '51C%'
OR "IAINVN00"."REF_LOCN" LIKE '511%')
This works fine, just brings back way too much data. When I add:
and "IAINVN00"."ADJDATE" >= (Date()-3)
This brings back an error of "ODBC--call failed. [Oracle][ODBC][Ora]ORA-00936: missing expression (#936)"
I have tried using this as well and get the same error
DateAdd("d",-3,Date())
In order to fix this, instead of using Date, I needed to use SysDate.
and "IAINVN00"."ADJDATE" >= sysdate - 3
Run a query that returns the number of trouble tickets in the VTM024… table, but only the ones created in the year 2015. Note: when setting the where criteria, use a function to extract the Year portion from the timestamp field.
Using the Year function in the first answer, write SQL that shows the number of trouble tickets created in each year dynamically, and do this using a “GROUP BY” clause in the SQL. It should return something like:
2012 10
2013 54
2014 111
etc
I was able to answer first question myself:
SELECT COUNT(CREATE_TIME) FROM TRACS_DW1.VTM024TROUBLE_TKT
WHERE EXTRACT (YEAR FROM CREATE_TIME) = 2015
I need help with 2nd question. I cant get groupby and extract functions together.
i have added a picture of my table.
Add EXTRACT (YEAR FROM CREATE_TIME) as year to your select query and group by the year field.
This question already has an answer here:
Filter query result by most recent date
(1 answer)
Closed 8 years ago.
Here's the table below I am using in MS-Access.
There can be multiple entries for the same item but I need to get the value corresponding to the latest dated entry. For example considering the below dataset,
If I want to know what the closingStock for item 'XYZ' is, it should return 70 as that is the latest entry (as per date /dd-mm-yyyy). I am using ms-access and vb.net for this, which I am very much unfamiliar with and have tried using a max(date) etc, but access syntax/interface seems a bit weird to me now. Would appreciate any help. I would prefer to do this in vb.net code.
Try this:
SELECT Max(tblInventory.transdate) AS MaxOftransdate, tblInventory.item, Last(tblInventory.closingStock) AS LastOfclosingStock
FROM tblInventory
GROUP BY tblInventory.item;
Replace the tblInventory with whatever your table name is. You can also replace the 'Max' with 'Last' if you prefer
Craig
This question already has answers here:
Compare 3 Consecutive rows in a table
(2 answers)
Closed 8 years ago.
I have a large table of transactions identified by user id and date. For each user's last transaction, I would like to calculate the time elapsed since the previous transaction. Is there something like a lag operator I can use to do this?
You can find an example of using Window Aggregate functions to accomplish LEAD and LAG in Teradata here.
I am wondering if it's possible (without actually parsing the given string) to get the actual range (in terms of days, minutes or seconds) that is specified when you have an SQL statement like
[select 'x'
from dual
where date between to_date('20111113152049')
and to_date('20120113152049')]
I am working on a query where I'm given a string in the form of
"between to_date(A) and to_date(B)"
and would like to get that value in days to compare to a policy we let the user set so they don't enter a date range longer than say a week.
Assuming you're looking for a theoretical answer (that is: don't take this into production) this could work:
Prerequistes:
have three tables: days_seq(day_seq), month_seq(mth_seq) and year_seq(yr_seq)
days has the numbers 1...31, month 1..12, years 2011....?
Use te following query (I used access because I don't have proper RDBMS available here, keep in mind that MS-ACCESS/JET is forgiving in the use of the Dateserial function, that is, it doesn't break when you ask the dateserial for february, 30th, 2012)
SELECT Max(DateSerial(
[year_seq]![yr_seq]
,[month_seq]![mth_seq]
, [days_seq]![day_seq]))
-
Min(DateSerial(
[year_seq]![yr_seq]
,[month_seq]![mth_seq]
,[days_seq]![day_seq])) AS days
FROM days_seq, month_seq, year_seq
WHERE DateSerial(
[year_seq]![yr_seq]
,[month_seq]![mth_seq]
,[days_seq]![day_seq])
BETWEEN #2012-02-1# AND #2012-02-28#
The query basically produces a carthesian product of three tables which generates all possible days in months, months in a year for as many years as you have in the years table.
Bonus:
You could off-course generate a permanent Calendar table as X-Zero suggests.
table calendar([date])
INSERT INTO calendar
SELECT DISTINCT DateSerial(
[year_seq]![yr_seq]
,[month_seq]![mth_seq]
, [days_seq]![day_seq]))
FROM days_seq, month_seq, year_seq
You still have to pick your start year and your end year wisely. According to the Maya's an enddate of december 21st, 2012 will do.