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;
Related
I am receiving an error as I try to convert my datetime column called rent_date to mm/dd/yyyy format.
I have consulted the relevant documentation but still receive error.
SQL statement:
SELECT listing.title , listing.author, listing.isbn, listing.cond , listing.additional_information, CAST(rented_listings.due_date as date), CONVERT(date, rented_listings.rent_date, 101)
FROM `listing`
INNER JOIN `rented_listings` ON `listing`.`upload_id` = `rented_listings`.`upload_id`
WHERE `availability`= 'R' ORDER BY `rented_listings`.`due_date` ASC , `rented_listings`.`rent_date` DESC
Error is within this part of the query
CONVERT(date, rented_listings.rent_date, 101) which makes sense because when I take it out the query works.
How do I convert 'rented_listing.rent_date' to mm/dd/yyyy?
EDIT:
Specific error included:
#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
'rented_listings.rent_date, 101)
FROM listing
INNER JOIN`' at line 1
I think you should do like this in mysql
DATE_FORMAT(cast(rented_listings.rent_date as date), '%m/%d/%Y');
which will first convert it to the date and then to the specific format
I was having some problem when trying to convert varchar field in MySQL to integer using SQL statement:
SELECT mrtpopTime, CONVERT(INT,mrtpopAmt)
FROM tm_mrtpop
WHERE mrtpopName = ''
ORDER BY CONVERT(INT, mrtpopAmt) DESC
I am trying to get the top 3 records when mrtpopAmt was arranged in a way where it is in reverse order. However, I am getting error message at the INT and the error message is:
Syntax error, Unecpected INT_SYM
I wonder why is it so? Thanks in advance.
This is because MySQL doesn't use CONVERT() for casting. It uses the CAST function. In your case you would use:
CAST(mrtpopAmt AS SIGNED) -- This can also be UNSIGNED if it will always be a positive integer
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.
This works
SELECT EntryId FROM 2_1_journal
WHERE CAST(CONCAT_WS('-', RecordYear,RecordMonth,RecordDay) AS DATE) = ?
But want before WHERE to define "virtual" column name for date with column name for example RecordDate.
Tried
SELECT EntryId FROM 2_1_journal
CAST(CONCAT_WS('-', RecordYear,RecordMonth,RecordDay) AS DATE) RecordDate
WHERE RecordDate = ?
SELECT EntryId FROM 2_1_journal
CAST(CONCAT_WS('-', RecordYear,RecordMonth,RecordDay) AS DATE) AS RecordDate
WHERE RecordDate = ?
SELECT EntryId FROM 2_1_journal
CAST((CONCAT_WS('-', RecordYear,RecordMonth,RecordDay) AS DATE) AS RecordDate)
WHERE RecordDate = ?
In all cases get error 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 'CAST.
Please advice what is correct synatax
http://dev.mysql.com/doc/refman/5.5/en/select.html:
βIt is not permissible to refer to a column alias in a WHERE clause, because the column value might not yet be determined when the WHERE clause is executed. See Section C.5.5.4, Problems with Column Aliases.β
If you want to select the result of an operation in the WHERE clause as a column value as well, then you will have to write that operation two times β once in the WHERE clause, without an alias, and in the column list after SELECT as well.
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