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'
Related
Currently I have the following conditional inside my SELECT statement:
IF ((`date1` IS NOT NULL AND `date1` <> 0), DATE_FORMAT(`date1`, "%M %D %Y"),
DATE_FORMAT(`date2`, "%M %D %Y")) AS finaldate
But under certain conditions instead of presenting a date for finaldate the outcome of the conditional is NULL. Is it possible to wrap this conditional inside a ISNULL conditional?
If you want to convert a NULL to another value you can use COALESCE(). For example:
COALESCE(<my_expression>, ' ')
In this case, if the value of <my_expression> is null, it will be rendered as a space.
In your case this could become something like:
COALESCE(
IF ((`date1` IS NOT NULL AND `date1` <> 0), DATE_FORMAT(`date1`, "%M %D %Y"),
DATE_FORMAT(`date2`, "%M %D %Y"),
' '
) AS finaldate
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)
)
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'))
I'm trying to format the results of this query:
CREATE OR REPLACE FUNCTION "alarmEventList"(sampleid integer
, starttime timestamp without time zone
, stoptime timestamp without time zone)
RETURNS text[] AS
$BODY$DECLARE
result text[];
BEGIN
select into result array_agg(res::text)
from (
select to_char("Timestamp", 'YYYY-MM-DD HH24:MI:SS')
,"AlertLevel"
,"Timestamp" - lag("Timestamp") over (order by "Timestamp")
from "Judgements"
WHERE "SampleID" = sampleid
and "Timestamp" >= starttime
and "Timestamp" <= stoptime
) res
where "AlertLevel" > 0;
select into result array_to_string(result,',');
return result;
END
$BODY$
LANGUAGE plpgsql VOLATILE
Right now without array_to_string() I get something like this:
{"(\"2013-10-16 15:10:40\",1,00:00:00)","(\"2013-10-16 15:11:52\",1,00:00:48)"}
and I want something like this:
2013-10-16 15:10:40,1,00:00:00 | 2013-10-16 15:11:52,1,00:00:48 |
But when I run the query I get error:
array value must start with "{" or dimension information
You do not actually want an array type, but a string representation.
Can be achieved like this:
CREATE OR REPLACE FUNCTION "alarmEventList"(sampleid integer
, starttime timestamp
, stoptime timestamp
, OUT result text) AS
$func$
BEGIN
SELECT INTO result string_agg(concat_ws(','
,to_char("Timestamp", 'YYYY-MM-DD HH24:MI:SS')
,"AlertLevel"
,"Timestamp" - ts_lag)
, ' | ')
FROM (
SELECT "Timestamp"
,"AlertLevel"
,lag("Timestamp") OVER (ORDER BY "Timestamp") AS ts_lag
FROM "Judgements"
WHERE "SampleID" = sampleid
AND "Timestamp" >= starttime
AND "Timestamp" <= stoptime
) res
WHERE "AlertLevel" > 0;
END
$func$ LANGUAGE plpgsql
The manual on string_agg() and concat_ws().
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')