- 26 Jul 2024
- 3 minute read

- Print
- DarkLight
- PDF

# Using Formulas in Queries

- Updated 26 Jul 2024
- 3 minute read

- Print
- DarkLight
- PDF

Query formulas are a powerful tool for SIS exports and data integration, making it possible to export your data in precisely the format and layout required by external systems. Query Formula Export Parts can be used to:

perform mathematical calculations

combine multiple fields into one export column

write if/then statements

and otherwise manipulate data in the Query Builder.

## Adding a Formula

1. Navigate to the Edit Query view of the query and select

Formulaas the subquery Output.2. In the Edit Part window:

Give the formula part a

Name. Like other export parts in a query, this becomes the column header in the data export.

Add Export: The corresponding export must be added in this section for each variable used in the formula. For example, if the formula calculates a value based on a person's GPA and highest overall max ACT score, you'll want to add an export for GPA and one for the appropriate ACT score.3. Edit each newly added export:

Change the name of the export to ensure that it does not contain any spaces or special characters (except underscore). Names must be unique within the formula.

Verify that the format type for the export is compatible with the formula you are creating (

Intfor integers,Realfor real numbers,Stringfor words/character strings, etc.). This can be done by double clicking on the export and changing the Format Type. The export value, translation code, or a custom null value can also be used. Performing a mathematical operation on a combination of numbers and strings, as an example, may yield unexpected results.4.

Formula:Enter the formula and add`@`

before the export name to reference it in the formula. A few functions can be pre-populated in the Edit Part window. These can be accessed by typing`#`

in the Formula box.

## Common Functions

### Mathematical Operations

Most mathematical functions can be done using a formula. The data type must be compatible with the operation you are performing. When in doubt, use the data type of *real*.

#### Examples

`@merit_aid + @fafsa_aid`

`@tuition - @merit_aid`

`@merit_aid * 4.0`

`@total_merit_aid / 4.0`

## Case Statement

Returns different results based on certain conditions, like an 'if...then...' statement. It goes through the conditions in order and will return the result for the first condition met. If no conditions are met, the END result will be returned. If there is no END result, a null value will be returned.

#### Syntax

```
case
when condition1 then result1
when condition2 then result2
when conditionN then resultN
else result3
end
```

#### Example

```
case @person_student_type
when 'TFR' then 'This is a transfer'
when 'GRD' then 'This is a graduate'
else 'This is a first-year'
end
```

## Coalesce

Returns the first non-null value in a series of values.

#### Syntax

`coalesce(@value1, @value2, @valueN)`

#### Example

`coalesce(@application_program, @person_program)`

## Convert

Changes the data type of a stored value for the export or filter. Data types that can be used:

date

datetime

int (integer)

money

real

string (characters)

This can be useful if you are hoping to perform mathematical functions on values that may not be stored as real numbers or integers. Most data types can be altered by double-clicking on the export and changing the 'Format Type.'

#### Syntax

`try_convert(data_type, @value)`

#### Example

`try_convert(varchar(max), @record_guid)`

## Date Addition

Adds a unit of time, using various units:

minute

hour

day

month

year

to a date and then returns the new date. Negative interval values can be used to subtract a time from a specified date as well.

#### Syntax

`dateadd(time_unit, interval_value, @value)`

#### Example

`dateadd(day, 4, @application_created_date)`

## Date Difference

Subtracts Value 1 from Value 2 in various units of time: minute, hour, day, month, year. If you want to calculate Age, use the standard Person exports of 'Age (as of date)' and 'Current Age Based on Birthdate.'

#### Syntax

`datediff(time_unit, @value1, @value2)`

#### Example

`datediff(day, @application_created_date, @application_submitted_date)`

## Like

Searches a value to see if it contains certain characters or numbers. Wildcards '%' can represent zero, one, or multiple variable characters.

#### Syntax

`@value LIKE 'pattern'`

`@value LIKE '%pattern'`

`@value LIKE 'pattern%'`

#### Examples

`@person_last LIKE 'Johnson'`

`@person_email LIKE '%@technolutions.com'`

## Null If

Returns Value 1 if the values are not equal or returns a null value if they are equal. Can be used to avoid a mathematical error if dividing, and the denominator could be 0.

#### Syntax

`nullif(@value1, @value2)`

#### Example

`@email_interactions / nullif(@total_interactions, 0)`