How to replace escape character in Netezza column - netezza

I am trying to replace escape character in Netezza column, but it is not properly replacing.
Please help me some one on this.
select replace('replaces\tring','\','\\\\');
I need output as replaces\\\\tring. Below is the error message i am getting...
ERROR [42S02] ERROR: Function 'REPLACE(UNKNOWN, UNKNOWN, UNKNOWN)'
does not exist Unable to identify a function that satisfies the given
argument types You may need to add explicit typecasts
Thanks in advance.

This is because REPLACE function needs to be installed (which is not by default). There is another function which is called TRANSLATE which can be used in a limited way instead of REPLACE but unfortunately won't fit in your situation.
You can use the below query instead:
SELECT SUBSTRING(x, 1, INSTR(x, '\') - 1) || '\\\\' || SUBSTRING(x, INSTR(x, '\') + LENGTH('\')) FROM
(SELECT 'replaces\tring' AS x) t
\ passed to INSTR and LENGTH is the string to be replaced. Note that they occur in three positions.
\\\\ in the middle is the replacement string.
replaces\tring is the string to search in.
Check the below example for replace love with like in I love Netezza:
SELECT SUBSTRING(x, 1, INSTR(x, 'love') - 1) || 'like' || SUBSTRING(x, INSTR(x, 'love') + LENGTH('love')) FROM
(SELECT 'I love Netezza' AS x) t

That particular function is part of the "SQL Extensions toolkit" and on our system it is placed in the ADMIN schema of the SQLEXT database. All users have been granted execute access to that schema. Furthermore the database.schema have been placed in the path (the DBA's did it globally, but you can issue a "set PATH=..." in your session if need be)
our path is:
select current_path;
CURRENT_PATH
---------------------------------------------------------------------------------------
SQLEXT.ADMIN,INZA.INZA,NZA.INZA,NZM.INZA,NZMSG.INZA,NZR.INZA,NZRC.INZA,SYNCHDB.ADMIN
and as you can see the SQLEXT is at the beginning...

Related

Snowflake JSON with foreign language to tabular format dynamically

I read through snowflake documentation and the web and found only one solution to my problem by https://stackoverflow.com/users/12756381/greg-pavlik which can be found here Snowflake JSON to tabular
This doesn't work on data with Russian attribute names and attribute values. What modifications can be made for this to fit my case?
Here is an example:
create or replace table target_json_table(
v variant
);
INSERT INTO target_json_table SELECT parse_json('{
"at": {
"cf": "NV"
},
"pd": {
"мо": "мо",
"ä": "ä",
"retailerName": "retailer",
"productName":"product"
}
}');
call create_view_over_json('target_json_table', 'V', 'MY_VIEW');
ERROR: Encountered an error while creating the view. SQL compilation error: syntax error line 7 at position 7 unexpected 'ä:'. syntax error line 8 at position 7 unexpected 'мо'.
There was a bug in the original SQL used as a basis for the creation of the stored procedure. I have corrected that. You can get an update on the Github page. The changed section is here:
sql =
`
SELECT DISTINCT '"' || array_to_string(split(f.path, '.'), '"."') || '"' AS path_nAme, -- This generates paths with levels enclosed by double quotes (ex: "path"."to"."element"). It also strips any bracket-enclosed array element references (like "[0]")
DECODE (substr(typeof(f.value),1,1),'A','ARRAY','B','BOOLEAN','I','FLOAT','D','FLOAT','STRING') AS attribute_type, -- This generates column datatypes of ARRAY, BOOLEAN, FLOAT, and STRING only
'"' || array_to_string(split(f.path, '.'), '.') || '"' AS alias_name -- This generates column aliases based on the path
FROM
#~TABLE_NAME~#,
LATERAL FLATTEN(#~COL_NAME~#, RECURSIVE=>true) f
WHERE TYPEOF(f.value) != 'OBJECT'
AND NOT contains(f.path, '[') -- This prevents traversal down into arrays
limit ${ROW_SAMPLE_SIZE}
`;
Previously this SQL simply replaced non-ASCII characters with underscores. The updated SQL will wrap key names in double quotes to create non-ASCII key names.
Be sure that's what you want it to do. Also, the keys are nested. I decided that the best way to handle that is to create column names in the view with dot notation, for example one column name is pd.ä. That will require wrapping the column name with double quotes, such as:
select * from MY_VIEW where "pd.ä" = 'ä';
Final note: The name of your stored procedure is create_view_over_json, however, in the Github project the name is create_view_over_variant. When you update, be sure to call the right procedure.

Remove the 'NUL' character in SSIS being imported from a text file

I have an input file that is generated from an external source on a daily basis, so I have littel control ovever it.
The file is to be read by SSIS and then inserted into a SQL Server 2012 database. I have come to issue on one particular line.
The line in questions looks like this:
"|"James"|"Done"|""|""|""|"11548"|" "|""|""|""|""|""|""|""|""|""|"
The value in the 7th field is not actually a space, but a 'NUL' character ASCII(0) and I seem to be unable
to remove it witin SSIS.
I'm using a derived column transform to replace the null. The whole string is named as "DataColumn". So far I have tried the following in the expression builder.
1) Replace(DataColumn, "CHAR(0)", "")
2) Replace(DataColumn, "CHAR(0)", "\"\"")
3) Replace(DataColumn, "ASCII(0), "")
4) Replace(DataColumn, " ", "")
5) Replace(DataColumn, "\x000", "")
In addition, the following suggestions have also been tried:
Replace(DataColumn, "0x0", "")
Replace(DataColumn, "\x0000", "")
To date none seem to be working for me, so any suggestions would be greatly appreciated.
When calling the REPLACE function, don't quote the calls to the CHAR or ASCII functions. If you quote them, then REPLACE is looking for a literal string with the value CHAR(0) for example.
This example here strips out a NULL character
declare #str varchar(100) = 'Hello>' + char(0) + '<'
declare #fixed varchar(100) = replace(#str, char(0), '')
select #str, len(#str), #fixed, len(#fixed)
Sidenote: The ASCII function returns the numeric codepint for a character, so that's not what you're after here since you know you're looking for the null character at point 0

Splitting contents of one sql column into 3 columns based on certain characters that always happen in the value

I'm trying to form a SQL query, using SQL Server 2014 without creating a function. I do not have permissions on the database to create functions so I have to do it with a query only.
I have a column named Test with the example value of:
Accounting -> Add Missing functionality in Payable -> Saving a blank Missing row
I want my query to return the information (of varying length) between the two arrows (->). I have tried the right, left, substring, charindex and patindex functions and various combinations of each.
Basically the query needs to be SUBSTRING(Test, CHARINDEX(' -> ', TEST) +3, <some length here>)
The length is the part I'm having a hard time figuring out. I need the full length minus the first part before and including the first -> which evaluates to:
Add Missing functionality in Payable -> Saving a blank Missing row
From that result, I need to remove everything after and including the ->, which would then leave me with:
Add Missing functionality in Payable
At the end of the day, I want to split this one column up into 3 like so:
Domain | Feature | Test
------------------------------------------------------------------------------
Accounting | Add Missing functionality in Payable | Saving a blank Missing row
Can anyone show me how to do this query, without having to write a function? Any suggestions would be greatly appreciated as I have been working on this one portion of the query for the better part of 4 hours now. Thank you in advance for your help. Have a great day!!
I tried the following query and it is woking fine for me:
DECLARE #X as varchar(1000)
SET #X = 'Accounting -> Add Missing functionality in Payable -> Saving a blank Missing row'
SELECT SUBSTRING(#X,1,CHARINDEX('->',#X) - 1) AS Domain,
SUBSTRING(#X,CHARINDEX('->',#X) + 2,LEN(SUBSTRING(#X,CHARINDEX('->',#X) + 2,LEN(#X))) - LEN(SUBSTRING(#X,LEN(#X) - CHARINDEX('>-',REVERSE(#X)) ,LEN(#X)))) AS Feature,
SUBSTRING(#X,LEN(#X) - CHARINDEX('>-',REVERSE(#X)) + 2 ,LEN(#X)) AS Test
You have to use this query:
SELECT SUBSTRING([Test],1,CHARINDEX('->',[Test]) - 1) AS Domain,
SUBSTRING([Test],CHARINDEX('->',[Test]) + 2,LEN(SUBSTRING([Test],CHARINDEX('->',[Test]) + 2,LEN([Test]))) - LEN(SUBSTRING([Test],LEN([Test]) - CHARINDEX('>-',REVERSE([Test])) ,LEN([Test])))) AS Feature,
SUBSTRING([Test],LEN([Test]) - CHARINDEX('>-',REVERSE([Test])) + 2 ,LEN([Test])) AS Test
FROM MyTable --Replace MyTable with your table name

Escaping an ampersand in SQL Server Full-Text Search query using CONTAINSTABLE

I have a very peculiar case. My ASP.NET page calls a stored procedure of ours that performs a Full-Text Search query on our database. Some of the commonly searched strings include an ampersand because a few brands of our products (well-known brands, too) have an & in their name.
It turns out that in a certain case I get no results unless I escape the ampersand (\&), and in a certain other case I get no results only if I escape the ampersand.
I don't know if this is relevant, but (without giving out the brand names) one ends in &b and the other one in &c.
Is it possible that these strings (&b or &c) have some special meaning of their own? And that by escaping them I'm actually passing a special string to T-SQL?
EDIT
Additional info: after further testing, I proved that the error is in the stored procedure itself. Calling it with & or \& yields different results.
I'll try to post selected parts of the stored procedures. I won't post it all, because most of it isn't really relevant.
The vParamBuca parameter is the one that causes the troubles. Values could be 'word&letter' or word\&letter.
SET #ricercaA = '''FORMSOF(INFLECTIONAL,"' +
REPLACE(LTRIM(RTRIM(#vParamBuca)),' ', '") AND FORMSOF(INFLECTIONAL,"') + '")'''
The variable #ricercaA is then used to create the query string:
[...]
FROM Products AS FT_TBL
LEFT OUTER JOIN CONTAINSTABLE (Products, Sign1, '+ #ricercaA + ') AS ColSign1_0 ON FT_TBL.ID = ColSign1_0.[KEY]
LEFT OUTER JOIN CONTAINSTABLE (Products, ManufacturerAdditionalText, '+ #ricercaA + ') AS ColManufacturerAdditionalText_0 ON FT_TBL.ID = ColManufacturerAdditionalText_0.[KEY]
LEFT OUTER JOIN CONTAINSTABLE (Products, ManufacturerForSearch, '+ #ricercaA + ') AS ColManufacturer_0 ON FT_TBL.ID = ColManufacturer_0.[KEY]
LEFT OUTER JOIN CONTAINSTABLE (Products, TuttaLaRiga, '+ #ricercaA + ') AS ColTuttaLaRiga_0 ON FT_TBL.ID = ColTuttaLaRiga_0.[KEY]
[...]
EDIT 2
Many thanks to #srutzky for pointing me in the right direction! In the meanwhile, I also found a data inconsistency where one of the brands with the & in its name was modified not to have the &, and the other one wasn't modified (bottom line, my current problem is caused by that: a partial fix that was made by someone in the past).
Anyway, back on track. Now I understand that the & character in the CONTAINSTABLE function is treated as a logical AND (non bitwise).
I still need a solution for that. This answer gives a solution that doesn't work for me (the conditions are not the same as mine). How could I perform a CONTAINSTABLE search for a string with an ampersand in it? Preferably without having to transform the ampersand to another safe character?
The odd behavior you are seeing is most likely due to the CONTAINS and CONTAINSTABLE functions (both used with SQL Server's Full Text Search feature) using the ampersand ( & ) character as equivalent to the AND operator. The following statement is taken from the documentation for CONTAINS:
The ampersand symbol (&) may be used instead of the AND keyword to represent the AND operator.
There is no mention of there being any escape character for it (and a back-slash isn't typically an escape character in SQL anyway).
UPDATE
Based on the information now provided in "Edit 2" of the Question, and additional research, I would say that you do not need to escape anything. It seems that putting the search phrases in double-quotes (as a result of using FORMSOF) treats the & as either a literal or a word-breaker, depending on the values on both sides of the &. Try the following examples:
DECLARE #Term NVARCHAR(100);
SET #Term = N'bob&sally'; -- 48 rows
--SET #Term = N'bob\&sally'; -- 48 rows
--SET #Term = N'r&f'; -- 4 rows
--SET #Term = N'r\&f'; -- 24 rows
SET #Term = N'FORMSOF(INFLECTIONAL,"' + #Term + '")';
SELECT * FROM sys.dm_fts_parser(#Term, 1033, 0, 0);
SELECT * FROM sys.dm_fts_parser(#Term, 1033, 0, 1);
SELECT * FROM sys.dm_fts_parser(#Term, 1033, NULL, 0);
SELECT * FROM sys.dm_fts_parser(#Term, 1033, NULL, 1);
The results for bob&sally and bob\&sally are the same, and in both cases bob and sally are separated and never combined into a single exact-match string.
The results between r&f and r\&f, however, are not the same. r&f is only ever treated as a single, exact-match string because r and f alone are not known words. On the other hand, adding in the back-slash separates the two letter since \ is a word-breaker, in which case you get both r and f.
Given that you stated in the Update that you have "data inconsistency, where one of the brands with the "&" in its name was modified not to have the "&", and the other one wasn't", I suspect that when you do not add in the \ character you get the brand that was not modified (since it is an exact match for the full term). But when you do add in the \ character, then you get the brand that was modified to have the & removed, since you are now searching on both pieces, each one matching part of that brand name.
I would fix the data to be consistent: update the brand names that had the & removed to put the ampersands back in. Then when people search using & without the extra \ added, it will be an exact match. This behavior will be consisted across the data, and will not require you adding code to circumvent the natural operation of FTS, which seems to be an error-prone approach.

DB2 -- How to use LTRIM scalar function with 2 arguments?

I am new to DB2 (using version 10.1) and I'm trying to execute a simple ltrim function in a test query.
select ltrim(',1,2,3,4', ',') from sysibm.sysdummy1;
This results in the following error:
DB2 SQL Error: SQLCODE=-440, SQLSTATE=42884, SQLERRMC=LTRIM;FUNCTION, DRIVER=3.65.77
Based on the documentation here, it seems my example should work. Note, I can use the alternative trim function or the 1-arg version of ltrim:
select trim(l ',' from ',1,2,3,4') from sysibm.sysdummy1;
select ltrim(' test') from sysibm.sysdummy1;
And those works fine!
Are there some fundamental differences between the 2-arg form of ltrim and the other examples I provided?
This function was added in DB2 10.1 fix pack 2 -- you may want to upgrade.
Making some guesses here, but are you sure you're on 10.1? That version of that function you're trying to use appears to have been added in 10, so if you're on 9.x, it won't work.
If you are, you might check and see if your schema path includes SYSIBM:
SELECT CURRENT_PATH FROM SYSIBM.SYSDUMMY1
The "old" one-argument version of the function is in SYSFUN, which (if your path doesn't include SYSIBM) might be why you're still able to use that version.
If SYSIBM isn't in your path, you can try changing it using:
SET PATH = SYSIBM, CURRENT_PATH
Why not try STRIP() instead:
VALUES STRIP(',1,2,3,4', L, ',');
(side note: It is simpler to use the VALUES statement, rather than SELECT FROM SYSIBM.SYSDUMMY1)

Resources