cast in mysql query - mysql

I am converting my SQL database to MYSQL and I have problem using cast in mysql, please help me to convert this SQL query to mysql
Here col1 is type of varchar (storing values(01,02,03,04))
select * from table1 order by cast(col1 as numeric)

Try using the data type SIGNED instead:
select *
from table1
order by cast(col1 as SIGNED)
SQL Fiddle Demo

Related

What's the equivalent syntax in MySQL for `with xxx AS ()`

In MSSQL, I have a query something like:
WITH temp AS (
SELECT
*
FROM
xx.xxxx
)
But WITH temp AS () isn't supported in MySQL syntax, I wonder what the equivalent syntax should be in MySQL Workbench? Thanks.
Below is a screenshot of the syntax error:
If you have MySQL 5.x and you need to adapt the query which contains CTE, then convert
WITH cte AS (cte query text)
SELECT ...
FROM cte
...
to
SELECT ...
FROM (cte query text) cte
...
If there is more than one CTE then perform this substitution with accuracy from latter CTE to former one (you may need to use multiple copies of some CTE subquery text - this is a norma).

Mysql: Using str_to_date on a select query returns nothing

I've following Mysql query:
select str_to_date((select distinct cast(substr(tb2.sub1,1,4) AS CHAR) as year from (
SELECT SUBSTRING_INDEX(file_name,'_',-1) as sub1 from table2) as tb2) , '%Y')
And it is correct because mysqlworkbench returns green flag but no output.
Could you help me?
The expression
select str_to_date('2007', '%Y')
returns 2007-00-00. Some MySQL servers are set to disallow invalid dates. Try using
select makedate('2007', 1)
instead. That will give you the valid date of 2007-01-01.
I'm leaving it to you to edit your query to make the change.

How to do mysql SELECT json

Is there a way to select json within mysql? Or do I need to use concatenation to build it? I need to do a large json update statement and I'd rather do it within the db so I don't have to iterate of tens of millions of rows. Is there a better approach than:
SELECT CONCAT('{"field1: "', field1, '"}') FROM mytable
mysql Server (since Version 8.0): SELECT [field with json blob]->>"$.json_field" FROM mytable; see
mariaDB Server (since Version 10.26) SELECT JSON_EXTRACT([field with json blob], "$.json_field") from mytable see

My sql query not work with math operators

Where is a problem because I try count values using sql queries:
(SELECT quantity FROM db WHERE no='998') this is fine
but (('500') - (SELECT quantity FROM db WHERE no='998')) // incorrect syntax near -
But I need to use constant 500. Where is problem
SELECT 500-quantity FROM db WHERE no='998'
How about this?
SELECT 500 - quantity
FROM db
WHERE no = 998;
A select statement needs to start with a select. In addition, numeric constants should not use single quotes (although that has no effect on whether the query parses or runs).
Use:
SELECT 500 - quantity FROM db WHERE no='998'
-- or if the no in the where clause is an integer and not a string:
-- SELECT 500 - quantity FROM db WHERE no=998
Or if you have to use a constant string literal:
SELECT '500' - quantity FROM db WHERE no='998'
in this case MySQL would implicitly convert it to a suitable integer anyway.
SELECT 500 - COUNT(quantity)
FROM db
WHERE no='998';
SQL query always begin by SELECT keyword.

How to convert string into a decimal?

I have two strings "0.31" and "0.0076" and they need to be stored in a decimal(10,2) column in MySQL. How do I do this conversion in ruby but not in mysql directly
try using CAST
SELECT CAST(colName AS DECIMAL(10,2))
FROM tableName
SQLFiddle Demo
use MySql conversion functions CAST or CONVERT . Read Here
Select CAST(columnName as DECIMAL(10,2))
or
Select CONVERT(columnName,DECIMAL(10,2))
OR you can do the job like
select format(columnname, 0) as formated from tablename where condition