esql-functions.asciidoc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. [[esql-functions]]
  2. == Functions
  3. <<esql-row,`ROW`>>, <<esql-eval,`EVAL`>> and <<esql-where,`WHERE`>> support
  4. these functions:
  5. * <<esql-abs>>
  6. * <<esql-concat>>
  7. * <<esql-date_format>>
  8. * <<esql-date_trunc>>
  9. * <<esql-is_null>>
  10. * <<esql-length>>
  11. * <<esql-round>>
  12. * <<esql-starts_with>>
  13. * <<esql-substring>>
  14. [[esql-abs]]
  15. === `ABS`
  16. Returns the absolute value.
  17. [source,esql]
  18. ----
  19. FROM employees
  20. | PROJECT first_name, last_name, height
  21. | EVAL abs_height = ABS(0.0 - height)
  22. ----
  23. [[esql-concat]]
  24. === `CONCAT`
  25. Concatenates two or more strings.
  26. [source,esql]
  27. ----
  28. FROM employees
  29. | PROJECT first_name, last_name, height
  30. | EVAL fullname = CONCAT(first_name, " ", last_name)
  31. ----
  32. [[esql-date_format]]
  33. === `DATE_FORMAT`
  34. Returns a string representation of a date in the provided format. If no format
  35. is specified, the `yyyy-MM-dd'T'HH:mm:ss.SSSZ` format is used.
  36. [source,esql]
  37. ----
  38. FROM employees
  39. | PROJECT first_name, last_name, hire_date
  40. | EVAL hired = DATE_FORMAT(hire_date, "YYYY-MM-dd")
  41. ----
  42. [[esql-date_trunc]]
  43. === `DATE_TRUNC`
  44. Rounds down a date to the closest interval. Intervals can be expressed using the
  45. <<esql-timespan-literals,timespan literal syntax>>.
  46. [source,esql]
  47. ----
  48. FROM employees
  49. | EVAL year_hired = DATE_TRUNC(hire_date, 1 year)
  50. | STATS count(emp_no) BY year_hired
  51. | SORT year_hired
  52. ----
  53. [[esql-is_null]]
  54. === `IS_NULL`
  55. Returns a boolean than indicates whether its input is `null`.
  56. [source,esql]
  57. ----
  58. FROM employees
  59. | WHERE is_null(first_name)
  60. ----
  61. Combine this function with `NOT` to filter out any `null` data:
  62. [source,esql]
  63. ----
  64. FROM employees
  65. | WHERE NOT is_null(first_name)
  66. ----
  67. [[esql-length]]
  68. === `LENGTH`
  69. Returns the character length of a string.
  70. [source,esql]
  71. ----
  72. FROM employees
  73. | PROJECT first_name, last_name, height
  74. | EVAL fn_length = LENGTH(first_name)
  75. ----
  76. [[esql-round]]
  77. === `ROUND`
  78. Rounds a number to the closest number with the specified number of digits.
  79. Defaults to 0 digits if no number of digits is provided. If the specified number
  80. of digits is negative, rounds to the number of digits left of the decimal point.
  81. [source,esql]
  82. ----
  83. FROM employees
  84. | PROJECT first_name, last_name, height
  85. | EVAL height = ROUND(height * 3.281, 1)
  86. ----
  87. [[esql-starts_with]]
  88. === `STARTS_WITH`
  89. Returns a boolean that indicates whether a keyword string starts with another
  90. string:
  91. [source,esql]
  92. ----
  93. FROM employees
  94. | PROJECT first_name, last_name, height
  95. | EVAL ln_S = STARTS_WITH(last_name, "S")
  96. ----
  97. [[esql-substring]]
  98. === `SUBSTRING`
  99. Returns a substring of a string, specified by a start position and an optional
  100. length. This example returns the first three characters of every last name:
  101. [source,esql]
  102. ----
  103. FROM employees
  104. | PROJECT last_name
  105. | EVAL ln_sub = SUBSTRING(last_name, 1, 3)
  106. ----
  107. A negative start position is interpreted as being relative to the end of the
  108. string. This example returns the last three characters of of every last name:
  109. [source,esql]
  110. ----
  111. FROM employees
  112. | PROJECT last_name
  113. | EVAL ln_sub = SUBSTRING(last_name, -3, 3)
  114. ----
  115. If length is omitted, substring returns the remainder of the string. This
  116. example returns all characters except for the first:
  117. [source,esql]
  118. ----
  119. FROM employees
  120. | PROJECT last_name
  121. | EVAL ln_sub = SUBSTRING(last_name, 2)
  122. ----