Select ALL columns in MySQL with one column inside CAST function - mysql

I basically want to select all columns of my MySQL table, but want to change the datatype of only one column namely Patient_Number using CAST function only. Here is a screenshot of my MySQL table
So as my output, I want a similar table as I have shown in the screenshot, just want to have the datatype of Patient_Number from INT to VARCHAR.
I tried executing the following queries:
select * cast(Patient_Number as varchar) from clinic_data;
select * from clinic_data cast(Patient_Number as varchar);
But only got the following error message:
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(Patient_Number as char)' at line 1

SELECT * is basically not a good idea
But you need a comma, between both statements
And it should be char for the cast
select *, cast(Patient_Number as char) from clinic_data;
db<>fiddle here

Related

I need to create a null value in SQL

I am tasked to create a column with a null value but I got an error.
SELECT * FROM test.Persons
ALTER TABLE Persons
ADD mark_percentage FLOAT
This is my error.
12:32:47 use database test ALTER TABLE PERSONS ADD MARKS float SELECT * FROM test.Persons Error Code: 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 'database test ALTER TABLE PERSONS ADD MARKS float SELECT * FROM test.Persons' at line 1 0.063 sec
Try this :
ALTER TABLE Persons
ADD mark_percentage FLOAT

How to cast a string to numeric in mysql?

I have a table with string columns. Like varchar or text.
I want to select those values as a tinyint or int. But the following fails:
SELECT CAST(example AS INT) FROM mytable
Result:
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 'INT) FROM mytable LIMIT 0, 25' at line 1
Why?
Try using signed or unsigned:
SELECT CAST(example AS SIGNED) FROM mytable
I find it very strange that MySQL does not support INT in this context (or lengths on strings).
Also, in MySQL, implicit conversion is often used:
SELECT (example + 0)

SQL Syntax Sum and Count

I have a 1064 error:
You have an error in your SQL syntax; check the manual that correspond to your MySQL server version for the right syntax to use neear '*) as NB_FR, sum (*)) as MT_FR
FROM gc_mouvements where COOPX="477" or COOPX="4' at line 4
Here is the code I used, I don't understand the origin of the error:
ALTER TABLE gc_modele_retrait ADD COLUMN
(Nb_frais_477 int(5),Nb_frais_481 int(5),Mt_frais_477 int(5),Mt_frais_481 int(5));
UPDATE gc_modele_retrait
SET Nb_frais_477=0, Nb_frais_481=0, Mt_frais_477=0, Mt_frais_481=0;
DROP TABLE IF EXISTS gc_modele_retrait_frais;
CREATE TABLE gc_modele_retrait_frais
(PRIMARY KEY (COCO))
ENGINE=myisam
SELECT COCO, COOPX, COUNT(*) AS NB_FR, SUM(*) AS MT_OPE
FROM gc_mouvements WHERE COOPX="477" OR COOPX="481" GROUP BY COCO, COOPX;
The issue is with using SUM (*), you have to pass numerical value or column containing numerical value to SUM. In your case you need:
SUM (Nb_frais_477 + Nb_frais_481)

MySQL query with enum values

I've the following structure in MySQL 5.6.19.
CREATE TABLE `metadata` (
`md5` char(32) NOT NULL,
`match` enum('none','md5','similarity') DEFAULT NULL
)
And I got an error doing a query like this:
select * from metadata where match = 'md5';
The error is:
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 '= 'md5'' at line 1
There're multiple entries in the table and rows that could match the query. But MySQL refuse to do it. Any idea about the reason?
Thanks!
MATCH is reserved keyword in MySQL: http://dev.mysql.com/doc/mysqld-version-reference/en/mysqld-version-reference-reservedwords-5-5.html. You should enclose your field name in backticks to make it work:
select * from metadata where `match` = 'md5';

Debugging MySQL CREATE TABLE from existing table statement?

Why is this giving me an error ?
I haven't found any good example code online that combines CREATE TABLE with a SELECT statement with WHERE.
CREATE TABLE tmp_year (source CHAR(3),
target CHAR(3),
val FLOAT,
PRIMARY KEY (source, target))
(SELECT source,
target,
val
WHERE date>='2001-01-01'
AND date<='2001-12-12')
FROM db;
error message:
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 'WHERE date>='2001-01-01' AND date<='2001-12-12') FROM db' at line 1
Try this my friend:
CREATE TABLE tmp_year AS
SELECT * FROM YOURTABLE
WHERE date>='2001-01-01'
AND date<='2001-12-12';
ALTER TABLE tmp_year ADD PRIMARY KEY(source, target);
Here is example in SQL Fiddle
SQL expects FROM to immediately follow a SELECT clause's values and be before conditionals like WHERE.