Function format number and how to use it in sql statement - sql

For oracle,
If I have a table A with data in number (such as price), how to write a function to format a number(10,2) to 9,9999.99 (<< obvious format).
And then how to use this function in SQL statement for displaying the data in table A.
Thank you.
----- UPDATE -----
From kindly suggestions below, yes, I should not treat numbers in this way.
But I highly want to accomplish the task above
Here I come with the function
CREATE OR REPLACE FUNCTION Fmt_num(N1 in NUMBER)
RETURN CHAR
IS
BEGIN
RETURN TO_CHAR(N1,'FM9,9999.99');
END;
/
And I can use this with the SQL statement as follow
SELECT Fmt_num(price) from A;
It works ! However, Can anyone fixes the function above to let it works with "a number (10,2)"? Just this condition only. Thank you for your anticipate in this noob question. :)

Look at TO_CHAR function.
select to_char(price, 'FM9,999,999.99')
from A;

Related

Best way to write a udf in vertica, where I need to refer to data from one of the rate table and write a formula on top

I am planning to write a udf which can return the new value based on the rate setup for a specific date in a table, which means i need to write a query in udf
1. is it recommended as there are not enough example which refers to a table in udf
2. what are the other ways to solve this as Vetica procedural function does not allow to query within the function sql how it works in plsql
The requirement is not clear but a UDF function can be used. Below is the pseudocode
CREATE FUNCTION updateRate(x DATE) RETURN INT
AS BEGIN
RETURN (<your logic on updating rate> );
END;
And then call the function in update query
=>Update Mytable set rate=updateRate(colDate);

Complicated text compare in SQL

Suppose I have a table result
---------------------------------------------------------
coupon id| required_product_ids|used_product_in_this_year
---------------------------------------------------------
1 |1,2,3,10 |2,3,4,5,6,7,8,9,10,12,13
How can I check if used_product_in_this_year has at least one required_product_ids by SQL.
I tried somethings with SQL like keyword but did not success.
There is no native SQL construct for performing this type of comparison.
To find a single value in a comma separated list, MySQL provides a FIND_IN_SET function. But to handle a comma separated list of values, to check each one, to see if it's in a list, each separate value would need to be supplied into FIND_IN_SET. And that would be unweildy.
If the hard and fast requirement is to handle this comparison in a SQL statement, I'd recommend writing a function to do the comparison.
DELIMITER $$
CREATE FUNCTION upity_halo_rpi(upity VARCHAR(4000), rpi VARCHAR(4000))
RETURNS INT DETERMINISTIC
BEGIN
-- TODO: extract first element of upity
-- TODO: check if element is in rpi list
-- if it is found in the list
RETURN TRUE;
-- otherwise, split off next element
-- loop through all elements
-- if loop completes without finding a match is found, fall out
RETURN FALSE;
END$$
DELIMITER ;
With the function written, and thoroughly tested, it could be used in a SQL statement. To return a column that indicates that the row "has at least one"...
SELECT t.coupon id
, t.required_product_ids
, t.used_product_in_this_year
, upity_halo_rpi(t.used_product_in_this_year,t.required_product_ids) AS halo
FROM result t
To return:
coupon id required_product_ids used_product_in_this_year halo
--------- -------------------- ------------------------ -----
1 1,2,3,10 2,3,4,5,6,7,8,9,10,12,13 1
I'm not going to write the function. I'm just demonstrating a possible approach. One possible answer to "how" this type of comparison operation could be performed within a SQL statement.
This is how you can do it, without changing your database structure.
In MYSQL (Tested):
select * from TableName
where concat(',', used_product_in_this_year, ',') regexp concat(',',replace(required_product_ids,',',',|,'),',')
Using this Regex structure with your table and manipulating the data with a some mysql string functions.
I don't recommend your database structure, but I like puzzles and this one was fun, thanks for the challenge.

sql function to query a Table based on searching a column for a substring

I googled this but couldn't find an easy answer:
I need to write a function that takes a substring as parameter and return those rows which have this substring in a certain column:
select * from myTable where myTable.someColumn contains #substring
appreciate your help
thank you
you may use
where myTable.someColumn like '%?%'
with '#substring' as parameter.

Oracle SQL Function using data from another schema says table or view does not exist

Im trying to create a function which takes in 3 parameters and returns one. The value of the returned parameter is obtained by querying two tables, however, one of the tables is on a different schema. The SQL im using is below:
create or replace FUNCTION tester
(
originaltext IN VARCHAR2, lang IN VARCHAR2, category IN VARCHAR2 DEFAULT NULL)
RETURN VARCHAR2 AS
translatedttextval VARCHAR2(255) := '';
BEGIN
--if category is null, disregard category
if category is null then
SELECT distinct nvl(trans.translatedtext, originaltext)
INTO translatedttext
FROM tbl_translations trans, SECDEVSCHEMA.tbl_instanceids ids
WHERE trans.translatedlang = ids.id_a
AND ids.name = lang
AND trans.originaltext = originaltext;
end if;
RETURN translatedttextval;
END;
I've removed a bit of the query here which searches with category because it does something similar and has the same issue.
So, when I run this and pass in the params, I get an error which reads:
Error(16,46): PL/SQL: ORA-00942: table or view does not exist
If I do the following query it works fine when not on the SECDEVSCHEMA and returns the reults from tbl_instanceids which is on the SECDEVSCHEMA schema:
SELECT * FROM SECDEVSCHEMA.tbl_instanceids ids WHERE ids.id_a = 1234555;
I don't have DBA access to the DB but if I need some select priviledge granted or something I can get it done. Not sure if this the case though as the above query works.
Small additional question also:
where it says in the query
nvl(trans.translatedtext, originaltext)
If i wanted to surround the original text value with brackets when no translatedtext value exists, how would I go about this?
I'm using SQL Developer by the way in case that's important.
Thanks
for your smaller question
nvl(trans.translatedtext, '<'|| originaltext ||'>' )
This was down to a priviledges issue.
In SQL Developer, I expanded the tables package and selected the table I needed access to in the Function, right clicked and selected Priviledges > Grant.
As this is just a Development DB I just assigned all the priviledges to the appropriate schema or User and its working now.
Still unsure about this part though:
Small additional question also: where it says in the query nvl(trans.translatedtext, originaltext)
If i wanted to surround the original text value with brackets when no translatedtext value exists, how would >I go about this?
Any takers for this?
Thanks

Dynamic SQL Select Variable Statement

First time poster with beginner sql knowledge (so go easy on me)
I have a SQL statement currently that extracts monthly data from a table, im looking to make this extraction dynamic and rolling based on previous month.
For example - Assuming the date is 01/12/2013
Select Month10_Values
,Month11_Values
,Month12_Values
from test_table;
I want to replace the the numbers of the fields with a dynamic variable something like this:
Select Month[curr_month-2]_Values
,Month[curr_month-1]_Values
,Month[curr_month]_Values
from test_table;
Where [curr_month] will have been pre-determined (in this case 12 based on an assumed date of 01/12/2013)
Any help on this would be greatly appreciated, hopefully I've made some sense.
Thanks
You should do something like that (simplified)
declare
currMonth number:=12;
plsql_block VARCHAR2(500);
begin
plsql_block:='Select Month'||currMonth -2||'_Values,
Month'||curr_month-1||'_Values,
Month'||currMonth||'_Values
from test_table';
execute immediate plsql_block;
end
If you want to get the result of the dynamic statement, take a look here for example.