I have a query which work fine with sql server but error in mysql
this is the error
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Incorrect parameter count in the call to native function 'DATE_FORMAT'
And this is my query:
String sql = "SELECT SUM(Pay) AS Pay, ClinicId, DATE_FORMAT
(DATENAME(MONTH, Date)) as Months "
+"FROM Prescription GROUP BY DATE_FORMAT (DATENAME(MONTH,
Date)), ClinicId HAVING ClinicId = '"+cid+"'";
Had you checked out the MySQL documentation on date_format() function, you would have noticed that it requires 2 parameters, not just one:
DATE_FORMAT(date,format)
Formats the date value according to the format string.
You probably need the date_format(Date, '%M') expression. The linked documentation contains all the formatting options, so you can play around with it to tailor the output to your needs.
Related
I took over an old SQL query that includes the following expression:
SELECT
g.Citystate,
g.Cityorder,
if(CityPostal is null, CityName, concat(CityName, ', ', CityPostal)) as CityName,
g.CityName as CityAdress,
g.CityKey,
#Published = ifnull(if (g.CityKey='S',
(SELECT sum(if(FROM_UNIXTIME(g1.Timestamp/1000) >= STR_TO_DATE('$VARIANT$', '%e.%m.%Y'), g1.Published, 0))
from PUBLISHED_DATA g1
If I execute the whole SQL Statement in DBeaver I get the following error:
Incorrect datetime value: '$VARIANT$' for function str_to_date
I think the error occures because the syntax did not work with the newest MySQl version.
The query works if I pass a static string like "01.01.1971" as first argument to the STR_TO_DATE function. I'm searching for how to set up dynamic variable names, but I didn't find any solution. What does $VARIANT$ exactly mean? Does it define a parameter?
I've view named Sales View
select * from SalesView;
I want to fetch records from SalesView having order_date 9-OCT-2019.
Value for Order date is supplied from GUI in format DD-MM-YYYY.
I tried,
select *
from salesView
where str_to_date(order_date,'%d-%m-%Y') = str_to_date('09-10-2019','%d-%m-%Y')
order by oID;
I've also tried date_format() instead of str_to_date(), but It is returning nothing.
Check your where clause
where str_to_date(order_date,'%d-%m-%Y') = str_to_date('09-10-2019','%d-%m-%Y')
Replace order_date's format with the format in which database stored it. YYYY-MM-DD
where str_to_date(order_date,'%Y-%m-%d') = str_to_date('09-10-2019','%d-%m-%Y')
I am using DBeaver. Here is my piece of code:
select convert(varchar(10), updated_at, 101)
from rewards.mission_actions
limit 30
This is the error:
[42601]: ERROR: syntax error at or near ","
Because the data is too specific to extend of hour and minute so I want to change it. Not extract details out of it.
Please help or at least give me another solution.
The CONVERT function is for SQL Server, not MySQL. The closest analog in MySQL would be DATE_FORMAT:
SELECT DATE_FORMAT(updated_at, '%m/%d/%Y')
FROM rewards.missions_actions
-- ORDER BY <something>
LIMIT 30
I have a TimeStamp field in a MySQL database that I'm trying to pull data from. I'm trying to get it as a string, so I've been using the following query:
select CONVERT(VARCHAR, date_created, 120) from junk;
It throws 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 'VARCHAR, date_modified, 120) from junk limit 10' at line 1
Can someone please tell me what I'm doing wrong?
If you wanted to format explicitly to yyyymmdd (style 120 in sql server)
Select DATE_FORMAT(somedate, '%Y%m%d') From SomeTable
CONVERT() in MySQL is used to convert between different character sets, you need DATE_FORMAT():
SELECT DATE_FORMAT(date_created, '%Y%m%d %H%i%S')
FROM Junk
Update: Originally had CAST() incorrectly using VARCHAR(), but CAST() will also work:
SELECT CAST(date_created AS CHAR(10))
FROM Junk
DATE_FORMAT() options
In MySql, the CONVERT() function is used to convert existing string types (like varchar) between character sets (like utf-8 or ascii). To format a TimeStamp column, use the FROM_UNIXTIME() function.
I am getting this error while I am trying to execute a simple SELECT statement in Toad
MySql.Data.Types.MySqlConversionException
Unable to convert MySQL date/time value to System.DateTime
What could be wrong?
That could mean one of these two common issues:
1) Zero dates, which are 0000-00-00 in MySQL. MySQL allows you to store them to mark 0 dates, you can even use 0001-01-01, but not all drivers or downstream programs can handle them. Add to the connection string
Allow Zero Datetime=true;
The other choice is explicitly removing them, something like
SELECT IF(DateCol='0000-00-00' OR DateCol<'1970-01-01', NULL, DateCol) as DateCol,
Othercol1, ID ....
FROM TBL
2) Date formatting. For some driver/program combination, the dates are handled as strings. Explicit conversion is necessary:
SELECT DATE_FORMAT(DateCol, '%m/%d/%Y') as DateCol,
Othercol1, ID ....
FROM TBL