- 22 Nov 2023
- 3 minute read

- Print
- DarkLight
- PDF

# Using Formulas In Queries for Student Success

- Updated 22 Nov 2023
- 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

Navigate to the Edit Query view of the query, insert a Subquery Export and select

**Formula**as the Output and give the part a name.In the Edit Part window:

**Add Exports**: The corresponding export must be added in this section for each variable used in the formula. For example, if the formula is being used to find the average risk score for a student, all scores need to be added as exports. Here, we are taking the average of the following reported ratings:

Edit each newly added export:

Click on each export and alter the name removing any spaces or special characters (except underscores and/or dashes). Names must be unique within the formula.

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

*Int*for integers,*Real*for real numbers,*String*for 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.

Create the 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 '#' (shift + 3) in the Formula box.In this example, we are adding four different scores and then dividing by 4.

## 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 'Senior' then 'This is a Senior'
when 'Junior' then 'This is a Junior'
when 'Sophomore' then 'This is a Sophomore'
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(@major_enrolled, @major_requested)`

### 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, @major_declaration_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, @major_requested_date, @major_declaration_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)`