# Step: Retype

## Overview

The **Retype** step converts a variable of a given type to another type.

#### Example starting data

```
/*---------+--------*
 | student | score  |
 +---------+--------+
 | jane    | 83     |
 | neal    | 35     |
 | sam     | 74     |
 | pat     | 62     |
 *---------+--------*/
```

#### Example output data:

Retype `score` from integer to float.

```
/*---------+--------*
 | student | score  |
 +---------+--------+
 | jane    | 83.0   |
 | neal    | 35.0   |
 | sam     | 74.0   |
 | pat     | 62.0   |
 *---------+--------*/
```

## Step structure

<figure><img src="https://1672950126-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LVodLwUXgJUGcm5Cvso%2Fuploads%2FgBXE42TAnqkO96WIi97H%2FScreenshot%202023-05-04%20at%2011.13.40%20AM.png?alt=media&#x26;token=53d4de9d-d7e4-49c8-9c4d-2185179890b8" alt=""><figcaption></figcaption></figure>

* There will be at least one retype block where you will define a variable and a new type.

## Field definitions

<table><thead><tr><th width="239">Field</th><th>Definition</th></tr></thead><tbody><tr><td><strong>Source variable</strong></td><td>The variable that you want to retype.<br><br>Note that you can see its current type by locating it in the variable selector at the bottom of the page, or hovering over the variable in this selection menu to see a tooltip with more information.</td></tr><tr><td><strong>New type</strong></td><td>The type that the <code>Source variable</code> will be converted to. Can be any of the <a href="../../../tables/variables#variable-types">supported variable types</a> on Redivis. More on <a href="#reference-type-conversion">conversion rules</a>.</td></tr><tr><td><strong>If invalid for type, set to null</strong></td><td>Whether failing type conversions should be converted to a <code>null</code> value. By default, failed type conversions will throw an error.<br><br>Note that this might significantly change the content of your data and we suggest using this option with full understanding of how it might affect your outcome and verifying the output results.</td></tr><tr><td><strong>Specify custom format</strong></td><td>Informs how the data will be read by the method. Only relevant for conversions of strings to date, time, and dateTime. <a href="#reference-type-conversion">More on format elements</a>.</td></tr></tbody></table>

## Examples

### Example 1: Basic usage

We can convert sales data currently stored as an integer type into float to use in another formula elsewhere that only accept the float type.

#### Starting data:

```
/*---------+-------+---------+------------*
 | test    | score | student | date       |
 +---------+-------+---------+------------+
 | quiz    | 83    | jane    | 2020-04-01 |
 | quiz    | 35    | pat     | 2020-04-01 |
 | quiz    | 89    | sam     | 2020-04-01 |
 | midterm | 74    | jane    | 2020-05-01 |
 | midterm | 62    | pat     | 2020-05-01 |
 | midterm | 93    | sam     | 2020-05-01 |
 | final   | 77    | jane    | 2020-06-01 |
 | final   | 59    | pat     | 2020-06-01 |
 | final   | 92    | sam     | 2020-06-01 |
 *---------+-------+---------+------------*/
```

#### Input fields:

<figure><img src="https://1672950126-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LVodLwUXgJUGcm5Cvso%2Fuploads%2F9fkP3UNxTXdZsdwDF8Lf%2FScreenshot%202023-05-04%20at%2011.14.31%20AM.png?alt=media&#x26;token=1ce7f01e-0384-40ca-acaf-fb557a536b77" alt=""><figcaption></figcaption></figure>

* **Source variable:** The variable we want to convert is `score` so we select it here.
* **New type:** We want this to be a float, and since the score variable is currently an integer it is compatible with conversion to the float type, so we can select `Float` here.
* **If invalid for type, set to null:** Since there are no incompatible values in this variable it doesn't matter what we choose. We leave it unchecked to validate that we understand our data and to confirm that this transform will execute without failing.

**Output data:**

```
/*---------+-------+---------+------------*
 | test    | score | student | date       |
 +---------+-------+---------+------------+
 | quiz    | 83.0  | jane    | 2020-04-01 |
 | quiz    | 35.0  | pat     | 2020-04-01 |
 | quiz    | 89.0  | sam     | 2020-04-01 |
 | midterm | 74.0  | jane    | 2020-05-01 |
 | midterm | 62.0  | pat     | 2020-05-01 |
 | midterm | 93.0  | sam     | 2020-05-01 |
 | final   | 77.0  | jane    | 2020-06-01 |
 | final   | 59.0  | pat     | 2020-06-01 |
 | final   | 92.0  | sam     | 2020-06-01 |
 *---------+-------+---------+------------*/
```

Our output table looks as it should when we look at the cells in the output table, and we can confirm the new type by clicking on the variable in the output table to check the type.

### Example 2: Handling invalid conversions

Lets say instead that in our initial data, the `sales` variable was stored as a string. Converting this to a float would be a bit trickier since the data entry wasn't as clean.

**Starting data:**

```
/*---------+-------+---------+------------*
 | test    | score | student | date       |
 +---------+-------+---------+------------+
 | quiz    | 83    | jane    | 2020-04-01 |
 | quiz    | 35%   | pat     | 2020-04-01 |
 | quiz    | 89    | sam     | 2020-04-01 |
 | midterm | 74    | jane    | 2020-05-01 |
 | midterm | 62    | pat     | 2020-05-01 |
 | midterm | 93    | sam     | 2020-05-01 |
 | final   | 77    | jane    | 2020-06-01 |
 | final   | 59    | pat     | 2020-06-01 |
 | final   | 92    | sam     | 2020-06-01 |
 *---------+-------+---------+------------*/
```

#### Input fields:

<figure><img src="https://1672950126-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LVodLwUXgJUGcm5Cvso%2Fuploads%2FiioWp4rT2nhQ1RxDyXET%2FScreenshot%202023-05-04%20at%2011.14.53%20AM.png?alt=media&#x26;token=ccb4e348-37ac-4b64-8395-fa56ab06a4f5" alt=""><figcaption></figcaption></figure>

* **Source variable:** Same as above example.
* **New type:** Same as above example.
* **If invalid for type, set to null:** Since this data has `35%` as a value, this can't be converted to a float. If we leave this box unchecked our transform will fail. Checking it will set that value to `null`.

#### Output data:

```
/*---------+-------+---------+------------*
 | test    | score | student | date       |
 +---------+-------+---------+------------+
 | quiz    | 83.0  | jane    | 2020-04-01 |
 | quiz    | null  | pat     | 2020-04-01 |
 | quiz    | 89.0  | sam     | 2020-04-01 |
 | midterm | 74.0  | jane    | 2020-05-01 |
 | midterm | 62.0  | pat     | 2020-05-01 |
 | midterm | 93.0  | sam     | 2020-05-01 |
 | final   | 77.0  | jane    | 2020-06-01 |
 | final   | 59.0  | pat     | 2020-06-01 |
 | final   | 92.0  | sam     | 2020-06-01 |
 *---------+-------+---------+------------*/
```

{% hint style="warning" %}
Note that while this retype was successful, our data might not be what we want, and in this case removes information.
{% endhint %}

### Example 3: Parsing dates

Let's say we want to convert our year variable which is currently a string to a Date variable type, but that the starting format does not cleanly translate.&#x20;

#### Starting data:

```
/*---------+-------+---------+------------*
 | test    | score | student | date       |
 +---------+-------+---------+------------+
 | quiz    | 83    | jane    | 04/01/2020 |
 | quiz    | 35    | pat     | 04/01/2020 |
 | quiz    | 89    | sam     | 04/01/2020 |
 | midterm | 74    | jane    | 05/01/2020 |
 | midterm | 62    | pat     | 05/01/2020 |
 | midterm | 93    | sam     | 05/01/2020 |
 | final   | 77    | jane    | 06/01/2020 |
 | final   | 59    | pat     | 06/01/2020 |
 | final   | 92    | sam     | 06/01/2020 |
 *---------+-------+---------+------------*/
```

#### Input fields:

<figure><img src="https://1672950126-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LVodLwUXgJUGcm5Cvso%2Fuploads%2FEnFejNggJgM7fqau6mEd%2FScreenshot%202023-05-04%20at%2011.16.40%20AM.png?alt=media&#x26;token=350cd23e-897f-479f-bae5-3f2c4a72ca44" alt=""><figcaption></figcaption></figure>

* **Source variable:** The variable we want to convert is `date` so we select it here.
* **New type:** We want this to be a date so we can select `Date` here.
* **If invalid for type, set to null:** Since there are no incompatible values in this variable it doesn't matter what we choose. We leave it unchecked to validate that we understand our data and to confirm that this transform will execute without failing.
* **Specify custom format:** Since our data does not fit the standard format (%Y-%m-%d, e.g. `2020-10-01`) we need to specify what format it is in. We can use the reference table at the bottom of this page to specify our format: MM/DD/YYYY

#### Output data:&#x20;

```
/*---------+-------+---------+------------*
 | test    | score | student | date       |
 +---------+-------+---------+------------+
 | quiz    | 83    | jane    | 2020-04-01 |
 | quiz    | 35    | pat     | 2020-04-01 |
 | quiz    | 89    | sam     | 2020-04-01 |
 | midterm | 74    | jane    | 2020-05-01 |
 | midterm | 62    | pat     | 2020-05-01 |
 | midterm | 93    | sam     | 2020-05-01 |
 | final   | 77    | jane    | 2020-06-01 |
 | final   | 59    | pat     | 2020-06-01 |
 | final   | 92    | sam     | 2020-06-01 |
 *---------+-------+---------+------------*/
```

## Reference: Type conversion&#x20;

Note you can see more information on Redivis variable types [here](https://docs.redivis.com/tables/variables#variable-types).

| Starting type | Possible destinations                                                       | Notes                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| ------------- | --------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| String        | <p>Integer<br>Float<br>Boolean<br>Date<br>DateTime<br>Time<br>Geography</p> | <p>To integer: A hex string can be cast to an integer. For example, <code>0x123</code> to <code>291</code> or <code>-0x123</code> to <code>-291</code>.<br><br>To float: Returns <code>x</code> as a floating point value, interpreting it as having the same form as a valid floating point literal.<br><br>To boolean: Returns <code>TRUE</code> if <code>x</code> is <code>"true"</code> and <code>FALSE</code> if <code>x</code> is <code>"false"</code>. All other values of x are invalid and throw an error instead of casting to a boolean. A string is case-insensitive when converting to a boolean.<br><br>To date, dateTime, or time: Uses the canonical format by default (see information below)</p> |
| Integer       | <p>String</p><p>Float<br>Boolean</p>                                        | <p>To float: Returns a close but potentially not exact floating point value.</p><p></p><p>To boolean: Returns <code>FALSE</code> if <code>x</code> is <code>0</code>, <code>TRUE</code> otherwise.</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| Float         | <p>String<br>Integer</p>                                                    | To integer: Returns the closest integer value. Halfway cases such as 1.5 or -0.5 round away from zero.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| Boolean       | <p>String<br>Integer</p>                                                    | <p>To string: Returns <code>true</code> if x is true, <code>false</code> otherwise.<br><br>To integer: Returns <code>1</code> if x is true, <code>0</code> otherwise.</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| Date          | String                                                                      |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| DateTime      | <p>String<br>Date<br>Time</p>                                               | To date, dateTime, or time: Uses the canonical format by default (see information below)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| Time          | <p>String<br>Date<br>DateTime</p>                                           | To date, dateTime, or time: Uses the canonical format by default (see information below)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| Geography     | String                                                                      |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |

## Reference: Cannonical representation

When retyping between a String type variable and a Date, DateTime, or Time type variable it is presumed that the data will be in the format below.&#x20;

|          | Layout                                                                                                                                   | Example                                             |
| -------- | ---------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------- |
| Date     | <p><em>(Four digit year)-</em><br><em>(1 or 2 digit month)-</em><br><em>(1 or 2 digit date)</em></p>                                     | <p>2023-01-01<br>2023-1-1</p>                       |
| Time     | <p>(1 or 2 digit hour):<br>(1 or 2 digit minute):<br>(1 or 2 digit second).<br>(Up to 6 fractional seconds)</p>                          | <p>01:01:01.123456<br>6:2:9<br>22:19:3</p>          |
| DateTime | <p><em>(Date specification)</em><br><em>(space </em> <em> or <code>T</code> or <code>t</code>)</em><br><em>(Time specification)</em></p> | <p>2023-01-01 01:01:01.123456<br>2023-1-1T6:2:9</p> |

If it is not in this canonical format you can click the **Specify custom format** field and use Format elements (below) to indicate otherwise.

## Reference: Format elements

Since Date, DateTime, and Time variable types contain structured information you can use format strings to indicate how you want different pieces of date and time information translated to and from string format when retyping.

For example when converting a date to a string you can choose whether it will become JAN 1 2023 or 2023-01-01. When converting from a string to a DateTime you'll need to outline how the information is structured in your data so it can be read in correctly.

You can use these elements in the **Specify custom format** field.&#x20;

| Element                                                                                                                | Return                                                                                                                                                                           |                                                                                                                                                                                                                                              |
| ---------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `YYYY`                                                                                                                 | Four (or more) digit year                                                                                                                                                        | <p>Input: 2023-01-01<br>Output: 2023</p><p></p><p>Input: 23-01-01<br>Output: 0023</p><p></p><p>Input: 20000-01-01<br>Output: 20000</p>                                                                                                       |
| `YYY`                                                                                                                  | Last three digit year                                                                                                                                                            | <p>Input: 2023-01-01<br>Output: 023</p><p></p><p>Input: 23-01-01<br>Output: 023</p>                                                                                                                                                          |
| `YY`                                                                                                                   | Two digit year                                                                                                                                                                   | <p>Input: 2023-01-01<br>Output: 23</p><p></p><p>Input: 2-01-30<br>Output: 02</p>                                                                                                                                                             |
| Y                                                                                                                      | Last one digit of year                                                                                                                                                           | <p>Input: 2023-01-01<br>Output: 3</p>                                                                                                                                                                                                        |
| `MM`                                                                                                                   | Two digit month                                                                                                                                                                  | <p>Input: 2023-01-01 <br>Output: 23</p>                                                                                                                                                                                                      |
| `MON`                                                                                                                  | <p>Three character month:<br>JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC</p>                                                                                      | <p>Input: 2023-01-01 <br>Output: JAN</p>                                                                                                                                                                                                     |
| `MONTH`                                                                                                                | Month name                                                                                                                                                                       | <p>Input: 2023-01-01 <br>Output: JANUARY</p>                                                                                                                                                                                                 |
| `DDD`                                                                                                                  | Three digit day of the **year**                                                                                                                                                  | <p>Input: 2023-01-01 <br>Output: 001</p>                                                                                                                                                                                                     |
| `DD`                                                                                                                   | Two digit day of the **month**                                                                                                                                                   | <p>Input: 2023-01-01 <br>Output: 01</p>                                                                                                                                                                                                      |
| `D`                                                                                                                    | Day of the week (1-7) with Sunday being 1                                                                                                                                        | <p>Input: 2023-01-01 <br>Output: 1</p>                                                                                                                                                                                                       |
| `DAY`                                                                                                                  | Day of the week. Spaces are padded on the right side to make the output size exactly 9.                                                                                          | <p>Input: 2023-01-01 <br>Output: SUNDAY</p>                                                                                                                                                                                                  |
| `DY`                                                                                                                   | <p>Three character day:<br>MON, TUE, WED, THU, FRI, SAT, SUN</p>                                                                                                                 | <p>Input: 2023-01-01 <br>Output: SUN</p>                                                                                                                                                                                                     |
| `HH`                                                                                                                   | Two digit hour of the day (valid values from 00 to 12)                                                                                                                           | <p>Input: 20:10:15<br>Output: 10</p>                                                                                                                                                                                                         |
| `HH12`                                                                                                                 | Hour of the day (valid values from 00 to 12)                                                                                                                                     | <p>Input: 20:10:15<br>Output: 10</p>                                                                                                                                                                                                         |
| `HH24`                                                                                                                 | Two digit hour (valid values from 00 to 24)                                                                                                                                      | <p>Input: 20:10:15<br>Output: 20</p>                                                                                                                                                                                                         |
| `MI`                                                                                                                   | Two digit minute                                                                                                                                                                 | <p>Input: 20:10:15<br>Output: 10</p>                                                                                                                                                                                                         |
| `SS`                                                                                                                   | Two digit second                                                                                                                                                                 | <p>Input: 20:10:15<br>Output: 15</p>                                                                                                                                                                                                         |
| `SSSSS`                                                                                                                | Five digit second                                                                                                                                                                | <p>Input: 20:10:15<br>Output: 15234</p>                                                                                                                                                                                                      |
| <p><code>FFn</code></p><p></p><p>(Replace <code>n</code> with a value from 1 to 9. For example, <code>FF5</code>.)</p> | Fractional part of the second, `n` digits long. The fractional part of the second is rounded to fit the size of the output.                                                      | <p><code>FF1</code></p><p>Input: 20:10:15<br>Output: 1<br></p><p><code>FF2</code><br>Input: 20:10:15<br>Output: 15<br></p><p><code>FF3</code><br>Input: 20:10:15<br>Output: 015</p>                                                          |
| <p><code>A.M.</code> or <code>AM</code><br><code>P.M.</code> or <code>PM</code></p>                                    | A.M. (or AM) if the time is less than 12, otherwise P.M. (or PM). The letter case of the output is determined by the first letter case of the format element.                    | <p><code>AM</code></p><p>Input: 09:10:15<br>Output: AM<br></p><p><code>A.M.</code><br>Input: 20:10:15<br>Output: P.M.<br></p><p><code>PM</code><br>Input: 09:10:15<br>Output: AM<br><br><code>PM</code><br>Input: 20:10:15<br>Output: PM</p> |
| `TZH`                                                                                                                  | Hour offset for a time zone. This includes the `+/-` sign and 2-digit hour.                                                                                                      | <p>Input: 2008-12-25 05:30:00+00 <br>Output: −08</p>                                                                                                                                                                                         |
| `TZM`                                                                                                                  | Minute offset for a time zone. This includes only the 2-digit minute.                                                                                                            | <p>Input: 2008-12-25 05:30:00+00 <br>Output: 00</p>                                                                                                                                                                                          |
|                                                                                                                        | A space                                                                                                                                                                          | <p>Input:  <br>Output:  </p>                                                                                                                                                                                                                 |
| `-./,'l;:`                                                                                                             | Same character in the output                                                                                                                                                     | <p>Input: -./,'l;:<br>Output: -./,'l;:</p>                                                                                                                                                                                                   |
| "Text"                                                                                                                 | Output is the value within the double quotes. To preserve a double quote or backslash character, use the `\"` or `\\` escape sequence. Other escape sequences are not supported. | <p>Input: "abc"<br>Output: abc</p><p></p><p>Input: "a"b\c"<br>Output: a"b\c</p>                                                                                                                                                              |

{% hint style="info" %}
These format elements will only work in the Retype step (or `CAST` method in SQL). Format elements for using other date parsing or formatting methods are [detailed elsewhere](https://docs.redivis.com/reference/workflows/variable-creation-methods/common-elements#format-elements-for-dates-and-time-methods) and might be useful if your data is not coercible using the format elements described here.
{% endhint %}
