is it possible to default the day field when using the string to date function in MySQL ?
I am using this:
sample_date = cast(STR_TO_DATE(#sampl_date,'%Y%m') AS date)
the input values I am receiving are in the format of
201305
201203
etc
so the str_to_date function works fine in converting that but it shows the day as 00 which I would like to default all of them to 01.
I tried various forms of the str_to_date function but none worked.
sample_date = cast(STR_TO_DATE(#sampl_date,'%Y%m%01') AS date)
sample_date = cast(STR_TO_DATE(#sampl_date,'%Y%m-01') AS date)
You could use this:
cast(STR_TO_DATE(CONCAT(#sampl_date, '01'),'%Y%m%d') AS date)
Related
I am using PHP with MySQL and would like to select rows that have a booking time within 2 hours from now. How do I compare what is in my database with the NOW() MySQL function?
I have columns pickupDate in the format yyyy-mm-dd and pickupTime in the format HH:mm (24-hour). I have tried creating a query with NOW() which returns the a 12-hour time as HH:mm:ss e.g. 2019-05-24 07:54:06 . I can't figure out how to format this to 19:54, or if I should use a different function instead.
For example, if the current date and time is 24/05/19 19:54:06, I would like to select rows between 19:54 and 21:54 on this date.
My table structure is:
referenceNo VARCHAR(100)
pickupDate DATE
pickupTime VARCHAR(100)
You need to create a DATETIME compatible value out of your pickupDate and pickupTime (which you can do by CONCATing them together), then you can compare that with a time range from NOW() to 2 hours later:
SELECT *
FROM yourtable
WHERE CONCAT(pickupDate, ' ', pickupTime) BETWEEN NOW() AND NOW() + INTERVAL 2 HOUR
Demo on dbfiddle
To add two hours in php
$hoursnow = date('H:i');
$timestamp = strtotime(date('H:i')) + 60*60*2;
$plusTwohours = date('H:i', $timestamp);
And $PlusTwohours using this variable frame the query like below
Sql Query:
$sqlQuery = 'select * from foodorder where pickupDate=DATE(NOW()) AND pickupTime>='.$hoursnow.' and pickupTime<='.$plusTwohours;
$result = mysql_query($sqlQuery);
variable $result will have the values of query
For Second Scenario: Adding hours to end of the day May 24 23:30:00
This should be handle by two different date for same column pickupDate
$d = new DateTime('2011-01-01 23:30:30');
$startDate = $d->format('Y-m-d H:i:s'); // For testing purpose assigned manually
$starttime = date('H:i');
// Here Process start, storing end date by adding two hours
$enddate1 = strtotime($startDate) + 60*60*2;
$enddate = date('Y-m-d', $enddate1); // Extracting date alone
$endtime = date('H:i', $enddate1); // Extracting time alone
Have to compare start and end date for column pickupDate, here is the query
$sqlQuery = "select * from foodorder where pickupDate>=DATE(".$startDate.") AND pickupDate<=DATE(".$enddate.") AND pickupTime>='".$starttime."' AND pickupTime<='".$endtime."'";
$result = mysql_query($sqlQuery);
Describe:
I have a table with timestamp column and i want to get the number of values where the timestamp in specific time window.
My code is as shown in here:
String startTime = "2018-08-08 00:00:00";
String endTime = "2018-08-08 23:59:59";
productDF.where("CREATETIME >= '" + startTime + "' AND CREATETIME <= '" + endTime + "'").count();
I also tried between...and...sentence; and also:
productDF.where(unix_timestamp(col("CREATETIME"), "yyyy-mm-dd hh:mm:ss")
.cast("timestamp")
.between(
Timestamp.valueOf(startTime),
Timestamp.valueOf(endTime)
)).count();
The result i get is 6843.
But when i operate the sql sentence using Navicat:
SELECT COUNT(*) FROM my_table
WHERE CREATETIME BETWEEN '2018-08-08 00:00:00' and '2018-08-08 23:59:59';
it shows 7689.
Problem:
I want to know why i get the different results in Spark and Mysql.....what am i missing here??
Problem solved!
The problem happened because of the TIMEZONE.
In spark env., it get the timezone from_unixtime..So need to set the config.
.config("spark.sql.session.timeZone", "UTC")
But I still don't understand why the spark sql session flow the system timezone instead of just select from the column.....
I have to update table which have column data type as integer, but I have an input as a datetime. this is query that i hve writen
UPDATE T_SCH_ETAX_TEMP SET CURRTIME = UNIX_TIMESTAMP
(TIMEDIFF("(SELECT DATE_FORMAT(?,'%H:%i:%s') TIMEONLY)", "(SELECT
DATE_FORMAT(CURRENT_TIMESTAMP,'%H:%i:%s') TIMEONLY)"), LENGTHTIME =
UNIX_TIMESTAMP(TIMEDIFF("(SELECT DATE_FORMAT(?,'%H:%i:%s')
TIMEONLY)", "(SELECT DATE_FORMAT(CURRENT_TIMESTAMP,'%H:%i:%s')
TIMEONLY)")
any one can help how to convert timestamp result as an integer so I can update the table using SSIS
I could be wrong, please forgive me if I am, but you seem to want something simpler:
UPDATE T_SCH_ETAX_TEMP
SET CURRTIME = UNIX_TIMESTAMP(
TIMEDIFF(TIME(?),
TIME(CURRENT_TIMESTAMP))),
LENGTHTIME = UNIX_TIMESTAMP(
TIMEDIFF(TIME(?), TIME(CURRENT_TIMESTAMP)));
Which would only require 2 parameters ('ss');
If this is the case, in can be simplified to:
UPDATE T_SCH_ETAX_TEMP
SET CURRTIME = UNIX_TIMESTAMP(
TIMEDIFF(?,
CURRENT_TIMESTAMP)),
LENGTHTIME = UNIX_TIMESTAMP(
TIMEDIFF(?, CURRENT_TIMESTAMP));
if you pre-format your parameter strings
I have a MySQL table where new entries are given a timestamp like this:
`timestamp` timestamp NULL default CURRENT_TIMESTAMP,
and looking like this:
2014-01-01 01:01:01
And I would like to be able to plot these timestamps in a ggplot2 scatterchart using:
[...]
if(myxaxis == "timestamp") {
p = p + scale_x_datetime(as.POSIXct("xvalue"))
}
print(p)
[...]
I get an error:
Error: character string is not in a standard unambiguous format
How can I transform my timestamp to the correct date format for ggplot2's scale_x_date?
At the moment, it looks like the as.POSIX function is trying to work out what date and time the actual string "xvalue" is. If your date/time string is stored in a variable called xvalue, then try removing the double quotes:
p = p + scale_x_datetime(as.POSIXct(xvalue))
Why does my date comparison not work,
mostly returning either all dates or none
Select DATE_FORMAT(time,'%Y - %m - %d'),'2013 - 01 - 10'
FROM signature,files
WHERE files.id = signature.id and instruction like '%BM%'
AND DATE_FORMAT(time,'%Y-%m-%d') < date('2013-01-10') ;
doesn't return result. when I change to DATE_FORMAT(time,'%Y-%m-%d') < '2013-01-10' same and even this DATE_FORMAT(time,'%Y %m %d') didn't work
DATE_FORMAT function returns a string with character set and collation.I am not sure if you will get correct comparison using DATE_FORMAT function.
Select DATE(time),'2013 - 01 - 10'
FROM signature,files
WHERE files.id = signature.id and instruction like '%BM%'
AND Date(time) < Date('2013-01-10') ;
or use STR_TO_DATE function.
SELECT STR_TO_DATE('2013,05,01','%Y,%m,%d');