Teradata没有函数replace,为了方便使用,定义了一个。其实也挺简单的,描述如下:
一、用c编写一个replace函数,挺简单,也可以在网上找到源码,编译通过,能达到目的即可。
二、运行一个bteq,将自定义函数载入。
详细描述如下:
[@more@]a、编写teradata创建自定义函数ddl:
REPLACE FUNCTION TEST.treplace (
input_string VARCHAR(16000) CHARACTER SET LATIN,
search_string VARCHAR(512) CHARACTER SET LATIN,
replace_string VARCHAR(512) CHARACTER SET LATIN)
RETURNS VARCHAR(16000) CHARACTER SET LATIN
SPECIFIC replace2LANGUAGE C
NO SQL
PARAMETER STYLE SQL
DETERMINISTIC
CALLED ON NULL INPUT
EXTERNAL NAME 'CS!replace2!replace2.c'
;
其中treplace即为自定义函数名,replace2.c为c程序文件名。
b、将上述文件保存为replacebtq(或其他任意你喜欢的名字);
c、执行bteq "logon copid/user,password" < replacebtq
执行成功后treplace函数即可用。注意,在administrator中我们看到的函数名称为replace2,但真正用的应该是我们定义的function,即treplace。
实验如下:
select test.treplace('abcdecd','cd','12345') ;
--------------------------------
ab12345e12345
附上我找到的c语言replace,即我使用的函数:
void replace2(VARCHAR_LATIN *input_string,
VARCHAR_LATIN *search_string,
VARCHAR_LATIN *replace_string,
VARCHAR_LATIN *result_string,
int *input_string_indicator,
int *search_string_indicator,
int *replace_string_indicator,
int *result_string_indicator,
char sqlstate[6],
SQL_TEXT extname[129],
SQL_TEXT specific_name[129],
SQL_TEXT error_message[257])
{
int source_length;
int search_length;
int replace_length;
unsigned char *source_ptr;
unsigned char *source_limit;
unsigned char *match_limit;
unsigned char *save_source_ptr;
unsigned char *target_ptr;
unsigned char *target_limit;
unsigned char *search_ptr;
unsigned char *search_limit;
if (*input_string_indicator == IS_NULL)
{
*result_string_indicator = IS_NULL;
(void) strcpy(sqlstate, UDF_OK);
error_message[0] = EOS;
return;
}
source_length = strlen((char *) input_string);
if (*replace_string_indicator == IS_NOT_NULL)
replace_length = strlen((char *) replace_string);
else
replace_length = 0;
/* Don't search if it is impossible to get a match. */
if ((source_length == 0) ||
(*search_string_indicator == IS_NULL) ||
((search_length = strlen((char *) search_string)) == 0) ||
(search_length > source_length))
{
if (source_length > MAXIMUM_LENGTH)
{
*result_string_indicator = IS_NULL;
(void) sprintf(sqlstate, UDF_ERR_RESULTLENGTH1);
(void) strcpy((char *) error_message, UDF_MSG_RESULTLENGTH1);
return;
}
(void) strcpy((char *) result_string, (char *) input_string);
*result_string_indicator = IS_NOT_NULL;
(void) strcpy(sqlstate, UDF_OK);
error_message[0] = EOS;
return;
}
source_ptr = input_string;
source_limit = input_string + source_length;
match_limit = (source_limit - search_length) + 1;
target_ptr = result_string;
target_limit = result_string + MAXIMUM_LENGTH;
search_ptr = search_string;
search_limit = search_string + search_length;
while (source_ptr < source_limit)
{
if ((*search_ptr == *source_ptr) && (source_ptr < match_limit))
{
/* Possible match. Check remainder of pattern */
save_source_ptr = source_ptr;
while ((++search_ptr < search_limit) &&
(*search_ptr == *(++source_ptr)));
if (search_ptr == search_limit)
{
/* Pattern match. */
++source_ptr;
if (replace_length > 0)
{
/* Substitute new value. */
*target_ptr = EOS;
if (((target_ptr + replace_length) - 1) >= target_limit)
{
*result_string_indicator = IS_NULL;
(void) sprintf(sqlstate, UDF_ERR_RESULTLENGTH2);
(void) strcpy((char *) error_message,
UDF_MSG_RESULTLENGTH2);
return;
}
strcat((char *) target_ptr, (char *) replace_string);
target_ptr += replace_length;
}
}
else
{
source_ptr = save_source_ptr;
if (target_ptr == target_limit)
{
*result_string_indicator = IS_NULL;
(void) sprintf(sqlstate, UDF_ERR_RESULTLENGTH3);
(void) strcpy((char *) error_message,
UDF_MSG_RESULTLENGTH3);
return;
}
*(target_ptr++) = *(source_ptr++);
}
search_ptr = search_string;
}
else
{
if (target_ptr == target_limit)
{
*result_string_indicator = IS_NULL;
(void) sprintf(sqlstate, UDF_ERR_RESULTLENGTH4);
(void) strcpy((char *) error_message, UDF_MSG_RESULTLENGTH4);
return;
}
*(target_ptr++) = *(source_ptr++);
}
}
*target_ptr = EOS;
*result_string_indicator = IS_NOT_NULL;
(void) strcpy(sqlstate, UDF_OK);
error_message[0] = EOS;
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/16723161/viewspace-1034829/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/16723161/viewspace-1034829/