REPLACE function SYBASE doesn't work - sybase

The replace function is not working with Sybase. I tried with the function str_replace but I had an error too. I need to replace all carriage returns and Lines feeds from the field "LIGTXT" by " -" .
SELECT str_replace(LIGTXT,char(10)+char(13), ' - ' ) as COMMENT FROM TXTENT
=> Function REPLACE invoked with wrong number or type of argument(s).
Can you please help me ?

Related

How to replace escape character in Netezza column

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...

SSIS Find & Reverse Function

I am trying to get the date from the below filename using ssis expression.i get this file name from the variable
I have tried using findstring and reverse function but it still throwing cannot parse.
Any recommendtions pls
Variable
#[User::CurrentFile] : Z:\RETAIL DATA\HN_NLG\Supplier_report_by_Brand_Trend Micro_WC_20170327.xlsx
Need to get 20170327 and convert to 2017-03-27
Thanks in advance
You can use LEFT and RIGHT and SUBSTRING to achieve this, use the following expression to get the expected output:
SUBSTRING(LEFT(right(#[User::CurrentFile],13),8),1,4) + "-" + SUBSTRING(LEFT(right(#[User::CurrentFile],13),8),5,2) + "-" + SUBSTRING(LEFT(right(#[User::CurrentFile],13),8),7,2)
If #[User::CurrentFile] = 'Z:\RETAIL DATA\HN_NLG\Supplier_report_by_Brand_Trend Micro_WC_20170327.xlsx' you will get 2017-03-27
Try something like below, may be in a exec sql task or script and assign it to a variable for using further.
select Cast(CONVERT(date,
Reverse(Substring(Reverse('Z:\RETAIL DATA\HN_NLG\Supplier_report_by_Brand_Trend Micro_WC_20170327.xlsx'),
CHARINDEX('.',Reverse('Z:\RETAIL DATA\HN_NLG\Supplier_report_by_Brand_Trend Micro_WC_20170327.xlsx'),1)+1,
8))) as varchar(10))

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

Forbidden characters inside CONTAINS (Transact-SQL) statement

I would like to know which characters cannot be inserted into a CONTAINS statement.
For example, let's assume I have an entry in my DB with name='Jon Snow'.
Running this query I correctly get the result.
select name
from Userstable
where CONTAINS((name),'JON AND SNOW')
Running e.g.
select name
from Userstable
where CONTAINS((name),'JO*N AND SNOW')
I get 0 results (but no error, so that's OK).
Instead, running e.g.
select name
from Userstable
where CONTAINS((name),'J[ON AND SNOW')
I get syntax error.
What are the characters that give me syntax errors?
So far I found:
[
]
!
,
Thanks
I think I found all the forbidden characters.
,
'
"
~
(
)
[
]
!

Building dynamic query for Sql Server 2008 when table name contains " ' "

I need to fetch Table's TOP_PK, IDENT_CURRENT, IDENT_INCR, IDENT_SEED for which i am building dynamic query as below:
sGetSchemaCommand = String.Format("SELECT (SELECT TOP 1 [{0}] FROM [{1}]) AS TOP_PK, IDENT_CURRENT('[{1}]') AS CURRENT_IDENT, IDENT_INCR('[{1}]') AS IDENT_ICREMENT, IDENT_SEED('[{1}]') AS IDENT_SEED", pPrimaryKey, pTableName)
Here pPrimaryKey is name of Table's primary key column and pTableName is name of Table.
Now, i am facing problem when Table_Name contains " ' " character.(For Ex. KIN'1)
When i am using above logic and building query it would be as below:
SELECT (SELECT TOP 1 [ID] FROM [KIL'1]) AS TOP_PK, IDENT_CURRENT('[KIL'1]') AS CURRENT_IDENT, IDENT_INCR('[KIL'1]') AS IDENT_ICREMENT, IDENT_SEED('[KIL'1]') AS IDENT_SEED
Here, by executing above query i am getting error as below:
Incorrect syntax near '1'.
Unclosed quotation mark after the character string ') AS IDENT_SEED'.
So, can anyone please show me the best way to solve this problem?
Escape a single quote by doubling it: KIL'1 becomes KIL''1.
If a string already has adjacent single quotes, two becomes four, or four becomes eight... it can get a little hard to read, but it works :)
Using string methods from .NET, your statement could be:
sGetSchemaCommand = String.Format("SELECT (SELECT TOP 1 [{0}] FROM [{1}]) AS TOP_PK, IDENT_CURRENT('[{2}]') AS CURRENT_IDENT, IDENT_INCR('[{2}]') AS IDENT_ICREMENT, IDENT_SEED('[{2}]') AS IDENT_SEED", pPrimaryKey, pTableName, pTableName.Replace("'","''"))
EDIT:
Note that the string replace is now only on a new, third substitution string. (I've taken out the string replace for pPrimaryKey, and for the first occurrence of pTableName.) So now, single quotes are only doubled, when they will be within other single quotes.
You need to replace every single quote into two single quotes http://beyondrelational.com/modules/2/blogs/70/posts/10827/understanding-single-quotes.aspx

Resources