> For the complete documentation index, see [llms.txt](https://docs.ubiquity.co.nz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ubiquity.co.nz/documentation/keep-learning/esl-ubiquitys-scripting-language/functions.md).

# Functions

### If

The if function can be used to swap a simple piece of text out based on whether a condition is true or not. Can also nest these functions.

```
[| If([Database.City] eq "Akl", "Auckland", "Elsewhere") |]
```

***

### And/Or

Use these instead of writing a whole bunch of nested if statements

Example of 'And' function

```
{{ If(|And(eq([merge1], "Yes"), eq([merge2], "Yes"))| is true) }}
 	do this
{{ EndIf }}
```

Example of 'Or' Function

```
{{ If(|Or(eq([merge1], "Yes"), eq([merge2], "Yes"))| is True) }}
 	do this
{{ EndIf }} 
```

***

### String Functions

**Length**

Checks the Length of the Merge field

```
{{ If(|Length([Merge])| LT/GT 15) }}
```

**Right/Left**

Counts in from Left/Right

```
{{ If(|Right/Left([Merge], "1")| neq "s") }}
```

**StartsWith/EndsWith**

If a merge starts with or ends with a specific string

```
{{ If(|StartsWith/EndsWith([Merge], "String")| eq true) }}
```

**Split EndsWith**

If a split merge field ends with a specific string

```
{{ If(|SplitEndsWith([Merge], ";", "String")| eq true) }}
```

**Split Count**

Number of items in a split merge field

```
{{ If(|SplitCount([Merge], ";")| eq "2") }}
```

**Substring**

Substring(str, start) returns the subString starting at the 0-based index start from str. Substring(str, start, length) returns the length character long subString starting at the 0-based index start from str.

This example was used to format a phone number

```
{{ Variable (@left, @middle, @right) }} 
   {{ Assign(@left) }}
      [|Substring([merge], 0, 2)|]
   {{ EndAssign }}
   
   {{ Assign(@middle) }}
      [|Substring([merge], 2, 3)|]
   {{ EndAssign }}
   
   {{ Assign(@right) }}
      [|Substring([merge], 5)|]
   {{ EndAssign }}-->
        
   <p>[@left] [@middle] [@right]</p>
        
{{ EndVariable }}
```

Turns "091234567" to this "09 123 4567".

***

### Find/FindX

The find function can search a merge field for a word, phrase, letter, symbol.

The find will return -1 if not found or a zero based index where the subject is found.

```
{{ If(|Find([Merge], "something")| neq "-1") }}
	Show this if found
{{ EndIf }}
```

The findX function is a regex find and will return the string that matches the suplied regex.<br>

```
{{ If(|FindX([Merge], "((Mr|Mrs|Ms|Miss|Master|Customer|Dr)|(^\w{1}\s.*)|(\s\w$))")| neq "-1") }}
	Show this if found
{{ EndIf }}
```

For example, the following will return "5.5":<br>

```
[|FindX("You can save $5.5 is you...", "[0-9.]+") |]
```

***

### Cast

Cast is used to change a field type when displayed.

This is helpful when you have a string field that is mobile number and want to format it correctly.

```
[| Format(Cast([Merge], "Field Type"), "format") |]
```

or

```
{{ Cast([Database.MobileNumberFieldName] as "MobileNumber") }}[Value(f)]{{ EndCast }}
```

* cast types
* case "int":
* case "integer":
* case "number":
* case "wholenumber":

**return typeof(int);**

* &#x20;case "real":
* case "float":
* case "decimal":

**return typeof(double);**

* &#x20;case "date":

**return typeof(DateTime);**

* &#x20;case "time":

**return typeof(TimeSpan);**

* &#x20;case "yesno":
* case "bool":

**return typeof(bool);**

* &#x20;case "text":

**return typeof(string);**

* &#x20;case "uniqueidentifier":

**return typeof(Guid);**

***

### &#x20;Replace/ReplaceX

Returns replaced string.

```
[|Replace([Merge]/[@variable2]/"Value", "replace this", "with this")|]
```

Replace Regex

```
[|ReplaceX(["doing", "[aeiou]", "_")|] = d__ng
```

*Note: Replace is a string replace whereas ReplaceX is a regex replace.*

Replace 2 strings in 1 function

```
[|Replace(Replace([Merge]/[@variable2]/"Value", "replace this", "with this"), "replace this", "with this")|]
```

*It works from the inside out. So it will replace the inner first, then the outer replace.*

***

### Format Functions

#### Text Transformations

Change text to uppercase

```
[|ToUpper("UbiQuity")|] = UBIQUITY
```

Change text to lowercase

```
[|ToLower("UBIQUITY")|] = ubiquity
```

Change to titlecase

```
[|TitleCase("ubiquity engage")|] = Ubiquity Engage
```

Change to smartcase

```
[|SmartCase("ubiquity & engage")|] = Ubiquity & Engage
```

*Smart case works like title case, however also understands RD and PO Box in addresses so will correctly format these.*

#### Encoding

HtmlEncode

```
[|HtmlEncode("<p>ubiquity & engagelt;/p>")|] = <p>ubiquity & engage</p>
```

*This turns html tags into text*

URL Encode

```
[|UrlEncode("http://engage.ubiquity.co.nz/")|] = http%3A//engage.ubiquity.co.nz/
```

URL Decode

```
[|UrlDecode("http%3A//engage.ubiquity.co.nz/")|] = http://engage.ubiquity.co.nz/
```

Strip Html

```
[|StripHtml("<p>ubiquity & engagelt;/p>")|] = ubiquity & engage
```

*Removes all html*

***

### Math Functions

The Add/Subtract/Multiply/Divide/Mod can take a floating point but will truncate it before performing the operation so there are also floating point functions that will not do this. All math functions can take any number of arguments.

#### Add

Adds two numbers together but rounds down to the nearest whole number

```
[|Add("5.5", "2")|] => 7
```

Adds two numbers together and does not round the output

```
[|AddF("5.5", "2")|] => 7.5
```

#### Subtract

Subtracts two numbers but rounds down to the nearest whole number

```
[|Subtract("5", "2")|] => 3
```

Subtracts two numbers and does not round the output

```
[|SubtractF("5.5", "2")|] => 2.5
```

#### Multiply

Multiplies two numbers but rounds down to the nearest whole number

```
[|Multiply("5", "2")|] => 10
```

Multiplies two numbers and does not round the output

```
[|MultiplyF("5.56", "2")|] => 11.12
```

#### Divide

Divides two numbers but rounds down to the nearest whole number

```
[|Divide("5", "2")|] => 2
```

Divides two numbers and does not round the output

```
[|DivideF("5.56", "2")|]  => 2.78
```

#### Mod

#### Between

Returns true if the first number is between the second and third, can be used for dates too

```
[|Between("5", "1", "10")|]
```

#### IsOdd

Returns true when its parameter is an odd number; false otherwise.

```
[|IsOdd("5")|]
```

#### IsEven

Returns true when its parameter is an even number; false otherwise.

```
[|IsEven("6")|]
```

#### Random

Generates random numbers.

Returns a random floating-point number between 0 (inclusive) and 1 (exclusive):

```
[|Random()|]
```

Returns a random integer between 0 (inclusive) and max (exclusive):

```
[|Random(max)|]
```

Returns a random integer between min (inclusive) and max (exclusive):

```
[|Random(min, max)|]
```

*Note: max must not be less than min.*

***

### Substring Functions

#### Length

Checks the Length of the Merge field

```
{{ If(|Length([Merge])| LT/GT 15) }}
```

#### Right/Left

Counts in from Left/Right

```
{{ If(|Right/Left([Merge], "1")| neq "s") }}
```

#### StartsWith/EndsWith

If a merge starts with or ends with a specific string

```
{{ If(|StartsWith/EndsWith([Merge], "String")| eq true) }}
```

#### Split EndsWith

If a split merge field ends with a specific string

```
{{ If(|SplitEndsWith([Merge], ";", "String")| eq true) }}
```

#### Split Count

Number of items in a split merge field

```
{{ If(|SplitCount([Merge], ";")| eq "2") }}
```

#### Substring

Substring(str, start) returns the subString starting at the 0-based index start from str. Substring(str, start, length) returns the length character long subString starting at the 0-based index start from str.

This example was used to format a phone number

```
{{ Variable (@left, @middle, @right) }} 
  {{ Assign(@left) }}
    [|Substring([merge], 0, 2)|]
  {{ EndAssign }}
   
  {{ Assign(@middle) }}
    [|Substring([merge], 2, 3)|]
  {{ EndAssign }}
   
  {{ Assign(@right) }}
    [|Substring([merge], 5)|]
  {{ EndAssign }}-->
        
  <p>[@left] [@middle] [@right]</p>       
{{ EndVariable }}
```

*Turns "091234567" to this "09 123 4567"*

***

### Date Functions

For date functions that take a datePart, the value is one of:

* Millisecond
* Second
* Minute
* Hour
* Day
* Week
* WeekDay
* DayOfYear
* Month
* Quarter
* Year

#### DateAdd

Date is offset by value dateParts

```
[|DateAdd("datePart", startDate, value)|(format)]
```

Day before mailout send date

```
[| DateAdd("Day", [Email Send Date], -1) |(d MMMM yyyy)]
```

#### DateDiff

Number of date and time boundaries crossed between startDate and endDate

```
[|DateDiff("datePart", startDate, endDate)|]
```

#### DateDelta

Number of whole intervals contained within startDate and endDate

```
[|DateDelta("datePart", startDate, endDate)|]
```

#### DatePart

The requested datePart of date as an integer

```
[|DatePart("datePart", date)|]
```

#### Date

The Date component of date

```
[|Date(date)|]
```

#### Time

The TimeSpan component of date

```
[|Time(date)|]
```

#### Now

Returns the current date and time

```
[|Now()|(format)]
```

<br>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ubiquity.co.nz/documentation/keep-learning/esl-ubiquitys-scripting-language/functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
