Example: Basic Scalar Function | External Routine Programming | Teradata Vantage - Example: Basic Scalar Function - Analytics Database - Teradata Vantage

SQL External Routine Programming

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Analytics Database
Teradata Vantage
Release Number
17.20
Published
June 2022
Language
English (United States)
Last Update
2023-07-11
dita:mapPath
iiv1628111441820.ditamap
dita:ditavalPath
qkf1628213546010.ditaval
dita:id
B035-1147
lifecycle
latest
Product Category
Teradata Vantageā„¢

Here is an example of a scalar function that extracts a substring from an input string given the starting position of the substring. The scalar function uses parameter style SQL.

/*****  C source file name: substr.c  *****/
#define  SQL_TEXT Latin_Text
#include <sqltypes_td.h>
#include <string.h>
#define  IsNull     -1
#define  IsNotNull  0
void udf_scalar_substr( VARCHAR_LATIN *inputString,
                        INTEGER       *start,
                        VARCHAR_LATIN *result,
                        int           *inputStringIsNull,
                        int           *startIsNull,
                        int           *resultIsNull,
                        char          sqlstate[6],
                        SQL_TEXT      extname[129],
                        SQL_TEXT      specific_name[129],
                        SQL_TEXT      error_message[257] )
{
   int inputStrLength;
   /* Return Null value on Null Input */
   if ((*inputStringIsNull == IsNull))
   {
      strcpy(sqlstate, "22004");
      strcpy((char *) error_message, "Null value not allowed.");
      *resultIsNull = IsNull;
      return;
   }
   strcpy((char *) error_message, " ");
   *resultIsNull = IsNotNull;
   inputStrLength = strlen((const char *) inputString);
   if (*start < 1)
   {
      strcpy((char *) result, (const char *) inputString) ;
      return;
   }
   if (*start > inputStrLength)
   {
      strcpy((char *) result, "");
      return;
   }
   strcpy((char *) result,
          (const char *) inputString + (*start - 1));
}