Jow to use str_to_date - mysql

I need to use str_to_date function in MySql and return the correct date. Below is my try and and I am getting syntax error.
SELECT STR_TO_DATE('011122143859',%d%c%Y%l%i%S);
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 '%d%c%Y%l%i%S)' at line 1.
I need to return the timestamp value as 2022-11-01 14:38:59
Can someone show me what would be the correct syntax. I followed this resource.
https://database.guide/str_to_date-examples-mysql/
Update:
I found the correct syntax as below.
STR_TO_DATE('011122143859','%d%m%y%H%i%S')

SELECT STR_TO_DATE("2017,8,14 10,40,10", "%Y,%m,%d %h,%i,%s");
OR
SELECT STR_TO_DATE("August,5,2017", "%M %e %Y");

Related

SQLSTATE[42000]: Syntax error or access violation: 1064. Cant find error in syntax

The following SQL statement worked on my job computer but not on my home one;
select
YEAR(FROM_UNIXTIME(UNIX_TIMESTAMP(time_created))) as year,
MONTH(FROM_UNIXTIME(UNIX_TIMESTAMP(time_created))) as month,
COUNT(id) as num
FROM orders
GROUP BY
YEAR(FROM_UNIXTIME(UNIX_TIMESTAMP(time_created))),
MONTH(FROM_UNIXTIME(UNIX_TIMESTAMP(time_created))) ASC
;
I fails with the error below;
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 'ASC' at line 1
What am I doing wrong and how can I fix it?
Mysql removed the asc and desc modifiers from the group by clause in v8.0. Your code was probabĺy created for an earlier version of mysql. If you check the mysql v5.7 documentation for the select syntax, the modifiers are still there.
You either need to remove the asc modifier or ensure that your code runs on a mysql version that still supports this synax.

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL serve

I am trying to execute the following code but not sure where i am going wrong. Mysql workbench is throwing error for the following code
Insert into abc(Date,Close_Price,Signal)
Select
Date,
close_price,
case
when dayma_20 > dayma_50 then 'BUY'
when dayma_20 < dayma_50 then 'SELL'
else 'HOLD'
end as Signal
from stock;
It is giving error as follows:
Error Code: 1064. 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 'Signal) Select date,close_price, case when dayma_20 > dayma_50 then 'BUY' when d' at line 1
SIGNAL is a MySQL reserved word. You need to enclose it between backticks (which, by the way, makes it case-sensitive, meaning that you must use the same case as the one that was used when the table was created).
insert into abc(Date,Close_Price,`Signal`) select ...
A better option would be rename that column to something that is not reserved. Using reserved words as object names in a schema is tedious and error prone.
Side note: DATE is also a reserved word.

Syntax Error or access violation - SQL query & Laravel

In my query, i am trying to compare today's date only (without time) to my field created_at but i get the error below
SQLSTATE[42000]: Syntax error or access violation: 1064 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 '2018-08-29 DATE(created_at)' at line 1
Controller
$item_shipped = Item::where('id',Auth::user()->id)->whereRaw('DATE(created_at)' , '=', Carbon::now()->toDateString())->get();
return $item_shipped;
How do i solve this problem ?
whereRaw doesn't accept the operator parameter. You have to write the whole condition and it takes the second parameter which is an array of bindings.
You need this:
->whereRaw('DATE(created_at) = ?', [Carbon::now()->toDateString()])->get();

Issues with the mysql query

i am trying to get the result from thquery, but i am getting error on the line 2, where i am using the cast function:
SELECT ticketstickets.`ID`, ticketsusers.`fname`, ticketsusers.`lname`, ticketstickets.`subject`,
ticketstickets.`created`, CAST(ticketstickets.modified AS VARCHAR(100)) as modified,
ticketstickets.`priority`, ticketstickets.`status`,
ticketsdepartments.`name` FROM ticketsusers, ticketstickets
LEFT JOIN ticketsdepartments ON ticketstickets.`DEPARTMENT_ID`= ticketsdepartments.`ID`
WHERE ticketstickets.parent = 0
AND ticketstickets.by=ticketsusers.ID
I tried with convert function too, but same error: [Err] 1064 - 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
'VARCHAR(100)) as modified,
ticketstickets.priority, ticketstickets.`s' at line 2
Please see in the manual that a value cannot be casted to varchar(N). Instead you should cast to char(N) (so without "var").
use that instead
CAST(ticketstickets.modified AS CHAR(100))
Probably the issue is in your CAST statement.
Refer this post to correct your query:
How to use the CAST function correctly in a MySql SELECT statement?

Error #1064 SQL when try to convert to VARCHAR with thousand commas

I got this error:
"#1064 - 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 'varchar,convert(Money, donate_amt),1),'.00','')' at line 1"
when try to convert all the Number of column: donate_amt, table" "corp_donate". Can help me how to fix it. Below is the code I tried:
select replace(convert(varchar,convert(Money, `donate_amt`),1),'.00','')
select replace(convert(varchar,cast(donate_amt as money),1), '.00','')
try this....