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
Related
I have dates in varchar type like:
201601
201602
201603
201701
201702 and so on
I am trying to view all my records where the dates are in ascending order. So I am using this query:
SELECT * FROM emp_pp GROUP BY YEARMM ORDER BY STR_TO_DATE(YEARMM,'%Y%m')
Here YEARMM is my column name. The query isn't working properly when I run it. Instead I keep getting all these notices:
Incorrect datetime value: '201601' for function str_to_date
Incorrect datetime value: '201602' for function str_to_date...
Why is that? Please help me
As mentioned by Akina, STR_TO_DATE function requires enough data to generate at minimum a full date value to work correctly. You do not have that (you can not have a date 2016-02-00, for example).
What you do have is a numerical Year and Month integer, 201601, 201602, 201603, 201701 etc. which orders exactly as you want by standard numerical ordering so all you need to do is remove the STR_TO_DATE part entirely:
SELECT * FROM emp_pp GROUP BY YEARMM ORDER BY YEARMM ASC /* Oldest date first */
Of note:
SELECT * is highly inefficient. You should name each column you want to collect.
Column names in SQL should not be upper case, as this is hard to read with the correct case syntax, SQL column should be only lower case; yearmm would be more readable in your SQL code.
i have an events table having start date and end date I am trying retrieve all the records by giving a date that is between start and end dates.
eg :
SELECT *
FROM `events`
WHERE '2017-01-29' BETWEEN start_date='2017-01-28'
AND end_date='2017-01-31'
but response is syntax error can any one help me to finish the query
Just list the columns.
WHERE '2017-01-29' BETWEEN start_date AND end_date
The values come from the table, you don't put them into the query.
According to mysql documentation (https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_between) the syntax for BETWEN is
expr BETWEEN min AND max
it is not
expr BETWEEN blabla=min AND stuff=max
Also, it is rather pointless to be using constants in all three expressions, because in this case the result will be known in advance (either always TRUE or always FALSE) without having to consult the values in your table.
It is kind of hard to give you an example without knowing the structure of your table, but what you probably want is something like
WHERE '2017-01-29' BETWEEN start_date
AND end_date
(assuming start_date and end_date are columns in your table)
or something like
WHERE some_column BETWEEN '2017-01-28'
AND '2017-01-31'
(assuming some_column is a column in your table.)
I believe you're trying to find all the rows where a date is 2017-01-29, and so, your query could be:
SELECT *
FROM `events`
WHERE
date = '2017-01-29';
If, however, you want all rows with date between 2017-01-28 and 2017-01-31, then you could do:
SELECT *
FROM `events`
WHERE
date BETWEEN '2017-01-28' AND '2017-01-31';
Instead of putting 2017-01-29 before WHERE, put the name of the field you want to filter by date, such as EventDate (or whatever your field is named).
I am working for a POS company, i have a query in my program that gets date from multiple tables and one of the fields it returns is a date field that returns the the date in the format yyyy-mm-dd. Is there a way to get my query to bring this date field in the form 'dd'? if yes how?
You are looking for the date_format() function (see here):
select date_format(<datefield>, '%d');
This returns the value as a string. If you want it as a number, just use day():
select day(<datefield>)
You are looking for this:-
EXTRACT(unit FROM date)
or try this:-
SELECT DAY(your_date_field) AS dtDay FROM your_table
select DATE_FORMAT(your_date_column,' %d ') FROM your_table
As you mentioned Delphi, why not a Delphi solution:
open the query in a dataset
get the corresponding field
get the value with AsDateTime
use function DayOf from System.DateUtils to retreive the day
in case you need a dataset field for that, create a calulated field
move the day calculation into the OnCalcFields event of the dataset
There might still be plenty of other ways to do it.
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')
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;