CONVERT or CAST field in SQL Query - mysql

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.

Related

Use convert statement in SQL : Error

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

Sql server Query in mysql

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.

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;

MySQL SELECT with fields less and greater than values

I am trying to do a select query on MySQL with phpMyAdmin or PHP with PDO.
SELECT 'uid' FROM 'clusters' WHERE 'lat'<='47.21125' AND 'lat'>='39.21125' AND 'lng'<='32.90243' AND 'lng'>='22.90243'
However, phpMyAdmin says:
#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 ''clusters' WHERE 'lat'<='47.21125' AND 'lat'>='39.21125' AND 'lng'<='32.90243' A' at line 1
What is wrong with it?
'' creates a string literal in MySQL, so your query is selecting the literal "uid" from the literal "clusters," which is invalid. Use backtics (or nothing)
SELECT Uid FROM clusters WHERE lat <= 47.21125 AND lat >= 39.21125
AND lng >= 22.90243

MySQL casting from decimal to string

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