How can I call a function in DB2 - sql

I have implement a function fn_getDate()
I want to use it as the following
select fn_getDate() as 'currenct_date'
so how shall I call it

select fn_getDate() as 'currenct_date' FROM SYSIBM.SYSDUMMY1;

Related

Error message when try to run a function in function

I create two functions that worked well as a stand alone functions.
But when I try to run one of them inside the second one I got an error:
'SQL compilation error: Unsupported subquery type cannot be evaluated'
Can you advise?
Adding code bellow:
CASE
WHEN (regexp_substr(ARGUMENTS_JSON,'drives_removed":\\[]') = 'drives_removed":[]') THEN **priceperitem(1998, returnitem_add_item(ARGUMENTS_JSON))**
WHEN (regexp_substr(ARGUMENTS_JSON,'drives_added":\\[\\]') = 'drives_added":[]') THEN 1
WHEN (regexp_substr(ARGUMENTS_JSON,'from_flavor') = 'from_flavor') THEN 1
WHEN (regexp_substr(ARGUMENTS_JSON,'{}') = '{}') THEN 1
ELSE 'Other'
END as Price_List,
The problem happened when with function 'priceperitem'
If I replace returnitem_add_item with a string then it will work fine.
Function 1 : priceperitem get customer number and a item return pricing per the item from customer pricing list
Function 2 : returnitem_add_item parsing string and return a string
As an alternative, you may try processing udf within a udf in the following way :
create function x()
returns integer
as
$$
select 1+2
$$
;
set q=x();
create function pi_udf(q integer)
returns integer
as
$$
select q*3
$$
;
select pi_udf($q);
If this does not work out as well, the issue might be data specific. Try executing the function with one record, and then add more records to see the difference in the behavior. There are certain limitations on using SQL UDF's in Snowflake and hence the 'Unsupported Subquery' Error.

Returning values from a database with a Substring

Let's say I've got 3 product description fields with the values AC-120 XXX, AC-120,CCC and AC-120 BBB.
How would I get that information from a table using only AC-120 as my search argument?
I've tried using the subStr function but that won't return any values either
SELECT TbArtikel.Artikel_Merk, TbArtikel.Artikel_Groep, TbArtikel.Artikel_Categorie_ID, TussenMAATenARTKEL.VoorraadNummer, TbArtikel.Artikel_ID, TussenMAATenARTKEL.ArtikelDetail_ID, TbArtikel.Artikel_Prijs_Advies, TbArtikel.Artikel_Prijs_Bees, TbArtikel.Artikel_Omschrijving
FROM TbArtikel INNER JOIN TussenMAATenARTKEL ON TbArtikel.Artikel_ID = TussenMAATenARTKEL.Artikel_ID
WHERE (((TbArtikel.Artikel_Merk)="Yonex") AND ((TbArtikel.Artikel_Omschrijving)="%AC-102%"));
LIKE is very appropriate for this comparison. However, MS Access uses different wildcards from standard SQL. So you want:
TbArtikel.Artikel_Omschrijving) LIKE "*AC-102*"
In standard SQL, this would be:
TbArtikel.Artikel_Omschrijving) LIKE "%AC-102%"
Try '((TbArtikel.Artikel_Omschrijving) LIKE "%AC-102%"' instead of '((TbArtikel.Artikel_Omschrijving)="%AC-102%"'
Use MID() function as
MID(TbArtikel.Artikel_Omschrijving, 1, 6) = 'AC-120'
Instead of
TbArtikel.Artikel_Omschrijving ="%AC-102%"

how to call table-valued function in CASE expression

I want call function in case when but when i call function I get an error .
My code is :
s = case
when v.IsAverage=1 then
isnull(avg([Value]),0)
when v.IsCumulative=1 then
isnull(sum([Value]),0)
when v.IsCumulative=0 then
GetLastValueTest('93/.1/01','93/12/29')
I get this error :
'GetLastValueTest' is not a recognized built-in function name.
but when I call this way it works :
select * from GetLastValueTest('93/.1/01','93/12/29')
select * from GetLastValueTest('93/.1/01','93/12/29')
GetLastValueTest looks like a table-valued function
then correct way to use it in case statement is:
case
when v.IsAverage=1 then isnull(avg([Value]),0)
when v.IsCumulative=1 then isnull(sum([Value]),0)
when v.IsCumulative=0 then (select top 1 [ColumnName] from GetLastValueTest('93/.1/01','93/12/29'))
end
Assuming your GetLastValueTest is scalar function - you need to specify schema explicitly, i.e. dbo.GetLastValueTest('93/.1/01','93/12/29') (or whatever you schema is if it different from dbo)
If your GetLastValueTest is table-valued function then you can't mix select from it with scalar values in other branches of case. In this case you need something like
when v.IsCumulative=0
then (select top 1 some_value from dbo.GetLastValueTest('93/.1/01','93/12/29'))

converting varchar to md5

I have the following sql code in oracle client 11g. I would like to convert the "ssno" to md5 hash. I've read other posts but none of them specifically say where to put the code. thanks!
SELECT FS_HIRES."rsa",
FS_HIRES."ssno",
FS_HIRES."lname",
FS_HIRES."series",
FS_HIRES."grade",
FS_HIRES."pos_title",
FS_HIRES."ethnicity",
FS_HIRES."disability",
FS_HIRES."type_appt",
FS_HIRES."Perm_Temp",
FS_HIRES."gender",
FS_HIRES."age",
FS_HIRES."age_categories",
FS_HIRES."los",
FS_HIRES."date_apnt",
FS_HIRES."mm_apnt",
FS_HIRES."yy_apnt",
FS_HIRES."apnt_noa",
FS_HIRES."apnt_auth",
FS_HIRES.L2_DESC,
FS_HIRES.L3_DESC,
FS_HIRES.L4_DESC,
FS_HIRES.L5_DESC,
FS_HIRES."fy"
FROM FS_HIRES
The HASH_MD5 constant can't be referred to directly from SQL, so a statement like:
SELECT FS_HIRES."rsa",
DBMS_CRYPTO.HASH(UTL_I18N.STRING_TO_RAW(FS_HIRES."ssno", 'AL32UTF8'),
DBMS_CRYPTO.HASH_MD5),
...
will get an error like "ORA-06553: PLS-221: 'HASH_MD5' is not a procedure or is undefined". You can either use the internal value for that constant, which is 2:
SELECT FS_HIRES."rsa",
DBMS_CRYPTO.HASH(UTL_I18N.STRING_TO_RAW(FS_HIRES."ssno", 'AL32UTF8'), 2),
...
Or if you don't want to rely on a constant that could potentially change in a future release, define your own function:
create or replace function my_md5(p_str varchar2) return raw is
begin
return dbms_crypto.hash(utl_i18n.string_to_raw(p_str, 'AL32UTF8'),
dbms_crypto.hash_md5);
end my_md5;
/
... and then call that:
SELECT FS_HIRES."rsa",
MY_MD5(FS_HIRES."ssno", 'AL32UTF8') AS "ssno",
...
If your database character isn't AL32UTF8, you may need to do more conversion as mentioned in the documentation, and it'll be easier to hide that in the function too.
Try this:
select
'123456789' as ssno,
rawtohex(
DBMS_CRYPTO.Hash (
UTL_I18N.STRING_TO_RAW ('123456789', 'AL32UTF8'),
2)
) as ssno_md5
from dual;
Output:
SSNO SSNO_MD5
123456789 25F9E794323B453885F5181F1B624D0B

SQL - Select statement using LastIndex

I have a resultset which has ProductURL stored :
accessoires/abc/285/P187654
accessoires/abc/285/D18765432
accessoires/abc/285/A1876543
I need to get the final part of the URL i.e. anything that is after the final '/' that appears in the URL.
I have a function which gives me the LASTINDEX :
SELECT [LAST_INDEX] (ProductURL,'/')
But how do I run a Select statement with this function inside a substring:
SELECT Substring(ProductURL,SELECT [LAST_INDEX] (ProductURL,'/'),len(ProductURL))
from data
This does not seem to work? Is there another way to do this?
Just get rid of the SELECT:
SELECT Substring(ProductURL, [LAST_INDEX] (ProductURL, '/'), len(ProductURL))
FROM data