Replacing a string without using regular expression in netezza - sql

Is there any way to replace a string in netezza sql without using regular expression function (i.e. regexp_replace())
eg:
replace('perfect','fect','fume')
TIA

Without the SQL Extensions Toolkit you can just use substr and instr functions. You may have to run them multiple times depending on the recurrence of the string in question. Below is an example:
select substr(a.txt,1,instr(a.txt,'fect')-1)
||'fume'
||substr(a.txt,instr(a.txt,'fect')+length('fect'),255)
from (select 'perfect' as txt) a

In case you have the SQL Extensions Toolkit installed then you can use:
select sql_functions..replace('prefect', 'fect', 'fume')

Related

starts_with in presto?

I am new to writing sql queries in presto and was looking for a function similar to 'starts_with'.
If a string starts with a given substring then the query needs to return that record.
In Postgresql, I am currently doing select * from tableA where name~'^Joh'. Whats the equivalent of this in Presto?
PostgreSQL and presto are RDBMS based on SQL. It is weird to see that you've learned a PostgreSQL proprietary add on (regular expressions) to the language before learning the standard SQL functions. In SQL you use LIKE for pattern matches:
select * from tableA where name like 'Joh%';
You can use Like in SQL. You can go through this link https://www.w3schools.com/sql/sql_like.asp. Using like you can search for a specified pattern.
In presto you can use regexp_like() which runs little faster than other like operators.For your case try below query which should provide you with expected functionality.
select regexp_like('John', '^John')

REGEXP_CONTAINS not recognized

Happy new years, stackoverflow!
I am trying to use some regex functions in bigquery but some of them return error as if I have the name wrong.
SELECT REGEXP_CONTAINS(path, r'^abc$') FROM [tablename]
Query Failed
Error: 2.24 - 2.26: Unrecognized function regexp_contains
Where as if I do a similar regex function, the function text in the editor changes color and the query works.
SELECT REGEXP_EXTRACT(path, r'^abc$') FROM [tablename]
It should work since it's documented in this link.
Does anyone know how to fix this?
BigQuery Legacy SQL and Standard SQL support different set of regular expression functions
Legacy SQL Regular Expression Functions:
REGEXP_MATCH, REGEXP_EXTRACT and REGEXP_REPLACE
Standard SQL Regular Expression Functions:
REGEXP_CONTAINS, REGEXP_EXTRACT, REGEXP_EXTRACT_ALL and REGEXP_REPLACE
So, in your case just make sure you use proper BigQuery SQL dialect
#standardSQL
SELECT REGEXP_CONTAINS(path, r'^abc$') FROM [tablename]

Informatica Coding to SQL

I am attempting to translate the following Informatica code to the equivalent SQL scripts. I am a little stuck as I am not familiar with Informatica and would appreciate any assistance.
The original informatica code reads as follows:
LTRIM(RTRIM(SUBSTR(COV_REINS_CONCAT_BK,11, INSTR(COV_REINS_CONCAT_BK, '|',1,3)-INSTR(COV_REINS_CONCAT_BK, '|',1,2)-1 ))) || 'C'
select LTRIM(RTRIM(SUBSTR(COV_REINS_CONCAT_BK,11,INSTR(COV_REINS_CONCAT_BK,'|',1‌​,3)
-INSTR(COV_REINS_CONCAT_BK,'|',1,2)-1 )))||'C' from TABLE;
Above script will work fine in Oracle. Please replace table with your table name.
It becomes increasingly difficult to port such expressions that make extensive use of INSTR function using the occurrence parameter.
Maybe it's easier to create in your MS-SQL database an INSTR equivalent function that supports the occurrence parameter. See how to create the dbo.INSTR function here.
On this same site you can also see a table with more function equivalences from Oracle to MS-SQL.
Then the expression from the comment becomes much easier to translate:
LTRIM(RTRIM(SUBSTRING(COV_REINS_CONCAT_BK,11,dbo.INSTR(COV_REINS_CONCAT_BK,'|',1‌​,3)
-dbo.INSTR(COV_REINS_CONCAT_BK,'|',1,2)-1 )))+'C'
Here SUBSTR became SUBSTRING, and INSTR became dbo.INSTR and concatenation || was changed to +.
The SUBSTR() and instr() functions are from the Informatica Transformation Language. IMHO it has its roots in function names from Oracle and MSSQL / Sybase function names. This is why it doesn't translate directly to either but is similar. The functions are very well documented in the online help. You'll need to review the switches in the INSTR() function for case sensitivity and the like to ensure their parallel can be written correctly in another tool. The numbers may translate to different things and in some of the Informatica functions the end arguments can be omitted such as, in the SUBSTR() function, meaning that the SUBSTR() will take effect from the numbered position to the end of the string regardless of length. The typing of the port in Informatica can affect the result too, although in this case, the combined function is performing a trim at the end.
SUBSTR() and INSTR() functions are not MS SQL Server functions. I'm guessing the code snippet is Oracle's PL/SQL. Try resources such as http://www.dba-oracle.com/oracle_news/2005_12_16_sql_syntax_differences.htm

How to replace all string in SQL Server 2005?

In postgresql database have one function btrim(string text [, characters text]).
For examples
btrim('xwxpostsqlwwx', 'wx')
In this function return value postsql.
Like this I need to do in SQL Server 2005. Is there any similar builtin function available in SQL Server?
REPLACE, SUBSTRING,CHARINDEX
http://msdn.microsoft.com/en-us/library/ms181984.aspx
I don't know of any exact equivalent, but you should be able to achieve the same results using SQLServer's Regular Expression functionality.
I believe there is no such a built-in function, but you can create your own if you like. You can find an example of a function doing exactly what you want here (at the bottom of the page).

SQL Statement using LIKE

I want to know in a column named NUMTSO if there exists data with this format "WO#############", so what I'm doing is this:
select *
from fx1rah00
where numtso like 'WO[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]'
but I get nothing. What am I doing wrong?
This works fine for me in SQL Server. If you are not using SQL Server you will likely need some different syntax though as the pattern syntax is not standard SQL.
;with fx1rah00 As
(
select 'WO1234567890123' as numtso
)
select *
from fx1rah00
where numtso like
'WO[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]'
MySQL allows you to use regular expressions with the REGEXP keyword instead of LIKE. I suggest the following code:
SELECT *
FROM `fx1rah00`
WHERE `numtso` REGEXP 'WO[0-9]{13}'
What dbms is this? Some databases don't let use use regex in like clause just wildcards. If its oracle you could checkout REGEXP_LIKE or REGEXP for mysql.
I would do something like:
where NUMTSO like 'WO%'
and REGEXP_LIKE(NUMTSO, 'WO[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]')
by using the like and the regex check you can still range scan on an index if there was one.
The SQL standard does not support REGEXP in LIKE. They have a much more primitive pattern language. You'll need to add a function, or post-filter, or discover a DBMS-specific extension.