I have a table called calendar with a column DateTo
This has format 2014-02-19T16:00:00 (varchar and can not be changed)
I would like to set time to 13:00
I've tried
UPDATE calendar
SET DateTo = (SUBSTRING(DateTo FROM 1 FOR 11) + '13:00')
WHERE SUBSTRING(DateFrom FROM 1 FOR 10) = '2014-02-19'
Thinking this would give me:
2014-02-19T13:00
but instead it returns 2027?
You can do as
update
calendar
set DateTo = concat(
substring_index(DateTo,'T',1),'T','13:00'
)
where substring_index(DateTo,'T',1) = '2014-02-19'
;
DEMO
As MatBailie suggested instead of
where substring_index(DateTo,'T',1) = '2014-02-19'
its better to use
WHERE DateTo LIKE '2014-02-19T%'
This will be significantly faster if there is an index on DateTo
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);
I want to put this format data on my table: 2018-12-04T13:05:00-00:00
This should be done with a query:
$uPqr = $conn->query("UPDATE table SET dateModified = "the date goes here" WHERE id = 25");
I don't want to write the date on the query, i want to know if there's a function like NOW() or time() that do it automatically.
If you want the current time -- on the server -- then just use now():
$uPqr = $conn->query("UPDATE table SET dateModified = now() WHERE id = 25");
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 want to update a databased based upon results from a case, however my "If" statement doesn't seem to pull information from the right table.
declare #dt datetime
set #dt = GETDATE()
select
Ugenummer = datepart(wk, #dt) - datepart(wk,dateadd(m, DATEDIFF(M, 0, #dt), 0)) + 1,
case when (datepart(wk, #dt) - datepart(wk,dateadd(m, DATEDIFF(M, 0, #dt), 0)) + 1) % 2 = 1
then 'ulige' else 'lige' end Ugelighed;
The above code gets the weeknumber (ugenummer) and determines whether it's odd or even (Ulige/lige)
IF Ugelighed = ulige and datepart(dw,#dt) = 1
THEN
UPDATE laeger
SET Antal=1
WHERE Navn=Lægenavn
I would like to use the information from the first code to update a database in a SQL 2008 server
You aren't using Ugenummer so I edited it out. Also created a variable for Ugelighed, you forgot to create one:
declare #dt datetime
set #dt = GETDATE()
declare #Ugelighed VARCHAR(5);
select
#Ugelighed=case when (datepart(wk, #dt) - datepart(wk,dateadd(m, DATEDIFF(M, 0, #dt), 0)) + 1) % 2 = 1
then 'ulige' else 'lige' end;
In the following statements I used variable #Ugelighed to compare to ulige however it is a VARCHAR so you need to enclose it in single quotes (''). Same for Lægenavn, it is a VARCHAR so enclose it in single quotes:
IF #Ugelighed = 'ulige' and datepart(dw,#dt) = 1
UPDATE laeger
SET Antal=1
WHERE Navn='Lægenavn'
Personally I would have created a BIT field for #Ugelighed since even/odd can be captured as a simple boolean rather than text.