multiply two varchars in sql - sql

This what I have tried but it is giving errors:
MariaDB [test]> create table table1 (length varchar, breadth varchar);
ERROR 1064 (42000): 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 ' breadth varchar)' at line 1
MariaDB [test]> create table table1 (length varchar(20), breadth varchar(20));
Query OK, 0 rows affected (0.06 sec)
MariaDB [test]> insert into table1 (length, breadth) values ('12','11');
Query OK, 1 row affected (0.05 sec)
MariaDB [test]> select * from table1;
+--------+---------+
| length | breadth |
+--------+---------+
| 12 | 11 |
+--------+---------+
1 row in set (0.02 sec)
MariaDB [test]> select convert (int, length)*convert(int, breadth) as T from table1;
ERROR 1064 (42000): 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 'int, length)*convert(int, breadth) as T fr
om table1' at line 1
MariaDB [test]> select convert(int, length)*convert(int, breadth) as T from table1;
ERROR 1064 (42000): 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 'int, length)*convert(int, breadth) as T fr
om table1' at line 1

Use cast():
select cast(length as int) * cast(breadth as int) as T
from table1;
As explained in the documentation, convert() is for converting between different character sets. You are confusing MySQL's (and MariaDB's) use of convert() with SQL Server's.
By the way, you do not even need the explicit cast() (at least in MySQL). The engine will do an implicit cast for you:
select length * breadth as T from table1;
Although this is supported, I don't really advocate depending on implicit casts.
Here is a SQL Fiddle.

Related

MariaDB: subselect in COALESCE throws ERROR 1064 (42000)

Intention: I want to subselect values within a coalesce function in MariaDB (10.7.1).
Problem:
Executing this SQL statement ...
select coalesce(select id from MY_TABLE limit 1);
... throws this error message:
ERROR 1064 (42000): 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 'select id from MY_TABLE limit 1)' at line 1
Workarounds / checks I tried:
select id from MY_TABLE limit 1; returns a valid value. So that part of the statement is fine.
select coalesce('a'); returns the value a. So executing coalesce with one parameter only is also fine.
You must make the result of select to be an expression. Statement is not an expression
with MY_TABLE as (
select 1 as id
)
select coalesce((select id from MY_TABLE limit 1)) c;
Note the result of LIMIT without ORDER BY is an arbitrary row.
The COALESCE function typically takes 2 or more arguments. The value returned is the first value which is not NULL. You probably intend something like this:
SELECT COALESCE(id, 'missing') AS id
FROM MY_TABLE
ORDER BY <some column>
LIMIT 1;

MariaDB does not accept specific date as 'default on update' value

Using MariaDB 10.5.8.
The following commands work :
CREATE TABLE t1 (
dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE t1 (
dt DATETIME DEFAULT '2100-01-01 00:00:000' ON UPDATE CURRENT_TIMESTAMP
);
But the following command does not work :
CREATE TABLE t1 (
dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE '2100-01-01 00:00:000'
);
It returns ERROR 1064 (42000): 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 '`2100-01-01 00:00:000` )' at line 1.
The documentation is really clear on what syntax is allowed:
[ON UPDATE [NOW | CURRENT_TIMESTAMP] [(precision)]]
This does not include putting in a custom value.
To be honest, I'm not sure if the designers would have even considered a constant value for such a column. After all, you can just have two columns and check an update if updatedAt <> createdAt.

SQL - how to create a new table?

I am self-studying how to write SQL. I am following the documentation here.
SQL 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 ')' at line 1
How can I fix this?
Can you add what SQL you have tried? Otherwise, this is how you create a new table:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
)

Mariadb SQL syntax error while creating table

Manually creating a table while in mariadb console;
got the following error:
MariaDB [mysql]> CREATE TABLE learningdb(command varchar(255) text2disp varchar(255));
ERROR 1064 (42000): 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 'varchar(255))' at line 1
MariaDB [mysql]> CREATE TABLE learningdb(command varchar(2048) text2disp varchar(2048));
ERROR 1064 (42000): 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 'varchar(2048))' at line 1
Cannot find out the correct syntax.
Dont you think a comma separator is required between each of type definition!?
Example:
CREATE TABLE learningdb(command varchar(255), -- <--- this comma was missing
text2disp varchar(255));
Refer to Documentation:
Create Table - Syntax
CREATE [OR REPLACE] [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
(create_definition,...) ...
Tutorial Point: MariaDB - Create Tables
... Multiple columns separated by commas ...

mysql error 1064 when inserting

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 '(country,ping,order) VALUES (China,1,1)' at line 1
this is my code
INSERT INTO
(country, ping, order)
VALUES
('China', '1', '1');
You're missing the Table Name. Try:
INSERT INTO MYTABLENAME (country,ping,order) VALUES ('China','1','1');
are ping and order text fields or numeric? if numeric remove the ticks from the 1's
INSERT INTO Tablename (country,ping,order) VALUES ('China',1,1)
could also be reserved word try:
INSERT INTO Tablename (country,`ping`,`order`) VALUES ('China',1,1)
Your insert statement is missing the table name:
INSERT INTO tbl_name (col_name,...) VALUES (expr,...)
you are missing table name. also make sure that those quotes are necessary