compatibility Query for to_number() - mysql

In this query using to_number() in oracle. How to write compatibility query for oracle and mysql databases.
SELECT col1 FROM table WHERE condition ORDER BY TO_NUMBER(col2);
Here col2 is varchar2 datatype. Suppose i was used ORDER BY command in this query must use converting function i.e to_number(col2),this function not available in mysql.so please give correct solution for above problem

Create a custom function in your mysql db with name to_number which takes same parameter and returns integer .
You can then use cast function inside your custom function
DELIMITER $$
DROP FUNCTION IF EXISTS to_number$$
CREATE FUNCTION to_number (number VARCHAR(10)) RETURNS INT (11)
BEGIN
RETURN (CAST(number AS SIGNED));
END$$
DELIMITER ;
This will create a custom/userdefined function with to_number as name
Then you can use your query both in oracle and mysql

You can do it with CAST
select id,num type, details,CAST(num AS SIGNED) as T
from demo order by T
SQL Fiddle link
For more info : http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html

Related

Select statement defined by parameter SQL

In SQL is there a way to grab the information in a table, but with the table name being specified by a function parameter?
Obviously the following doesn't work, but something along these lines maybe:
CREATE OR REPLACE FUNCTION select_table(table_name TEXT)
RETURNS TABLE (
"ID" TEXT
) AS
$$
SELECT * FROM uploads.<table_name>
$$
LANGUAGE SQL STABLE;
I'm a bit of a rookie when it comes to SQL, so would appreciate any guidance.
Any use of a variable in a query will be as if you had used a string literal, not an identifier.
To use a variable as an identifier, you would have to use dynamic SQL. That is, do string-concatenation of the table_name variable into a string which is your SELECT query, then PREPARE and EXECUTE that query.
But https://dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html says:
SQL syntax for prepared statements can be used within stored procedures, but not in stored functions or triggers.
This is because if a stored function is running, then by definition, your thread is already running a query. MySQL cannot do that.
You can write your function to do a large CASE statement to do a different fixed query depending on the input variable.
But as P.Salmon comments above, in MySQL you can't return a table from a stored function. Functions can only return a single scalar value, not a result set. So your SELECT would need to query one column and use INTO syntax to save it to a variable. Then return that variable.
Also current versions of MySQL have no "CREATE OR REPLACE FUNCTION" option. You can "CREATE FUNCTION."
DELIMITER //
CREATE FUNCTION select_table(table_name TEXT)
RETURNS TEXT READS SQL DATA
BEGIN
DECLARE result TEXT;
CASE table_name
WHEN 'mytable1' THEN SELECT col1 INTO result FROM mytable1;
WHEN 'mytable2' THEN SELECT col1 INTO result FROM mytable2;
WHEN 'mytable3' THEN SELECT col1 INTO result FROM mytable3;
END CASE;
RETURN result;
END//
DELIMITER ;

Can I see columns related to particular datatypes in oracle using query?

I am 90% sure that this is not possible but I need expert advice.
Is this possible to filter out columns on the basis of datatypes using any method in select list in oracle or in any other DBMS.
eg.
select datatype('DATE') from my_table;
To see all the columns in result having datatype DATE. I know above query is not possible according to me(specially the datatype function :) ) but acknowledge if it is.
In Oracle, You may not directly run a query containing specific datatypes. However, you can create a procedure to construct a query with table name and datatype as arguments to contain specific columns and return it as a REF CURSOR.
CREATE OR REPLACE PROCEDURE proc_getsql(
p_table_name IN VARCHAR2,
p_datatype IN VARCHAR2 ,
p_query OUT SYS_REFCURSOR )
IS
v_query VARCHAR2(1000) ;
BEGIN
OPEN p_query FOR
SELECT 'SELECT '
|| LISTAGG(COLUMN_NAME,',') WITHIN GROUP (
ORDER BY column_name )
||' FROM '||p_table_name query_to_run
FROM USER_TAB_COLUMNS
WHERE TABLE_NAME = p_table_name
AND DATA_TYPE LIKE '%'
||p_datatype
||'%';
END;
You may then call or fetch from this CURSOR depending on your requirement.
VAR x REFCURSOR;
EXEC proc_getsql('EMPLOYEES','DATE', :x);
PRINT :x;
Output
QUERY_TO_RUN
------------
SELECT HIRE_DATE,DATE_OF_BIRTH FROM EMPLOYEES

How to use a procedure into the select and insert?

I'm work with store procedures, supose that I've the following procedure that return a value, and this value I use in other query.
CREATE PROCEUDRE filter(IN string varchar(1000), OUT salida varchar(1000))
BEGIN
.....
END
And I want make a insert with a select query for example:
INSERT INTO otherTable
SELECT filter(concat_group(column)) , value1,value2 from mytable
GROUP BY column,value,value2;
which is the correct way to do this?
Generally, you cannot call a stored procedure in the SQL select statement. What you want is like custom scalar functions.
reference
mysql scalar function with for loop

MySQL -> create alias for DATE_FORMAT function with given pattern

I just wanted to know if it was possible to do something like:
CREATE ALIAS SOLR_DATE_FORMAT FOR DATE_FORMAT(date_val,'%Y-%m-%dT%TZ')
DATE_FORMAT(date_val,'%Y-%m-%dT%TZ') exists in MySQL, but I'd like to give it another name and make it a one argument function, because in my unit tests I use another DB (H2) on which I defined such a SOLR_DATE_FORMAT function
You can create your own function:
DELIMITER $$
create function SOLR_DATE_FORMAT( date_val )
returns char(20)
begin
return DATE_FORMAT(date_val,'%Y-%m-%dT%TZ');
end$$
DELIMITER ;
EDITED Fixed returned type. Thanks eggyal!

Is it possible to call stored procedure in view?

A similar question about sql-server has been asked here. I'm wondering if its possible in MySql.
edit:
I want to use result set returned from procedure in view.
If you want to get result-set and use routine in FROM clause - NO. Stored routines (procedures or functions) in MySQL cannot return tables as result value.
But you can use functions as simple values, for example -
DELIMITER $$
CREATE FUNCTION mul10(Param1 INT)
RETURNS INT(11)
BEGIN
RETURN Param1 * 10;
END
$$
DELIMITER ;
CREATE OR REPLACE VIEW view1
AS
SELECT mul10(2) AS column1;
SELECT column1 FROM view1;
----------
20