compare string to date on mysql - mysql

In MySQL 4.0.21-standard, I have a table with a date, saved as a string.
I want to compare this string with a date in my request.
SELECT FE_CLIENT.*
FROM FE_CLIENT
WHERE D_DATFINPUBLI < '2010/06/03'
How can I cast my column date_deb to a date for compare?

Assuming MySQL (if not, retag your question)
Use the MySQL STR_TO_DATE function to put the '2010/06/03' to a DATETIME value.
SELECT FE_CLIENT.*
FROM FE_CLIENT
WHERE D_DATFINPUBLI < STR_TO_DATE('2010/06/03','%Y/%m,%d');
Do the same thing for D_DATFINPUBLI if it's not already a DATETIME format.
EDIT:
SELECT STR_TO_DATE( D_DATFINPUBLI, '%d/%m/%Y %h:%i' ) DD, FE_CLIENT . *
FROM FE_CLIENT
WHERE STR_TO_DATE( D_DATFINPUBLI, '%d/%m/%Y %h:%i' ) < STR_TO_DATE( '04/06/2010', '%d/%m/%Y' )
AND D_CDSTATUPUBLI <> 'EXP'
ORDER BY D_NIDPUBLI

Just format your string to proper format before query execution
or, if you want it strictly with mysql,
WHERE D_DATFINPUBLI < replace('2010/06/03','/','-')
EDIT:
D_DATFINPUBLI field must be of date type and have format of 2010-06-03

Related

Converting date and time TEXT fields to one INT unix timestamp field in SQL

I currently have a mySQL database with two TEXT fields: Date and Time. These are in the format 'dd/mm/yyyy' and '0:00' respectively, for example '11/08/2020' and 19:12. I want to create a third field called Timestamp (of type INT) and convert the two original fields into this timestamp field and remove the date/time text fields.
I have done a bit of research in regards to using UNIX_TIMESTAMP() and STR_TO_DATE() but I can't seem to get it to work, the format seems to be wrong for it.
How can I achieve this in SQL and convert two string fields which represent date and time into a third field to replace them both which just stores the unix timestamp?
This is my best attempt so far..
SELECT UNIX_TIMESTAMP(STR_TO_DATE(CONCAT(`InfractionDate`, " ", `InfractionTime`), '%d %M %Y %h:%i%p')) FROM `playerinfractions`
The table is called playerinfractions and the date/time are stored in the TEXT fields InfractionDateand InfractionTime.
Many thanks in advance!
The format pattern that you use in the function STR_TO_DATE()is wrong.
Try this:
SELECT
UNIX_TIMESTAMP(
STR_TO_DATE(
CONCAT(`InfractionDate`, ' ', `InfractionTime`),
'%d/%m/%Y %H:%i')
)
FROM `playerinfractions`
You need to tell STR_TO_DATE the correct format of your date and time, which you suggested was '%d/%m/%Y %H:%i'
SELECT UNIX_TIMESTAMP(
STR_TO_DATE(
CONCAT('10/08/2020', ' ', '12:20'), '%d/%m/%Y %H:%i' )
)
)
FROM `playerinfractions`
So using your columns
SELECT UNIX_TIMESTAMP(
STR_TO_DATE(
CONCAT(`InfractionDate`, ' ', `InfractionTime`), '%d/%m/%Y %H:%i' )
)
)
FROM `playerinfractions`
I tried passing a string to the same function in below format and it worked. Also you can share your format to check it further.
select UNIX_TIMESTAMP(STR_TO_DATE(CONCAT('8/12/2020',' ', '12:01:49' ),'%m/%d/%Y %h:%i:%s'))

SQL Where clause with VARCHAR date

I have a MySQL DB to work with which has a LASTUPDATE column as VARCHAR.
This field has this format: YYYY-MM-DD HH:mm:ss
I need to set up a SELECT query to find all rows after e certain date, so i was trying to use the CONVERT function in this way
SELECT * from POWER WHERE CONVERT(DATETIME, LASTUPDATE) > 'CONVERT(DATETIME, '2016-12-29 17:24:22')'
but i'm getting the error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LASTUPDATE) > 'CONVERT(DATETIME, '2016-12-29 17:32:52')'' at line 1
I have also tried to use:
SELECT * from POWER WHERE CONVERT(DATETIME, LASTUPDATE) > '2016-12-29 17:32:52' but i'm getting the same error
You could use the str_to_date
SELECT *
from POWER
WHERE str_to_date(LASTUPDATE, '%Y-%m-%d %H:%i:%s' ) >
str_to_date('2016-12-29 17:24:22', '%Y-%m-%d %H:%i:%s' )
Use str_to_date()
The query should use str_to_date() like this. See MySQL Manual for more details
SELECT
*
from POWER
WHERE
str_to_date(LASTUPDATE, '%Y-%m-%d %H:%i:%s') > '2016-12-29 17:24:22'
Date and time literial
Note that MySQL accepts date and time literals, so you do not need another str_to_date on the right-hand side of the > operator. See this for more details.
About the form between ... and ... on a string field storing date value
Some suggested using the form of between ... and ..., such as the one
shown below. Note that this method would work only if the string
values happen to use a format in which sorting the string alphabetically (in the given collation) happens to give the same sorting order as date (the format used in the original question -- '%Y-%m-%d %H:%i:%s' -- happens to satisfy this condition, but this is not guaranteed (see an example below) for many string representations of date/time values).
SELECT
*
from POWER
WHERE
LASTUPDATE between '2016-12-29 17:24:22' AND '2016-12-31 23:59:59';
And it would fail if the string values are using a different format, such as these:
create table power (
lastupdate varchar(40) not null default ''
) engine=innoDB;
insert into power
values
('Dec 30, 2016 07:24:22')
, ('Jan 28, 2016 07:24:22')
For the above, we must still use the str_to_date() function (this time with a format argument %b %d, %Y %H:%i:%s that is matching the format used with the string values)
SELECT
*
from POWER
WHERE
str_to_date(LASTUPDATE, '%b %d, %Y %H:%i:%s') > '2016-12-29 17:24:22';
I have an sqlfiddle.com page showing the above example.
convert is an MS-SQL Server function. In MySQL, you can use the str_to_date function with similar semantics:
SELECT *
FROM power
WHERE STR_TO_DATE(lastupdate, '%Y-%m-%d %H-%i-%s') >
STR_TO_DATE('2016-12-29 17:24:22', '%Y-%m-%d %H-%i-%s')

Mysql database data format in .net

Hi I will use mysql db in my project. The database using with php now. I will use with asp.net. Tables have data column and date like this : 1289053800.
How can I convert this value to .net datetime.
Regards
It seems your date time values are stored in UNIX_TIMESTAMP format.
You can use FROM_UNIXTIME on them to convert to regular datetime type.
select
date_cloumn -- that has 1289053800 kind of values
, from_unixtime( date_cloumn ) as dt_f_ut
, date_format( from_unixtime( date_cloumn ), '%d.%m.%Y %h.%i' ) as dt_f_ut_12hr
, date_format( from_unixtime( date_cloumn ), '%d.%m.%Y %H.%i' ) as dt_f_ut_24hr
from table_name
;
Using .Net, you may need no changes but just read:
a DateTime on dt_f_ut or
a String on dt_f_ut_12hr or
a String on dt_f_ut_24hr or
into desired variables.
Demo # MySQL 5.5.32 Fiddle
Refer to:
FROM_UNIXTIME()
Format UNIX timestamp as a date
UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date)
Return a UNIX timestamp

subtract 2 datetime in mysql ( one in 24 hours format and one in am/pm format )

I'm trying to create a query using mysql.
select ID,NCOde,ifnull(EndTime,now())-starttime from xxx where starttime between
'2012-05-09 00:00:00' and '2012-05-09 23:59:59'
the problem is ifnull(EndTime,now()) return datetime in 24 hours format, while the starttime using am/pm format.
I've tried using DATE_FORMAT(starttime, '%m-%d-%Y %T'), but it seems that the operation changed the datetime type to other type.
Any advice?
Use STR_TO_DATE() to convert your starttime string to a MySQL DATETIME:
STR_TO_DATE(starttime, '%m-%d-%Y %r')
and then use TIMEDIFF() to subtract two times:
select ID,NCOde,
TIMEDIFF(ifnull(EndTime,now()), STR_TO_DATE(starttime, '%m-%d-%Y %r'))
from xxx
where STR_TO_DATE(starttime,'%m-%d-%Y %r')
between '2012-05-09 00:00:00' and '2012-05-09 23:59:59'
You should probably consider changing the data type of the starttime column to DATETIME or TIMESTAMP. Note also that this assumes EndTime is already of such a data type, or else you will also have to perform a similar conversion with it too.
Use the DATE_SUB() function.
Plus what eggyal said.

Creating DATETIME from DATE and TIME

Is there way in MySQL to create DATETIME from a given attribute of type DATE and a given attribute of type TIME?
Copied from the MySQL Documentation:
TIMESTAMP(expr), TIMESTAMP(expr1,expr2)
With a single argument, this function returns the date or datetime expression expr as a datetime value. With two arguments, it adds the time expression expr2 to the date or datetime expression expr1 and returns the result as a datetime value.
mysql> SELECT TIMESTAMP('2003-12-31');
-> '2003-12-31 00:00:00'
mysql> SELECT TIMESTAMP('2003-12-31 12:00:00','12:00:00');
-> '2004-01-01 00:00:00'
To get a true DATETIME value from your two separate DATE and TIME values:
STR_TO_DATE(CONCAT(date, ' ', time), '%Y-%m-%d %H:%i:%s')
You could use ADDTIME():
ADDTIME(CONVERT(date, DATETIME), time)
date may be a date string or a DATE object.
time may be a time string or a TIME object.
Tested in MySQL 5.5.
datetime = CONCAT(date, ' ', time);
select timestamp('2003-12-31 12:00:00','12:00:00');
works, when the string is formatted correctly. Otherwise, you can just include the time using str_to_date.
select str_to_date('12/31/2003 14:59','%m/%d/%Y %H:%i');
Without creating and parsing strings, just add an interval to the date:
set #dt_text = '1964-05-13 15:34:05.757' ;
set #d = date(#dt_text) ;
set #t = time(#dt_text) ;
select #d, #t, #d + interval time_to_sec( #t ) second;
However this truncates the microseconds.
I agree with Muki - be sure to take account of time zones and daylight savings time!