How to execute levenshtein distance in PostgreSQL dynamic SQL? - sql

I'm trying to apply Levenshtein distance in PostgreSQL Dynamic SQL. Here is what I put together. This will go into a stored procedure.
The end result is to return the total number of records to the output variable (v_totalcount).
Am I on the right path? Please advise. Thanks!
my variables:
v_name varchar
v_col_nm varchar
v_dep_num integer
v_totalcount integer
my query:
EXECUTE 'SELECT count(*) FROM EMP
WHERE LEVENSHTEIN'('||v_name||','||v_col_nm||')<=' ||v_dep_num INTO v_totalcount;

Related

How to use string argument in Select statement in BigQuery UDF?

This is my code for Google BigQuery UDF.
CREATE OR REPLACE FUNCTION `my_dataset.ST_EXTENT_TABLE`
(tableName STRING)
RETURNS STRUCT<xmin FLOAT64, ymin FLOAT64, xmax FLOAT64, ymax FLOAT64>
AS (
WITH data AS ( SELECT geom FROM tableName)
SELECT ST_EXTENT(geom) AS box FROM data
);
In "SELECT geom FROM tableName", tableName should be replaced with passed value.
Any idea?
According to your requirement, it is not possible to dynamically pass the table name in UDF. As an alternative you can write a dynamic sql query where you can pass the table name dynamically. Using Execute Immediate you can execute those dynamic queries and get the result.
You can try the below sample query.
Query:
DECLARE query String;
SET
query = '''with cte as (
select filename from %s limit 10
)
select * from cte''';
EXECUTE IMMEDIATE
FORMAT(query,"bigquery-public-data.bbc_news.fulltext")
Output:

How to find number of times a values is repeated in a string in a column

In SQL HANA, I need to find how many times a given word is repeated in a string column whose values are delimited by "," and output it as a separate column.
Example, the string column contains:
ZN,ZN,ZS,ZQ
Expected result for "ZN":
2
You might find it acceptable to search only the string ZN by ignoring the fact that there's a comma.
You may count the number of occurrences of any substring by using the string function OCCURRENCES_REGEXPR:
SELECT OCCURRENCES_REGEXPR('(ZN)' IN STRINGCOLUMN) "occurrences_zn" FROM TABLE;
If you really want to clearly specify that ZN is to be searched as an entire word between commas or at the edges, then you may find a better regular expression (the question is then more about regular expressions and not SQL HANA, and you may find existing answers in Stack Overflow).
I can't remember where I found the trick, but in SQL Server, the following works like a charm:
DECLARE #myStringToSearch nvarchar(250) = 'ZN,ZN,ZS,ZQ'
DECLARE #searchValue nvarchar(5) = 'ZN'
SELECT (LEN(#myStringToSearch) - LEN(REPLACE(#myStringToSearch, #searchValue, ''))) / LEN(#searchValue)
The last line compares the length of the original string with the length of the same string, but this time replacing your search value (ZN) with a blank string. In our case, this would result in 4, because ZN is 2 characters, and it was removed twice. However, we're not interested in how many characters were removed, but in how many times the value was encountered, so we divide that result by the length of your search string (2).
Output of the query:
2
You could easily implement this as a DEFAULT constraint in your table, provided your search string is the same across every row.
I wrote one anonymous block in sql , which can be converted to HANA Table function and can be used to achieve expected result.
DO
BEGIN
DECLARE FULL_STRING VARCHAR(100);
DECLARE TRIM_STRING VARCHAR(100);
DECLARE VAL_STRING VARCHAR(100);
FULL_STRING ='ZN,ZN,ZS,ZQ';
FULL_STRING=CONCAT(FULL_STRING,',');
--SELECT :FULL_STRING FROM DUMMY;
VAL_STRING=SUBSTRING(:FULL_STRING,1,LOCATE(:FULL_STRING,',',1)-1);
VAR_TABLE=SELECT :VAL_STRING STRINGVAL FROM DUMMY;
TRIM_STRING=SUBSTRING(:FULL_STRING,LOCATE(:FULL_STRING,',',1)+1 ,LENGTH(:FULL_STRING));
--SELECT * FROM :VAR_TABLE;
--SELECT :TRIM_STRING FROM DUMMY;
WHILE :TRIM_STRING IS NOT NULL AND LENGTH(:TRIM_STRING)>0
DO
VAL_STRING=SUBSTRING(:TRIM_STRING,1,LOCATE(:TRIM_STRING,',',1)-1);
--SELECT :VAL_STRING FROM DUMMY;
VAR_TABLE=SELECT STRINGVAL FROM :VAR_TABLE
UNION ALL
SELECT :VAL_STRING FROM DUMMY;
TRIM_STRING=SUBSTRING(:TRIM_STRING,LOCATE(:TRIM_STRING,',',1)+1 ,LENGTH(:TRIM_STRING));
--i=i+1;
--SELECT :TRIM_STRING FROM DUMMY;
END WHILE ;
SELECT STRINGVAL,COUNT(STRINGVAL) FROM :VAR_TABLE GROUP BY STRINGVAL;
--SELECT :TRIM_STRING FROM DUMMY;

Declare long string for using few times in query statement ORACLE

I want to build a query in oracle and for some materials and I want to declare these materials once and I want to use this variable in the query. The variable will be a string of strings.
It is my query but is wrong. How I can write correct query :
enter code herev_material varchar2(18000) := '2421032060SST','2421040080SST','2421050080SST';
SELECT MARA.MATERIAL,MARA.NAZWA_MATERIALU,MARA.JM,MARA.MAABC
FROM OLAP_DANE.MV_SAP_MARA MARA
Left outer join
(select something
from a
where material in v_material)c
on c.material in mara.material
WHERE MARA.MATERIAL = v_material
;
Not only query wrong, declaration of string is also wrong. You can not declare string such way.
1. For simple solution, if we can suppose that values of materials are unique from comma to comma, you can declare string like
v_material varchar2(18000) := ',2421032060SST,2421040080SST,2421050080SST,';
and when use in query
... and instr(','|| MARA.Materials ||',', v_material) > 0
2. Or way two, and it's more right in my point of view.
declare schema level table type of string
create or replace type TStrings as table of varchar2(150);
declare
v_materials TStrings := TStrings('2421032060SST','2421040080SST','2421050080SST');
and then use in query like
... select column_value from table(v_material) ...
something like this.

Extracting a number of specific length from a string in SQL Server

I have a string like
ADN120_XK7760069988881LJ
in one of my columns. I have to extract the number with 13 digits length. For example, in the above case, I want to extract 7760069988881 in SQL Server.
using patindex() with substring() (using a variable for the pattern and replicate() to simplify repeating [0-9] 13 times):
create table t (val varchar(128));
insert into t values ('ADN120_XK7760069988881LJ');
declare #pattern varchar(128) = '%'+replicate('[0-9]',13)+'%';
select substring(val,patindex(#pattern,val),13)
from t;
rextester demo: http://rextester.com/MOEVG64754
returns 7760069988881
Creating TEMP table with your query
SELECT 'ADN120_XK7760069988881LJ' CODE INTO #TEMP
Solution using regular expression
SELECT SUBSTRING(CODE,PATINDEX('%[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%',CODE),13)
FROM #TEMP
Couldn't reduce the number of times [0-9] used
Hope this helps

SQL Server 2005 I am not able to read from a table

Please suppose that in SQL Server 2005, if you launch the following query:
SELECT CHICKEN_CODE FROM ALL_CHICKENS WHERE MY_PARAMETER = 'N123123123';
you obtain:
31
as result.
Now, I would like to write a function that, given a value for MY_PARAMETER, yields the corresponding value of CHICKEN_CODE, found in the table ALL_CHICKENS.
I have written the following stored function in SQL Server 2005:
ALTER FUNCTION [dbo].[determines_chicken_code]
(
#input_parameter VARCHAR
)
RETURNS varchar
AS
BEGIN
DECLARE #myresult varchar
SELECT #myresult = CHICKEN_CODE
FROM dbo.ALL_CHICKENS
WHERE MY_PARAMETER = #input_parameter
RETURN #myresult
END
But if I launch the following query:
SELECT DBO.determines_chicken_code('N123123123')
it yields:
NULL
Why?
Thank you in advance for your kind cooperation.
define the length of your varchar variables like this
varchar(100)
Without the 100 (or whatever length you choose) its lengh is 1 and the where clause will filter out the correct results.
Specify a length for your varchar (ex.: varchar(100)). Without length, varchar = 1 char.
As per other PS, You can store only one char in the #myresult because you have not specified any length, bcoz 1 char length is default for Varchar datatype.
Why we are getting NUll, not the first char:
If there are multiple records are filtered on basis of Where clause in ALL_CHICKENS table then the value of CHICKEN_CODE column is picked up from last row in ALL_CHICKENS table.
It seems that the last row has null value in CHICKEN_CODE column.
Specify a length for #input_parameter, #myresult as by default varchar lengh is 1.