INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Informix 4GL Functions

How do I not reinvent functions previously created by olded
Posted: 22 Aug 02

{
Miscellaneous Informix 4GL functions

abs(val)       # returns the absolute value of a integer
abs_dec10()    # returns the absolute value of a decimal
parse_str()    # parses string by character delimiter
rt_just()      # right justify a string
ret_day()      # takes a date and returns a three character day
replace()      # replace in string occurrences of old pattern with new pattern
is_online()    # returns true if the database is online else false
row_locked()   # tests the sqlca.sqlerrd[2] variable to check if record locked
squeeze()      # squeezes out all the spaces in a string
squ_any_char() # squeezes out a particlar character from a string

Datetime functions:
date_to_str()
return_min()
return_hr()
date_to_int()
secs_to_int()
int_to_date()
dtaddhrs()
yr_of_date()
ck_time()
ck_diget()
}

#main
#   define
#      v1 integer,
#      v2 decimal(10)

#let v1 = -31
#let v2 = -45.78
#let v1=abs(v1)
#let v2=abs_dec10(v2)
#display v1
#display v2
#end main

######################################################################
function abs(val)
######################################################################
# This function will return the absolute value of val

    define val integer

    if val < 0
    then
        let val = -val
    end if
    return val

end function
# abs()

######################################################################
function abs_dec10(val)
######################################################################
# This function will return the absolute value of val

    define val decimal(10)

    if val < 0
    then
        let val = -val
    end if
    return val

end function
# abs_dec10()
#main
#define
#   tmp_string CHAR(20),
#   ret_string CHAR(10),
#   cnt SMALLINT

#let cnt = 1
#let tmp_string = "first|second|third"
#while cnt > 0
#   call parse_str("|", cnt, tmp_string) returning ret_string, cnt
#   if ret_string is not null and ret_string != " "
#   then
#      display ret_string
#   end if
#end while

#end main

# This function searches string for an instance of char_delim
# starting at position cnt and returns the string ending at the next
# char_delim or the end of string
######################################################################
function parse_str(char_delim, cnt, string)
######################################################################
define
   char_delim CHAR(1),   # character delimiter
   cnt SMALLINT,         # postion in string to start counting
   string CHAR(300),     # string to search
   ret_string CHAR(40),  # string returned
   i,                    # index
   x,                    # counter
   string_len SMALLINT   # string length

   LET string_len = LENGTH(string)

   IF string_len = 0 # got a null string
   THEN
     RETURN "",0
   END IF

   IF string[cnt] = char_delim
   THEN
      LET cnt = cnt + 1 # get off the delimiter
   END IF
   IF cnt > string_len
   THEN # return when the end of the string is reached
     RETURN "",0
   END IF

   LET x = 1
   FOR i = cnt TO string_len
      IF string = char_delim
      THEN
         EXIT FOR
      END IF
      LET ret_string[x] = string  # save a character
      LET x = x + 1
   end for

   return ret_string, i # return the positon ended
end function

#main
#   define
#      str char(10)

#let str = rt_just("fred", 10)
#display str
#end main

######################################################################
function rt_just(strng, lngth)
######################################################################
# This function returns a padded string of an argument length.  The
# returned string is right justified with leading blanks.
# The calling routine must include the actual string as well as the
# required length of the string (16 or 64) is anticipated but not
# required.
######################################################################
#
define
  strng char(64),
  lngth, y, z smallint,
  n, m, x, cnt smallint,
  new_string char(64),
  ret_strng char(64)

let new_string = "  "
let cnt = 1
let n = length(strng)
if n = 0 then
   return " "
end if
let y = 0
let z = 0
while y <= n
  let y = y + 1
  if strng[y] = " " then
    continue while
  else
    let z = z + 1
    let new_string[z] = strng[y]
  end if
end while
let m = lngth - z
let x = m+1
while cnt <= m
  let ret_strng[cnt] = " "
  let cnt = cnt + 1
end while
let ret_strng[x,lngth] = new_string
return ret_strng[1,lngth]
end function
# rt_just()

#main
#   define
#      str char(3),
#      ddate date

#   let ddate = today
#   let str = ret_day(ddate)
#   display str
#end main

#################################################################
function ret_day(retday)
#################################################################
# This function takes a date and returns a three character day
# sun thru sat

    define
       retday date,
       x smallint

    let x = weekday(retday)

    case
       when x = 0
          return "SUN"
       when x = 1
          return "MON"
       when x = 2
          return "TUE"
       when x = 3
          return "WED"
       when x = 4
          return "THU"
       when x = 5
          return "FRI"
       when x = 6
          return "SAT"
       otherwise
          return "NON"
    end case

end function
# ret_day()

#main
#define
#   string char(80)

#   let string = replace("The Old Thing", "Old", "New")
#   display string
#end main

# This function replaces all occurrences of old pattern
# in the string with the new pattern.  After all
# replacements are completed, this function returns the modified string.  
function replace(string, old_pattern, new_pattern)
define
   string char(512),      # string for replacement.
   old_pattern char(20),  # pattern to be found in the string.
   new_pattern char(20),  # string to replace the old pattern.
   i,                     # index
   old_len,               # length of the old_pattern
   new_len,               # length of the new_pattern
   str_len smallint       # length of the original string

# get the lengths of the parameters passed in.
let old_len = length(old_pattern clipped)
let new_len = length(new_pattern clipped)
let str_len = length(string clipped)

if old_len = 0 or str_len = 0 then   # got a null string
  return string  # without any changes being made
end if

# Set the index pointer into the string, 0 = before first character.
let i = 0

# Go through each character of the source string, one at a time.
while i <= (str_len - old_len + 1)
   # update the index pointer
   let i = i + 1

   # See if the old pattern is found starting at the i position
   if string[i,(i+old_len-1)] = old_pattern
   then # replace
      if i = 1
      then # old pattern is at the front of the source string
         if string = old_pattern
         then # replacing one word
            let string = new_pattern clipped
         else # more after this word, thus tack the remaining on to the end
            let string = new_pattern clipped,
                 string[(i+old_len),str_len] clipped
         end if
      else # the pattern is buried in the middle or at the end of the string
         if i+old_len-1 = str_len
         then # at the end of the string
            let string = string[1,(i-1)],new_pattern
         else # buried in the middle, so something to tack on to the end
            let string = string[1,(i-1)],new_pattern clipped,
                string[(i+old_len),str_len] clipped
         end if
      end if
      # adjust string length and adjust i to bypass the new pattern
      let str_len = length(string  clipped)
      let i = i + new_len - 1
   end if
end while

# return the modified string
return string
end function
# replace()

#main
#database jet
#if is_online()
#then
#   display "we're running online"
#else
#   display "we're probably running S.E."
#end if
#end main

######################################################################
function is_online()
# returning true if using the online engine else false
######################################################################

   select rowid from syscolumns
       where colname = "dirpath" and tabid = 1
   if sqlca.sqlcode = notfound
   then
      return true
   else
      return false
   end if
end function
# is_online()

#######################################################################
function row_locked()
#######################################################################
# This function tests the sqlca.sqlerrd[2] variable to determine if
# a lock can be granted.  Returns true if locked and false if not.
# sample sqlca.sqlerrd[2] immediately after the data management statement
# being tested.


    define locked smallint

    case
      when sqlca.sqlerrd[2] = -107
        let locked = true

      when sqlca.sqlerrd[2] = -113
        let locked = true

      when sqlca.sqlerrd[2] = -134
        let locked = true

      when sqlca.sqlerrd[2] = -143
        let locked = true

      when sqlca.sqlerrd[2] = -144
        let locked = true

      when sqlca.sqlerrd[2] = -154
        let locked = true

      otherwise
        let locked = false

    end case

    return locked

end function
# row_locked()

#main
#   define scratch char(50)

#let scratch = squeeze("a line needs the spaces removed")
#display scratch
#end main

########################################################################
function squeeze(source_string)
########################################################################
# This function returns the passed in string with all of the
# spaces squeezed out of it.
#
# source_string  String to be searched.
#

    define source_string char(512),
           source_len,   # source string length
           i,j smallint  # counters

    # get the length of the source string
    let source_len = length(source_string clipped)

    # step through the string one character at a time.
    for i = source_len to 1 step -1
        if source_string = " "
        then # squeeze it!
            if i < 80
            then
            # move all characters to the right of the space,
            # one position to the left.
                for j = i to source_len-1
                    let source_string[j] = source_string[j+1]
                end for
                # blank last character to remove repeating last character
                let source_string[source_len] = " "
            end if
         end if
    end for

    # return the squeezed string
    return source_string clipped

end function
# squeeze(source_string)

#main
#   define scratch char(50)

#let scratch = squ_any_char("first|second|third", "|")
#display scratch
#end main

########################################################################
function squ_any_char(string, any_char)
########################################################################
# This function returns the passed in string with all of
# any_char squeezed out of it.
#

define string char(512),
       any_char char(1), # character to squeeze
       len smallint,     # source string length
       i,j smallint      # counters

# get the length of the source string
let len = length(string clipped)

# step through the string one character at a time.
for i = len to 1 step -1
  if string = any_char then # squeeze it!
    if i < 80 then
      # move all characters to the right of the space, one position to the left.
      for j = i to len-1
        let string[j] = string[j+1]
      end for
      # blank last character to remove repeating last character
      let string[len] = " "
    end if
  end if
end for

# return the squeezed string
return string clipped
end function
# squ_any_char()

# There functions build datetime datatypes from a date and a time string.
# They also return the number of minutes and hours between two datetimes
# The interval between the two datetimes can't go over the scale of the
# defined datetime datatypes or  these functions break.
# The return_min or return_hr functions work on an informix datatype
# interval.  Be advised, you may have to change the range of the
# datetime and interval parameters to suit your purposes.
main
define dt_in, dt_out datetime year to fraction(5),
       dinter interval day to minute,
       date_in, date_out, ddate date,
       time_in, time_out, ttime char(4),
       tot_min integer,
       tot_hrs decimal(10,2),
       err_found smallint

    whenever error continue
    let date_in = "08/20/2002"
    let time_in = "1330"
    let date_out = "08/21/2002"
    let time_out = "1300"
    
    # build date times
    call date_to_int(date_in, time_in) returning dt_in, err_found
    call date_to_int(date_out, time_out) returning dt_out, err_found
    let dinter = dt_out - dt_in
    
    # get the number of minutes between the two datetimes
    let tot_min = return_min(dinter)
    display "Total minutes between in and out: ", tot_min

    # get the number of seconds between the two datetimes
    let tot_hrs = return_hr(dinter)
    display "Total hours between in and out: ", tot_hrs

    # break out the date and time from a datetime type
    call int_to_date(dt_in) returning ddate, ttime, err_found
    display "Date is ", ddate
    display "Time is ", ttime

end main

###########################################################################
function date_to_str(dd)
###########################################################################
# This function takes a date type and returns a string of the type:
# yyyy-mm-dd

    define dd date,
           bufyy, bufmm, bufdd smallint,
           daystr char(14)

    let bufyy = year(dd)
    let bufmm = month(dd)
    let bufdd = day(dd)

    let daystr = bufyy using "####", "-"
    let daystr = daystr clipped, bufmm using "##", "-"
    let daystr = daystr clipped, bufdd using "##"

    if daystr is null
    then
        let daystr = ""
    end if

     return daystr
end function
# date_to_str(dd)

###########################################################################
function return_min(dinter)
###########################################################################
# This function takes an interval string and then returns the number of
# minutes.  This is only works if the interval is defined day-to-minute

    define dinter interval day to minute,
           dstring char(14),
           dday char(4),
           dhr  char(2),
           dmin  char(2),
           aday smallint,
           ahr smallint,
           amin smallint,
           totmin integer
    
    let dstring = dinter
    #do the day
    let dday[1] = dstring[1]
    let dday[2] = dstring[2]
    let dday[3] = dstring[3]
    #do the time
    let dhr[1] = dstring[5]
    let dhr[2] = dstring[6]
    let dmin[1] = dstring[8]
    let dmin[2] = dstring[9]

    #change data type
    let aday = dday
    let ahr = dhr
    let amin = dmin

    #if negative interval
    if dday[1] = "-" or
       dday[2] = "-" or
       dday[3] = "-"
    then
       let ahr = ahr * (-1)
       let amin = amin * (-1)
    end if
    let totmin = (aday * 24 * 60) + (ahr * 60) + amin

    if totmin is null
    then
        let totmin = 0
    end if

    return totmin
end function
# return_min()

###########################################################################
function return_hr(dinter)
###########################################################################
# This function takes an interval string and then returns the number of
# hours.  This is only works if the interval is defined day-to-minute

    define dinter interval day to minute,
           dstring char(14),
           dday char(4),
           dhr  char(2),
           dmin  char(2),
           aday smallint,
           ahr smallint,
           amin smallint,
           tothr decimal(10)

    let dstring = dinter
    #do the day
    let dday[1] = dstring[1]
    let dday[2] = dstring[2]
    let dday[3] = dstring[3]
    #do the time
    let dhr[1] = dstring[5]
    let dhr[2] = dstring[6]
    let dmin[1] = dstring[8]
    let dmin[2] = dstring[9]

    #change data type
    let aday = dday
    let ahr = dhr
    let amin = dmin
    
    #if negative interval
    if dday[1] = "-" or
       dday[2] = "-" or
       dday[3] = "-"
    then
       let ahr = ahr * (-1)
       let amin = amin * (-1)
    end if
    let tothr = (aday * 24) + ahr + (amin/60.0)

    if tothr is null
    then
        let tothr = 0.0
    end if

    return tothr
end function
# return_hr()

###########################################################################
function date_to_int(dd,ss)
###########################################################################
# This function takes a date and time string and returns an informix date
# time year to minute.  For error checking, if the date or time is bad,
# typically null, return true else false if conversion was good
# this was modified from base datetime conversion which was datetime
# year to minute to handle datetime year to fraction(5)
    define dd date,
           ss char(4),
           retstr datetime year to fraction(5),
           bufstr char(25),
           daystr char(14),
           time_buf char(5)
        
    let retstr = current

    if dd is null or dd = 0
    then  #date error
       return retstr, true
    end if
    
    if not ck_time(ss)
    then
       return retstr, true
    end if

    #do the time
    let time_buf[1] = ss[1]
    let time_buf[2] = ss[2]
    let time_buf[3] = ":"
    let time_buf[4] = ss[3]
    let time_buf[5] = ss[4]
    #do the date
    let daystr = date_to_str(dd)
    
    #do the date string
    let bufstr = daystr clipped, " ", time_buf clipped,
      ":00.00000"
    let retstr = bufstr
    
    return retstr,false
end function
# date_to_int(dd,ss)

###########################################################################
function secs_to_int(dd,ss)
###########################################################################
# This function takes a date and time string and returns an informix date
# time year to fraction(5).  For error checking, if the date or time is bad,
# typically null, return true else false if conversion was good

    define dd date,
           ss char(6),
           retstr datetime year to fraction(5),
           bufstr char(25),
           daystr char(14),
           time_buf char(8)
        
    let retstr = current

    if dd is null or dd = 0
    then  #date error
       return retstr, true
    end if
    
    if not ck_time(ss)
    then
       return retstr, true
    end if

    #do the time
    let time_buf[1] = ss[1]
    let time_buf[2] = ss[2]
    let time_buf[3] = ":"
    let time_buf[4] = ss[3]
    let time_buf[5] = ss[4]
    let time_buf[6] = ":"
    let time_buf[7] = ss[5]
    let time_buf[8] = ss[6]
    #do the date
    let daystr = date_to_str(dd)
    
    #do the date string
    let bufstr = daystr clipped, " ", time_buf clipped,
      ".00000"
    let retstr = bufstr
    
    return retstr,false
end function
# secs_to_int(dd,ss)

###########################################################################
function int_to_date(p_date)
###########################################################################
# Receives a datetime variable and returns a date and char(4) with time

    define p_date datetime year to fraction(5),
           r_date date,
           date_str char(25),
           r_time char(4)

    if p_date is null
    then
       let r_date = null
       let r_time = "0000"
       return r_date,r_time, 1
    end if

    let r_date = mdy(month(p_date),day(p_date),year(p_date))
    let date_str = p_date

    let r_time[1] = date_str[12]
    let r_time[2] = date_str[13]
    let r_time[3] = date_str[15]
    let r_time[4] = date_str[16]

    return r_date,r_time, 0
end function
# int_to_date()

#############################################################################
function dtaddhrs(dt, hrs)
#############################################################################
#  RETURNS:  dt -  The datetime value (dt) after adding hours (hrs) to it.
#############################################################################
#  Used to add hours (consisting of hours and a fraction of hours) to
#  a given datetime value and return the result to the calling function.
#
# [
    define
        dt      datetime year to fraction(5),  # Datetime value to add to
        hrs     decimal(8,2),             # Hours to add to datetime value
        mins    smallint                  # Minutes conversion factor
   
    let mins  = (hrs * 60)
    let dt    = dt + mins units minute

    return dt

end function
# dtaddhrs()

############################################################################
function yr_of_date(din, digs)
############################################################################
#  RETURNS:  yr - year of the given date value (din).  -1 indicates an error.
############################################################################
#  Used to extract the year from a date value.
#
# [
    define
        din        date,      # A value of null specifies "today" as the date
        digs       smallint,  # Digits of precision (2 or 4--default is 2)
        dstr       char(20),  # Date time string value
        yrstr      char(4),   # Date time string value
        yr         smallint   # The extracted year

    if din is null
    then
        let din = today
    end if

    # Default to current year
    if digs = 4
    then
        let dstr = extend(din, year to month)
        let yrstr = dstr[1,4]
    else
        let dstr = din
        let yrstr = dstr[7,8]
    end if
    let yr = yrstr

    if yr is null then let yr = -1 end if

    return yr

end function
# yr_of_date()

######################################################################
function ck_time(f_time)
######################################################################
# This function returns true if the input value of f_time
# is in the correct hhmm format and doesn't exceed 2359
# returning 0 or -1

    define f_time char(4),
           m1, m2, m3, m4 char(1),
           i1, i2, i3, i4 smallint,
           hrs, mns smallint

    let m1 = f_time[1]
    let m2 = f_time[2]
    let m3 = f_time[3]
    let m4 = f_time[4]

    #check each diget being numeric
    if not ck_diget(m1) or not ck_diget(m2) or not ck_diget(m3) or
       not ck_diget(m4)
    then
        return false
    end if

    let i1 = m1
    let i2 = m2
    let i3 = m3
    let i4 = m4

    let hrs = (i1 * 10) + i2
    let mns = (i3 * 10) + i4
    
    if hrs > 23 or mns > 59
    then
        return false
    else
        return true
    end if

end function
# ck_time()

#########################################################
function ck_diget(cc)
#########################################################
    define cc char(1)

    if cc is null
    then
        return false
    end if

    if cc = "0" or cc = "1" or cc = "2" or cc = "3" or cc = "4"
       or  cc = "5" or cc = "6" or cc = "7" or cc = "8" or cc = "9"
    then
        return true
    else
        return false
    end if

end function
# ck_diget()

Back to Informix solutions FAQ Index
Back to Informix solutions Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close