Can't execute string to date conversion - mysql

Could anyone point what to read, because I'm stuck with trivial issue. I store data in JSON format and I have a key with string that consists of a non ISO format datetime value, e.g. '%d-%m-%Y %H:%i:%S'
I've tried function STR_TO_DATE on string "17-12-2019 13:49:50" and it returned SQL datetime result, but when I use it in function
DELETE FROM queue WHERE
ts = CASE
WHEN type = 'entry' THEN HOUR(TIMEDIFF (NOW(), STR_TO_DATE(JSON_EXTRACT(request, "$.date_event"),'%d-%m-%Y %H:%i:%S'))) > 72
SQL raises
Incorrect datetime value: '"07-12-2016 18:21:59"' for function
str_to_date
When I try statement SELECT STR_TO_DATE(JSON_EXTRACT(request, "$.date_event"),'%d-%m-%Y %H:%i:%S') MySQL returns null

Your issue is caused by the double quotes returned around the extracted value. You need to either unquote the extracted value e.g.
JSON_UNQUOTE(JSON_EXTRACT(request, "$.date_event"))
or using shorthand notation
request->>'$.date_event'
Or add the double quotes into the STR_TO_DATE template i.e.
STR_TO_DATE(JSON_EXTRACT(request, "$.date_event"),'"%d-%m-%Y %H:%i:%S"'))
Demo on dbfiddle

Related

to_timestamp() passing the json data which is int and has unix timestamp not working in postgresql

info{
...
created_at:'1667801192'
}
select to_timestamp(info->'created_at') from booking;
info column has unix timestamp
expecting the whole column to convert as date
SQL is a typed language; every expression has a data type. The type of info->'created_at' is json or jsonb (depending on the type of info), and there is no function to_timestamp that accepts these data types as input. You will have to perform a type cast:
to_timestamp(CAST (info ->> 'created_at' AS double precision))

Failed to conver string to timestamp bigQuery

I am trying to convert string to timestamp but haven't been successful.
My field (payment_at) string contains
01-10-2021 9:04:06
01-10-2021 11:48:19
01-10-2021 16:01:35
I tried convert using
PARSE_TIMESTAMP("%Y-%m-%d %H:%M", payment_at)
and
CAST(payment_at AS TIMESTAMP)
but it shows error
Error running query; Invalid timestamp
Please help if I am doing anything wrong
Thankyou!
The format_string your provided does not match the timestamp string input.
Replace "%Y-%m-%d %H:%M" with "%d-%m-%Y %H:%M:%S"
Documentation for the parse_timestamp function:
https://cloud.google.com/bigquery/docs/reference/standard-sql/timestamp_functions#parse_timestamp
Documentation for supported format elements:
https://cloud.google.com/bigquery/docs/reference/standard-sql/timestamp_functions#supported_format_elements_for_timestamp
Try this one:
SELECT PARSE_TIMESTAMP("%d-%m-%Y %H:%M:%S", "01-10-2021 11:48:19")
Documentation

Neo4j Date conversion and comparison?

I was working with MS access database.MY datetime is now like "05-03-2016 14:55:20" .I need to convert it into datetime format in neo4j.1.How to do it ? 2.After conversion I need to use date filter i.e I want to find all nodes created between 2 dates.How to do it ?
If Neo4j is pulling data from Access query, construct field in query that calculates date to a string that Neo4j can then convert to date with its DateTime function.
Format([fieldname], "yyyy/mm/dd hh:nn:ss")
You can use the APOC function apoc.date.parse() to convert your MS Access datetime string into the number of seconds from the UNIX epoch, and then construct a neo4j datetime from that value. For example, this will return a neo4j datetime that represents your sample time string:
RETURN datetime({
epochSeconds: apoc.date.parse('05-03-2016 14:55:20', 's', 'MM-dd-yyyy HH:mm:ss')
})
A neo4j temporal type can only be compared directly to the same type. For instance, to compare a datetime X to a date Y, you can convert X to a date before doing the comparison. The following sample query will return true:
WITH
datetime({
epochSeconds: apoc.date.parse('05-03-2016 14:55:20', 's', 'MM-dd-yyyy HH:mm:ss')
}) AS X,
date('2016-05-04') AS Y
RETURN date({date: X}) <= Y

Retreive date from MySQL DATETIME in Doctrine QueryBuilder

I have a MySQL date stored in DATETIME format. So I would like to know how to use date() in my Doctrine QueryBuilder's where clause. For example, 2013-02-01 12:51:17 is the date in MySQL. But I need to retrieve only the date. This is what I have tried:
$qb = $this->getEntityManager()->createQueryBuilder()
->select('t.balance','a.id','t.date')
->from('TestMainBundle:Transaction','t')
->groupBy('a.id')
->orderBy('a.id')
->where("t.date in date('t.date') ");
return $qb->getQuery()->getResult();
I received the following error:
QueryException: [Syntax Error]: Error: Expected Doctrine\ORM\Query\Lexer::T_OPEN_PARENTHESIS, got 'date'
Hi You can use SUBSTRING to fix your probleme
->where('SUBSTRING(t.date, 1, 10) IN (:param)')
->setParameter('param', array('2017-04-06'))
As it is pointed out in the comments, you cannot use mysql-specific date() or date_format() functions in Doctrine.
But for the particular case of searching a certain date in the datetime field, you can treat a date like a string, and thus use LIKE operator
->Where('t.date LIKE :date')
->setParameter(':date', "$date%")
As of
But I need to retreive only the date
you just format the returned value using format("Y-m-d") method. i.e.
echo $row->getDate()->format("Y-m-d");
You don't need the "date" in your where clause.
Juste remove it like this :
->where('t.date in (:yourwanteddate)')
->setParameter('yourwanteddate', '2013-02-01 12:51:17');

Selecting DateTime from a query

I have the following linq to sql query:
DateTime linqdate = (from de in dvlist
where de.DataValue == value
select de.DateTime);
I want to get the date value form database in a datetime variable but I got the following error:
cannot implicitly convert type
'System.Collections.Generic.IEnumerable'
to 'System.DateTime'
any ideas where the problem is? thanks in advance
A Linq query returns an IEnumerable<T> that you can iterate or you can convert it to another type of object (using some extension methods)
In order to get what you want you should do something like this :
var dateTime=(from de in dvlist
where de.DataValue == value
select de.DateTime).FirstOrDefault();
this way you are returning the first element of your enumerable object, or the default value for that type (T, in this case DateTime) if there is no match in the query.