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
Related
I am pulling the close date from a json field using:
data->>'closeDate' as PortalCloseDate however this returns a string and I am trying to convert it to a date format. The date format in the string is YYYY-MM-DD so I tried (data->>'closeDate')::date but am getting the error: "ERROR: invalid input syntax for type date: "2020-12-09"" Any help is appreciated, thanks!
Try bruteforcing on cleaning and casting the string to date:
select
cast(replace(cast(data->>'closeDate' as varchar), '"','') as date) as "closeDate"
from table
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
I am trying to update a datetime which is in String format YYYYMMDDHHMMSSZ into YYYY-MM-DD HH:MM:SS.
I was trying to run a query: Update consolidated_table SET createtimestamp_E=DATE_FORMAT(createtimestamp_E, '%Y-%m-%d %T') but getting:
Error Code: 1292`. Incorrect datetime value: ''
I tried inserting the csv file in datetime format, but that didn't work.
So I had to upload it in String format and now I'm trying to convert the string into date format, but in vein.
I was able to get this issue fixed by using below command:
SUBSTRING(#var, 1, CHAR_LENGTH(#var)-1).
I further wanted to convert this column from string to datetime for which i used the below command:
SET createtimestamp=CONVERT (SUBSTRING(#var, 1, CHAR_LENGTH(#var)-1),DATETIME)
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');
I have the following line:
command.Parameters.Add("#date", SqlDbType.DateTime, 8).Value = date_of_trip;
where date_of_trip is a string containing 25-9-2013 (i.e. day-month-year)
This gives me an error message:
System.ArgumentException: Cannot convert 25-9-2013 to System.DateTime.
Parameter name: type ---> System.FormatException: String was not
recognized as a valid DateTime.
However, if date_of_trip is 1-1-2013, it seems to work perfectly!
You should parse the string to datetime before you send it to the db.
For example:
DateTime dt;
if(DateTime.TryParse(date_of_trip, out dt))
{
command.Parameters.Add("#date", SqlDbType.DateTime, 8).Value = dt;
// ...
}
else
{
// you should use a CompareValidator to check if it's a valid date
}
If you want to ensure that your given format will work even if the current-culture of the server will change you can use DateTime.TryParseExact with format "dd-MM-yyyy":
if (DateTime.TryParseExact(date_of_trip, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt))
{
// ...
}
I guess it's because the server expects the first parameter to be a month. Try to use a real DateTime or the format yyyy-mm-dd (i.e. 2013-09-25), which should always work.