2012-03-19 223 views
3

我试图删除所有空白字符(TAB,CR,LF),我已经找到了最好的表达方式(即实际工作)是:使用Teradata的syslib.oreplace删除所有空格字符

syslib.oreplace(
syslib.oreplace(
    syslib.oreplace(my_string,X'0a',''),X'0d',''),X'09','') 

有什么更优雅的?我不能在一个单独的电话号码中删除所有的电话吗?

+0

我用这个确切的代码来清理一些数据!谢谢!! – banging 2012-05-03 17:22:48

回答

1

我用它是基于isprint() C函数以下UDF:

#define SQL_TEXT Latin_Text 
#include "sqltypes_td.h" 
#include <ctype.h> 
#include <string.h> 
#include <stdlib.h> 
/* 
||------------------------------------------------------------------- 
|| retain_print()         
|| 
|| Description: 
|| Accepts an input string and returns only the printable characters 
|| including space based on the isprint() C function. 
|| 
|| Note: tabs, vertical tabs, form feeds, newlines, and carriage 
|| returns will be stripped. Otherwise, the isspace() function 
|| would have been used to check for space. 
|| 
|| Arguments:          
||  sStrIn:  string to be processed      
||  result:  processed string or spaces if invalid. 
|| 
|| Examples: 
|| retain_print("ABCD. 1234") 
||  returns "ABCD. 1234" 
|| 
|| Sample Use: 
|| SELECT nullif(retain_print('ABCD. 1234'), ' '); 
||------------------------------------------------------------------- 
*/ 
void retain_print(VARCHAR_LATIN *sStrIn, 
        VARCHAR_LATIN *result) 
{ 
    int slen;   /* sStrIn length in chars */ 
    int iposin;  /* position of input string */ 
    int iposout;  /* position of output string */ 
    char strout[10000]; /* output string */   
    /* 
    ||------------------------------------------------ 
    || Loop thru testing for alpha.     
    ||------------------------------------------------ 
    */ 
    iposin = 0; 
    iposout = 0; 
    slen = strlen((const char *) sStrIn); 
    if (slen < 1) { 
     goto ERROR; 
     } 
    while ((slen > iposin)) { 
     /* Check if argument is a printable character (including a space) */ 
     if (isprint(sStrIn[iposin])) { 
      strout[iposout] = sStrIn[iposin]; 
      iposout = iposout + 1; 
     } 
     /* The argument is not a printable character replace with a space */ 
     else { 
      strout[iposout] = ' '; 
      iposout = iposout + 1;   
     } 
     iposin = iposin + 1; 
    } 
    strout[iposout] = '\0'; 
    strcpy((char *) result, strout); 
    goto EXIT; 
ERROR: 
    strcpy((char *) result, " "); 
EXIT: 
    return; 
+0

稍微调整一下,可以利用其他C函数(如isalnum())来扩展UDF库。 – 2012-03-19 13:20:43

+0

whoa,我希望使用更标准的otranslate/oreplace函数,而不是新的UDF,但是我会让我的DBA看看这个,并让您知道这是否对我们有帮助! – ihadanny 2012-03-19 16:08:05

0

不会这项工作?

sel COALESCE(OREPLACE('no spaces',' '), '')