Date Difference in MySQL to calculate age - mysql

I have a problem regarding the datediff MYSQL function, I can use it and it is simple. But I don't understand how to use it to collect differences within the table field. E.g.
I have a column dob and I want to write a query that will do something like
select dateDiff(current_timeStamp,dob)
from sometable 'here dob is the table column
I mean I want the difference from the current date time to the table field dob, each query result is the difference, the age of the user.

You mean like this?
SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(), dob)), "%Y")+0 AS age from sometable
(Source)

You could do this
SELECT TIMESTAMPDIFF(YEAR, date_of_birth, NOW()) ASageFROM your_table
Works everytime.

If you want, for each user, display the age in years, do
select name,extract(year from (from_days(dateDiff(current_timestamp,dob))))
from sometable;

If I understand your comments on the previous answers, the date-of-birth column is not actually a DATE value but a string in the format m/d/y. I strongly recommend you change this; it slows down any date computations you want to do and you risk invalid date values getting entered into the column.
I think this is what you need. It uses the STR_TO_DATE() function and an algorithm for computing the age from the MySQL documentation:
SELECT YEAR(CURDATE()) - YEAR(STR_TO_DATE(dob, '%m/%d/%Y'))
- (RIGHT(CURDATE(), 5) < RIGHT(STR_TO_DATE(dob, '%m/%d/%Y'), 5)) AS age
FROM sometable;

I think this should help
SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(now()) - TO_DAYS(#dateofbirth)), '%Y') + 0;
Note: Give the D.O.B in the correct format, E.g. YYYY-MM-DD'=> '1991-11-11

Try this
SELECT DATEDIFF(CURDATE(), '2014-02-14');

select truncate(datediff(curdate(),dob)/365.25,0) from table;

Related

How to check date and time individually in Mysql?

In my SQL table have column like startDate - DATETIME(data type) And it gives format like
`2013-08-08 06:30:00`
And i have two fields dateonly - 2013-08-08(value) timeonly - 06:30:00(value).
Now i want to query to check these two fields values with database value.
Hear two comparisons first check dateonly field is greater then or not and next check timeonly field is greater then or not.
is this possible in Mysql?
please help me in writing query
for date you can use this
WHERE DATE_FORMAT(datecolumn,'%Y-%m-%d') > 'some date here'
for time
WHERE DATE_FORMAT(datetimecolumn,%T) < 'sometime'
or use functions date(datetimecolumn) , time(datetimecolumn)
try this
select * from Demo d where DATE(d.startDate)< 'dateonly'
or
select * from Demo d where DATE_FORMAT(d.startDate ,'%Y-%m-%d')< 'dateonly'
I think you want this two functions:
Date(startDate) and Time(startDate) which can be found here: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

SQL Server: Want to use between clause with dates, but dates in string form (YYYY.MM.DD)

Help! One column in my database is for dates. All of my dates are unfortunately in the String form (YYYY.MM.DD). I have a MASSIVE database (300+GB) so ideally would like to avoid transformations.
Is there a way I can select rows for dates in between YYYY.MM.DD and YYYY.MM.DD? What would the script look like?
Thank you!
If the months and days are stored with leading zeroes, the BETWEEN operator will work as expected. So will ORDER BY.
create table your_table (
date_value varchar(10) not null
);
insert into your_table values
('2013.01.01'), ('2013.01.13'), ('2013.01.30'), ('2013.01.31'),
('2013.02.01'), ('2013.02.13'), ('2013.02.28'), ('2013.02.31'),
('2013.03.01'), ('2013.03.15'), ('2013.03.30'), ('2013.03.31');
select date_value
from your_table
where date_value between '2013.01.01' and '2013-01-31'
order by date_value;
2013.01.01
2013.01.13
2013.01.30
One of the main problems with your structure is that you lose type safety. Look at this query.
select date_value
from your_table
where date_value between '2013.02.01' and '2013.02.31'
order by date_value;
2013.02.01
2013.02.13
2013.02.28
2013.02.31
If you'd used a column of type date or datetime or timestamp, the dbms would not have allowed inserting the values '2013.02.31', because that's not a value in the domain of date. It is a value in the domain of varchar. (And so is "Arrrrgh!", unless you've got a CHECK constraint on that column that severely restricts the acceptable values.)
Not good solution, but works (cost much performance).
You have formated date in order year, month, day (good order to compare strings, without transformation to datetime), so you can try
SELECT * FROM Table WHERE StringDate > '2013.07.10' AND StringDate < '2013.07.14'
It returns bad results if there are dates before year 1000 without leading zero ('999.07.14').
But I dont know how it works on big database.
SQL Fiddle
Between in SQL is inclusive of both bounds. If that is what you want, you can just use between:
where col between 'YYYY.MM.DD' and 'YYYY.MM.DD'
Where the two constants are whatever values you are looking for.
If you have an index on the column, then between (as well as >, >=, and so on) will use the index. You do not need to transform the values. If your constants are dates of one form or another, then you can use date_format() to create a string in the right format. For instance, to get dates within the past week:
where col >= date_format(adddate(now(), -7), '%Y.%m.%d')

How to get just the date number in SQL (E.g. if its 2013/06/22 I just need the 22)

Does anyone know how to grab just the day number of the date result in SQL.
So for example if it was the 2013/06/22, what can I use to just grab the date number (22)?
For MS SQL, you can use DATEPART:
SELECT DATEPART(dd, '2013/06/22');
Here's a SQLFiddle.
For MySQL, you can use DAYOFMONTH:
SELECT DAYOFMONTH('2013-06-22');
Here's a SQLFiddle.
Both return 22.
In ANSI SQL standard, I don't believe there are any date time functions defined.
You should be clear on which DBMS you are asking.
In Oracle, you may do to_char(aDate, 'dd')
For example, select to_char(sysdate, 'dd') from dual give you day-of-month for current date.

Filter dates stored as varchar in mysql

I am working with a MySQL database where dates are stored as varchar like this:
'2013-01-31' in column cl_223
I need to select only records from 2013 so I tried:
SELECT ..
FROM ....
Where cl_223 Like '2013'
But that does not seem to work.
Thanks for all help!
You must add % as a wildcard :
SELECT ..
FROM ....
WHERE cl_223 LIKE '2013%'
Storing a datettime value in a varchar column complicates some functionality on date time operations. But of course you can select your values writing such a query as follow
SELECT * FROM table_name WHERE cl_223 LIKE '2013%'
But if you don't have any performance issue you can convert the varchar column to a datetime value and write stronger typed query like this:
SELECT * FROM table_name WHERE STR_TO_DATE(cl_223,'%Y-%m-%d2') BETWEEN '2013-01-01' AND '2013-12-31'
But if you need a date time value as a date time in your process you'd better store it in a datetime column instead of a varchar column.
The query should be
SELECT ..
FROM ....
Where cl_223 Like '2013%'
However, the better solution would be to store the dates as DATE data types. If the dates in that column are always used in the format they're in now, the change would be backwards compatible. It would also allow for easier processing of the date values.

Querying Where date_created without time is same as last_updated without time

In MySQL, I need to write a query (if possible) that finds all rows of a table where the date_created is the same as last_updated. The rub is that I need to ignore the time. Basically, I'm looking for user rows that were created and activated the same day (we don't store an activation date). So presumably the dates would be the same but the times may be different.
You could use the DATE() function, which returns only the date portion of a datetime value. This allows you to compare just the date portion of the values:
SELECT * FROM table_name
WHERE DATE(date_created) = DATE(last_updated)
The timezone may be relevant here. So you may want to cast the datetime values to the user's timezone prior to using the DATE() function, using CONVERT_TZ().
Try this:
SELECT *
FROM table_name
WHERE DATE_FORMAT(date_created, '%Y-%m-%d') = DATE_FORMAT(last_updated, '%Y-%m-%d')
not pretty but works:
SELECT *
FROM table_name
WHERE day(date_created) = day(last_updated) and
month(date_created) = month(last_updated) and
year(date_created) = year(last_updated)