I have a MySQL table like this-
And a query builder of Laravel like this-
$baseQuery = DB::table('webinars')
->select(
'id',
'title',
'description',
'hosts',
DB::raw('concat(DATE_FORMAT(starts_on,"%d %b %Y %h:%i %p), " ", timezone) as starts'),
'duration',
'created_at'
)
->where('user_id', '=', $user_ID)
->where('starts_on >= NOW()');
So, I am getting error for this 2 line-
DB::raw('concat(DATE_FORMAT(starts_on,"%d %b %Y %h:%i %p), " ", timezone) as starts')
And
->where('starts_on >= NOW()');
Error is -
Can anyone help me please?
You are missing a double quote after date mask:
DB::raw('concat(DATE_FORMAT(starts_on, "%d %b %Y %h:%i %p), " ",
timezone) as starts')
Should be:
DB::raw('concat(DATE_FORMAT(starts_on, "%d %b %Y %h:%i %p"), " ", timezone) as starts')
->where('starts_on >= NOW()');
you can use:
whereRaw('starts_on >= NOW()') or
where('starts_on', '>=', new DateTime('today'))
Related
I need to update column pubDate_Date to 2018-02-04 20:39:55 from column pubDate with format Sun, 04 Feb 2018 20:39:55 +0100.
I have tried the following, but without success:
UPDATE `feeds` SET `pubDate_Date` = date('Y-m-d H:i:s', strtotime(`pubDate`))
Hope someone can point me in the right direction
In general, you can use MySQL's STR_TO_DATE() function:
UPDATE feeds SET pubDate_Date = STR_TO_DATE(pubDate, '%a, %d %b %Y %T')
However there's no format code for timezone offsets, which MySQL will therefore ignore from the end of your original string (instead interpreting each string to be in the session's time_zone). Therefore:
If all records are in the same timezone, you could simply do SET time_zone = '+01:00' before the above update command.
Otherwise, do the following instead to adjust each time to the intended timezone:
UPDATE feeds SET pubDate_Date = CONVERT_TZ(
STR_TO_DATE(pubDate, '%a, %d %b %Y %T'),
##session.time_zone,
CONCAT(SUBSTR(pubDate, -5, 3), ':', RIGHT(pubDate, 2)
)
'19/10/2013 2:41'
I have a huge dataset in the above format that I want to insert into my table. But I guess 'YYYY-MM-DD HH:MM:SS' is the format for datetime.
Any solutions?
You can use php preg_split() function.
<?php
$myDate = '19/10/2013 2:41';
$split = preg_split('/[\/\s]/',$myDate);
$formattedDate = $split[2] . '-' . $split[1] . '-' . $split[0] . ' ' . $split[3];
echo $formattedDate;
?>
Since your input is a string in the form 03.09.13, I'll assume (since today is September 31, 2014) that it's dd-mm-yy. You can convert it to a date using STR_TO_DATE:
STR_TO_DATE(myVal, '%d/%m/%y')
Then you can format it back to a string using DATE_FORMAT:
DATE_FORMAT(STR_TO_DATE(myVal, '%d/%m/%y'), '%Y-%m-%d')
Note that the year is %y (lowercase "y") in STR_TO_DATE and %Y (uppercase "Y") in DATE_FORMAT. The lowercase version is for two-digit years and the uppercase is for four-digit years.
I have a field 'time' in my Mysql server and want to query to database to get this field in my C program as below:
select * from tbl where str_to_date(time, '%M %d %Y %H:%i:%s') > given_date
Now the problem is printing format specifiers in my query string. How to print %d, %i and %s in the query string?
If you want to use % in a formatting string, without using it as a format specifier, use %% instead:
char buffer[1024] = {};
char query_string[] = {
"select * from tbl where str_to_date(time, '%%M %%d %%Y %%H:%%i:%%s') > '%s'"
};
char given_date[] = { "03 4 2014 14:55:21" };
sprintf(buffer, query_string, given_date);
printf(buffer);
Output:
select * from tbl where str_to_date(time, '%M %d %Y %H:%i:%s') > '03 4 2014 14:55:21'
I have following php routine to extract values from a table. I'm trying to convert the DATE type from "2014-08-10", value to "20140810", value
Rtn:
//query
$query = mysql_query("SELECT CAST(date AS DATE), EtotalDay from MonthData group by 1 order by 1")
OR die ('Query is invalid: ' . mysql_error());
//write the results
while ($row = mysql_fetch_array($query)) {echo $row['CAST(date AS DATE)'] . "," . $row['EtotalDay'] . "\n";
Haw can I do this?
You should use
DATE_FORMAT(date, '%Y%m%d')
instead of CAST(), complete with an column alias name for the retrieving of the field
$query = mysql_query("SELECT DATE_FORMAT(date, '%Y%m%d') formattedDate, EtotalDay from MonthData group by 1 order by 1") or die ('Query is invalid: ' . mysql_error());
//write the results
while ($row = mysql_fetch_array($query)) {echo $row['formattedDate'] . "," . $row['EtotalDay'] . "\n";
see the manual, DATE_FORMAT
DATE_FORMAT(date,format)
Formats the date value according to the format string.
The following specifiers may be used in the format string. The “%”
character is required before format specifier characters.
%d Day of the month, numeric (00..31)
%m Month, numeric (00..12)
%Y Year, numeric, four digits
I'm importing data from a csv to a MySQL table. I need to convert the date format from String to Date.
From Starting format to finale format:
Mon Feb 04 00:00:00 UTC 2011 ---> 2011-02-04 00:00:00
I've done it sucessfully:
select str_to_date('Mon Feb 04 00:00:00 UTC 2011', '%a %b %d %k:%i:%s UTC %Y');
Now I'm writing the script to do all the import from the csv, there are 2 columns with the date to be converted, but I'm stuck with a MySQL syntax exception on the set part.
My SQL script:
load data local infile 'movimento.csv' into table movimento
fields terminated by ',' lines terminated by '\n'
(id, anno, creditore, #data_pag, #data_spost, descrizione)
set data_pagamento = str_to_date(#data_pag, '%a %b %d %k:%i:%s UTC %Y')
set data_spostamento = str_to_date(#data_spost, '%a %b %d %k:%i:%s UTC %Y')
show warnings;
I'm stuck with a syntax exception on the set part. The error:
ERROR 1064 (42000): 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 'set data_spostamento = str_to_date(#data_spost, '%a %b %d %k:%i:%s UTC %Y')
show' at line 5
>
What's the right syntax?
The syntax is incorrect. Try this one -
LOAD DATA LOCAL INFILE 'movimento.csv' INTO TABLE movimento
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
(ID, anno, creditore, #data_pag, #data_spost, descrizione)
SET
data_pagamento = STR_TO_DATE(#data_pag, '%a %b %d %k:%i:%s UTC %Y'),
data_spostamento = STR_TO_DATE(#data_spost, '%a %b %d %k:%i:%s UTC %Y')