Why doesn't the CAST work in this MySQL query?
SELECT MAX(Signups) AS Max,
MIN(Signups) AS Min,
CAST(ROUND(AVG(Signups),2) AS VARCHAR(3)) AS Avg
FROM
(
SELECT COUNT(1) AS Signups,
DATE_FORMAT(JoinDate, "%Y-%m-%d") AS Date
FROM users
GROUP BY Date
) z
Why am I getting 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(3)) AS Avg FROM ( ' at line 2
This may be due to MySQL bug #34564: CAST does not accept varchar type.
Try casting to a different type, like this:
CAST(ROUND(AVG(Signups),2) AS CHAR(3)) AS Avg
Changing VARCHAR to CHAR solves the problem
Related
In my SQL table I have "country" and "we200326" columns. Column "we200326" contains only "1" or "NULL" entries.
I'm trying to get a total of all "1"s in column "we200326" and a total by country. I have written the following statement but it gives an error but I don't know what I did wrong (I'm very new at this):
SELECT country, we200326,
(SUM(we200326) OVER () AS Total)
(SUM(we200326) OVER (PARTITION BY country) AS CountryTotal)
FROM table_name
ORDER BY CountryTotal, Country;
The error I get is this:
MySQL said: Documentation
#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 'OVER () AS Total)
(SUM(we200326) OVER (PARTITION BY country) AS CountryTotal)
' at line 2
I have searched for similar errors and found several (each time was a simple syntax error like a space or comma or so) I tried several versions but could not resolve my problem when following those instructions. Any help would be appreciated.
Window functions are available in MySQL 8.0 only.
In earlier versions, one option is to use subqueries:
select
country,
wewe200326,
(select sum(we200326) from table_name) total,
(select sum(we200326) from table_name t1 where t1.country = t.country) country_total
from table_name t
order by country_total, country
i have a table and i have columns. I want to get AGE column- it is creating (current date - year column data)- and whole columns like *.
I tried;
SELECT (YEAR(CURRENT_DATE) - YEAR(dateOfBirth)) as age , * FROM users
but it gives 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 '* FROM users LIMIT 0, 25' at line 1
What should i write? Thank you!
use alias and TIMESTAMPDIFF function
SELECT u.*,
TIMESTAMPDIFF(YEAR, dateOfBirth, CURRENT_DATE) as age
FROM users u
'Use of an unqualified * with other items in the select list may produce a parse error. To avoid this problem, use a qualified tbl_name.* reference' - https://dev.mysql.com/doc/refman/8.0/en/select.html
This is ok
SELECT (YEAR(CURRENT_DATE) - YEAR(dateOfBirth)) as age, users.* FROM users
i'm trying to run this in sql query, but not working. I tried single cast, it works fine, but when i tried to add two cast, it gives 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 'FROM useradvert WHERE optionsalerent='For sale' ORDER BY cast (rm as signed)' at line 1
This query below works.
SELECT * FROM popol WHERE optsale='For sale' ORDER BY cast(rm as signed) ASC
THis one doesn't work
SELECT * FROM popol WHERE optsale='For sale' ORDER BY cast(rm as signed) ASC, cast(salary as signed) ASC
I need to output both rm and salary rm. Both are integer values. Tqs in advance.
send me the right cast query to use on this :
SELECT
id,
device_id,
outlet_id,
duration,
current,
voltage,
kw_used,
outlet_total_kwh,
outlet_kwh_demand_15,
outlet_kw_demand_peak,
submeter_real_kw,
submeter_total_kwh,
submeter_kwh_demand_15,
submeter_kw_demand_peak,
peak_voltage,
peak_current,
demand,
inst_demand,
hist_peek_demand,
power_factor,
crest_factor,
frequency,
app_power,
tot_app_energy,
tot_har_dist_vol,
tot_har_dist_curr,
har_x_dist_v,
har_y_dist_v,
har_z_dist_v,
har_x_dist_c,
har_y_dist_c,
har_z_dist_c,
INTERVAL,
CAST (duration AS DATE)
FROM
epowerg1.dru;
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 ' CAST (duration as DATE) from epowerg1.dru' at line 1
Remove the space between cast and (
CAST(duration as DATE)
^---------------------------here
You can convert string to date. By using below SQL.
SELECT STR_TO_DATE('21,5,2013','%d,%m,%Y');
I don't know what is the format of your duration column.
Thank You.
create table x
(studentname varchar(50),
dt timestamp);
you can use "cast" operator to convert any datatype into another datatype.
answer is below :
select studentname,dt,cast(dt as date) from x;
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.