You have a few options for converting a number to a string in a Looker expression, depending on your specific needs and the Looker version you're using. Here are the most common and reliable methods:
1. TO_TEXT() (Recommended):
This is the preferred and most consistent method across different Looker versions.
TO_TEXT(${your_number_field})
Replace ${your_number_field} with the actual field containing the number.
2. CAST( ... AS STRING):
This is a standard SQL casting function that also works in Looker expressions.
CAST(${your_number_field} AS STRING)
Replace ${your_number_field} with the actual field containing the number.
3. STRING(${your_number_field}) (Less Common, May Not Be Supported in All Versions):
This is a more concise option, but it might not be supported in older Looker versions.
STRING(${your_number_field})
Replace ${your_number_field} with the actual field containing the number.
Which one should you use?
TO_TEXT() is generally the best choice. It's the most reliable and widely supported method.
CAST(... AS STRING) is a good alternative if TO_TEXT() doesn't work for some reason.
- Avoid
STRING() unless you're sure your Looker version supports it.
Example:
Let's say you have a field called order_quantity that contains the number 10. To convert it to a string, you would use:
TO_TEXT(${order_quantity})
or
CAST(${order_quantity} AS STRING)
Both of these expressions would return the string "10".