Use convert statement in SQL : Error - mysql

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

Related

SQL Convert 'mm/dd/yy' string to date format

I have table named templog and I have a date column named 'tdate' which stores a string in the 'mm/dd/yy' format. I tried to convert using the following syntax but I receive an error.
SELECT convert(datetime,tdate,110) from templog
SQL query: Documentation
SELECT convert(datetime,tdate,110) from templog LIMIT 0, 25
MySQL said: Documentation
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'tdate,110) from templog LIMIT 0, 25' at line 1
Any ideas on what I'm doing wrong?
For MariaDB, you want str_to_date():
SELECT str_to_date(tdate, '%m/%d/%y')
FROM templog

MySQL Getting the return of a substraction

We are trying to get the length of a physical exercise by using the timestamps on our sensor data.
We currently have the following query:
SELECT UNIX_TIMESTAMP(
SELECT HAAS2.trainingsdata.timestamp
FROM HAAS2.trainingdata
WHERE HAAS2.trainingsdata.training_id= 1
ORDER BY timestamp DESC LIMIT 1)
- UNIX_TIMESTAMP(
SELECT HAAS2.trainingsdata.timestamp
FROM HAAS2.trainingdata
WHERE HAAS2.trainingsdata.training_id= 1
ORDER BY timestamp ASC LIMIT 1)
AS output
(enters added for readability)
When testing this query in phpMyAdmin we get the following 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 'SELECT HAAS2.trainingsdata.timestamp FROM HAAS2.trainingdata
WHERE HAAS2.trainin' at line 1
We've tried different ways to write down the query all resulting in the same error. We don't understand where the syntax error lies.
SELECT max(UNIX_TIMESTAMP(timestamp)) -
min(UNIX_TIMESTAMP(timestamp)) AS output
FROM HAAS2.trainingdata
WHERE training_id = 1

sql query order by multiple CAST value int

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.

how to change timestamp into date in mysql 5.5?

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;

CONVERT or CAST field in SQL Query

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.