You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/UserGuide/Master/Table/SQL-Manual/Basis-Function.md
+186Lines changed: 186 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -758,6 +758,192 @@ SELECT *
758
758
IN (try_cast('2024-11-27'ASDATE), try_cast('2024-11-28'ASDATE));
759
759
```
760
760
761
+
### 7.2 Format Function
762
+
763
+
This function generates and returns a formatted string based on a specified format string and input arguments. Similar to Java’s `String.format` or C’s `printf`, it allows developers to construct dynamic string templates using placeholder syntax. Predefined format specifiers in the template are replaced precisely with corresponding argument values, producing a complete string that adheres to specific formatting requirements.
764
+
765
+
#### 7.2.1 Syntax
766
+
767
+
```SQL
768
+
format(pattern, ...args) -> STRING
769
+
```
770
+
771
+
**Parameters**
772
+
773
+
*`pattern`: A format string containing static text and one or more format specifiers (e.g., `%s`, `%d`), or any expression returning a `STRING`/`TEXT` type.
774
+
*`args`: Input arguments to replace format specifiers. Constraints:
775
+
* Number of arguments ≥ 1.
776
+
* Multiple arguments must be comma-separated (e.g., `arg1, arg2`).
777
+
* Total arguments can exceed the number of specifiers in `pattern` but cannot be fewer, otherwise an exception is triggered.
778
+
779
+
**Return Value**
780
+
781
+
* Formatted result string of type `STRING`.
782
+
783
+
#### 7.2.2 Usage Examples
784
+
785
+
1. Format Floating-Point Numbers
786
+
```SQL
787
+
IoTDB:database1>SELECT format('%.5f', humidity) FROM table1 WHERE humidity =35.4;
788
+
+--------+
789
+
| _col0|
790
+
+--------+
791
+
|35.40000|
792
+
+--------+
793
+
```
794
+
2. Format Integers
795
+
```SQL
796
+
IoTDB:database1>SELECT format('%03d', 8) FROM table1 LIMIT1;
797
+
+-----+
798
+
|_col0|
799
+
+-----+
800
+
| 008|
801
+
+-----+
802
+
```
803
+
3. Format Dates and Timestamps
804
+
805
+
* Locale-Specific Date
806
+
807
+
```SQL
808
+
IoTDB:database1>SELECT format('%1$tA, %1$tB %1$te, %1$tY', 2024-01-01) FROM table1 LIMIT1;
809
+
+--------------------+
810
+
| _col0|
811
+
+--------------------+
812
+
|Monday, January 1, 2024|
813
+
+--------------------+
814
+
```
815
+
816
+
* Remove Timezone Information
817
+
818
+
```SQL
819
+
IoTDB:database1>SELECT format('%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL', 2024-01-01T00:00:00.000+08:00) FROM table1 LIMIT1;
820
+
+-----------------------+
821
+
| _col0|
822
+
+-----------------------+
823
+
|2024-01-0100:00:00.000|
824
+
+-----------------------+
825
+
```
826
+
827
+
* Second-Level Timestamp Precision
828
+
829
+
```SQL
830
+
IoTDB:database1>SELECT format('%1$tF %1$tT', 2024-01-01T00:00:00.000+08:00) FROM table1 LIMIT1;
| 'B' | Locale-specific full month name, for example "January", "February" |
861
+
| 'b' | Locale-specific abbreviated month name, for example "Jan", "Feb" |
862
+
| 'h' | Same as`b`|
863
+
| 'A' | Locale-specific full weekday name, for example "Sunday", "Monday" |
864
+
| 'a' | Locale-specific short weekday name, for example "Sun", "Mon" |
865
+
| 'C' | Year divided by 100 (two digits, zero-padded) |
866
+
| 'Y' | Year (minimum 4 digits, zero-padded) |
867
+
| 'y' | Last two digits of year (zero-padded) |
868
+
| 'j' | Day of year (three digits, zero-padded) |
869
+
| 'm' | Month (two digits, zero-padded) |
870
+
| 'd' | Day of month (two digits, zero-padded) |
871
+
| 'e' | Day of month (no padding) |
872
+
873
+
4. Format Strings
874
+
```SQL
875
+
IoTDB:database1>SELECT format('The measurement status is: %s', status) FROM table2 LIMIT1;
876
+
+-------------------------------+
877
+
| _col0|
878
+
+-------------------------------+
879
+
|The measurement status is: true|
880
+
+-------------------------------+
881
+
```
882
+
5. Format Percentage Sign
883
+
```SQL
884
+
IoTDB:database1>SELECT format('%s%%', 99.9) FROM table1 LIMIT1;
885
+
+-----+
886
+
|_col0|
887
+
+-----+
888
+
|99.9%|
889
+
+-----+
890
+
```
891
+
892
+
#### 7.2.3 Format Conversion Failure Scenarios
893
+
894
+
1. Type Mismatch Errors
895
+
896
+
* Timestamp Type Conflict
897
+
898
+
If the format specifier includes time-related tokens (e.g., `%Y-%m-%d`) but the argument:
899
+
900
+
* Is a non-`DATE`/`TIMESTAMP` type value. ◦
901
+
* Requires sub-day precision (e.g., `%H`, `%M`) but the argument is not `TIMESTAMP`.
902
+
903
+
```SQL
904
+
-- Example 1
905
+
IoTDB:database1>SELECT format('%1$tA, %1$tB %1$te, %1$tY', humidity) from table2 limit1
906
+
Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: Invalid format string: %1$tA, %1$tB %1$te, %1$tY (IllegalFormatConversion: A !=java.lang.Float)
907
+
908
+
-- Example 2
909
+
IoTDB:database1>SELECT format('%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL', humidity) from table1 limit1
910
+
Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: Invalid format string: %1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL (IllegalFormatConversion: Y !=java.lang.Float)
911
+
```
912
+
913
+
* Floating-Point Type Conflict
914
+
915
+
Using `%f` with non-numeric arguments (e.g., strings or booleans):
916
+
917
+
```SQL
918
+
IoTDB:database1>select format('%.5f',status) from table1 where humidity =35.4
919
+
Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: Invalid format string: %.5f (IllegalFormatConversion: f !=java.lang.Boolean)
920
+
```
921
+
922
+
2. Argument Count Mismatch
923
+
The number of arguments must equal or exceed the number of format specifiers.
924
+
925
+
```SQL
926
+
IoTDB:database1>SELECT format('%.5f %03d', humidity) FROM table1 WHERE humidity =35.4;
927
+
Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: Invalid format string: %.5f %03d (MissingFormatArgument: Format specifier '%03d')
928
+
```
929
+
3. Invalid Invocation Errors
930
+
931
+
Triggered if:
932
+
933
+
* Total arguments < 2 (must include `pattern` and at least one argument).•
934
+
*`pattern` is not of type `STRING`/`TEXT`.
935
+
936
+
```SQL
937
+
-- Example 1
938
+
IoTDB:database1>select format('%s') from table1 limit1
939
+
Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: Scalar function format must have at least two arguments, and first argument pattern must be TEXTor STRING type.
940
+
941
+
--Example 2
942
+
IoTDB:database1>select format(123, humidity) from table1 limit1
943
+
Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: Scalar function format must have at least two arguments, and first argument pattern must be TEXTor STRING type.
Copy file name to clipboardExpand all lines: src/UserGuide/latest-Table/SQL-Manual/Basis-Function.md
+186Lines changed: 186 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -758,6 +758,192 @@ SELECT *
758
758
IN (try_cast('2024-11-27'ASDATE), try_cast('2024-11-28'ASDATE));
759
759
```
760
760
761
+
### 7.2 Format Function
762
+
763
+
This function generates and returns a formatted string based on a specified format string and input arguments. Similar to Java’s `String.format` or C’s `printf`, it allows developers to construct dynamic string templates using placeholder syntax. Predefined format specifiers in the template are replaced precisely with corresponding argument values, producing a complete string that adheres to specific formatting requirements.
764
+
765
+
#### 7.2.1 Syntax
766
+
767
+
```SQL
768
+
format(pattern, ...args) -> STRING
769
+
```
770
+
771
+
**Parameters**
772
+
773
+
*`pattern`: A format string containing static text and one or more format specifiers (e.g., `%s`, `%d`), or any expression returning a `STRING`/`TEXT` type.
774
+
*`args`: Input arguments to replace format specifiers. Constraints:
775
+
* Number of arguments ≥ 1.
776
+
* Multiple arguments must be comma-separated (e.g., `arg1, arg2`).
777
+
* Total arguments can exceed the number of specifiers in `pattern` but cannot be fewer, otherwise an exception is triggered.
778
+
779
+
**Return Value**
780
+
781
+
* Formatted result string of type `STRING`.
782
+
783
+
#### 7.2.2 Usage Examples
784
+
785
+
1. Format Floating-Point Numbers
786
+
```SQL
787
+
IoTDB:database1>SELECT format('%.5f', humidity) FROM table1 WHERE humidity =35.4;
788
+
+--------+
789
+
| _col0|
790
+
+--------+
791
+
|35.40000|
792
+
+--------+
793
+
```
794
+
2. Format Integers
795
+
```SQL
796
+
IoTDB:database1>SELECT format('%03d', 8) FROM table1 LIMIT1;
797
+
+-----+
798
+
|_col0|
799
+
+-----+
800
+
| 008|
801
+
+-----+
802
+
```
803
+
3. Format Dates and Timestamps
804
+
805
+
* Locale-Specific Date
806
+
807
+
```SQL
808
+
IoTDB:database1>SELECT format('%1$tA, %1$tB %1$te, %1$tY', 2024-01-01) FROM table1 LIMIT1;
809
+
+--------------------+
810
+
| _col0|
811
+
+--------------------+
812
+
|Monday, January 1, 2024|
813
+
+--------------------+
814
+
```
815
+
816
+
* Remove Timezone Information
817
+
818
+
```SQL
819
+
IoTDB:database1>SELECT format('%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL', 2024-01-01T00:00:00.000+08:00) FROM table1 LIMIT1;
820
+
+-----------------------+
821
+
| _col0|
822
+
+-----------------------+
823
+
|2024-01-0100:00:00.000|
824
+
+-----------------------+
825
+
```
826
+
827
+
* Second-Level Timestamp Precision
828
+
829
+
```SQL
830
+
IoTDB:database1>SELECT format('%1$tF %1$tT', 2024-01-01T00:00:00.000+08:00) FROM table1 LIMIT1;
| 'B' | Locale-specific full month name, for example "January", "February" |
861
+
| 'b' | Locale-specific abbreviated month name, for example "Jan", "Feb" |
862
+
| 'h' | Same as`b`|
863
+
| 'A' | Locale-specific full weekday name, for example "Sunday", "Monday" |
864
+
| 'a' | Locale-specific short weekday name, for example "Sun", "Mon" |
865
+
| 'C' | Year divided by 100 (two digits, zero-padded) |
866
+
| 'Y' | Year (minimum 4 digits, zero-padded) |
867
+
| 'y' | Last two digits of year (zero-padded) |
868
+
| 'j' | Day of year (three digits, zero-padded) |
869
+
| 'm' | Month (two digits, zero-padded) |
870
+
| 'd' | Day of month (two digits, zero-padded) |
871
+
| 'e' | Day of month (no padding) |
872
+
873
+
4. Format Strings
874
+
```SQL
875
+
IoTDB:database1>SELECT format('The measurement status is: %s', status) FROM table2 LIMIT1;
876
+
+-------------------------------+
877
+
| _col0|
878
+
+-------------------------------+
879
+
|The measurement status is: true|
880
+
+-------------------------------+
881
+
```
882
+
5. Format Percentage Sign
883
+
```SQL
884
+
IoTDB:database1>SELECT format('%s%%', 99.9) FROM table1 LIMIT1;
885
+
+-----+
886
+
|_col0|
887
+
+-----+
888
+
|99.9%|
889
+
+-----+
890
+
```
891
+
892
+
#### 7.2.3 Format Conversion Failure Scenarios
893
+
894
+
1. Type Mismatch Errors
895
+
896
+
* Timestamp Type Conflict
897
+
898
+
If the format specifier includes time-related tokens (e.g., `%Y-%m-%d`) but the argument:
899
+
900
+
* Is a non-`DATE`/`TIMESTAMP` type value. ◦
901
+
* Requires sub-day precision (e.g., `%H`, `%M`) but the argument is not `TIMESTAMP`.
902
+
903
+
```SQL
904
+
-- Example 1
905
+
IoTDB:database1>SELECT format('%1$tA, %1$tB %1$te, %1$tY', humidity) from table2 limit1
906
+
Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: Invalid format string: %1$tA, %1$tB %1$te, %1$tY (IllegalFormatConversion: A !=java.lang.Float)
907
+
908
+
-- Example 2
909
+
IoTDB:database1>SELECT format('%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL', humidity) from table1 limit1
910
+
Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: Invalid format string: %1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL (IllegalFormatConversion: Y !=java.lang.Float)
911
+
```
912
+
913
+
* Floating-Point Type Conflict
914
+
915
+
Using `%f` with non-numeric arguments (e.g., strings or booleans):
916
+
917
+
```SQL
918
+
IoTDB:database1>select format('%.5f',status) from table1 where humidity =35.4
919
+
Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: Invalid format string: %.5f (IllegalFormatConversion: f !=java.lang.Boolean)
920
+
```
921
+
922
+
2. Argument Count Mismatch
923
+
The number of arguments must equal or exceed the number of format specifiers.
924
+
925
+
```SQL
926
+
IoTDB:database1>SELECT format('%.5f %03d', humidity) FROM table1 WHERE humidity =35.4;
927
+
Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: Invalid format string: %.5f %03d (MissingFormatArgument: Format specifier '%03d')
928
+
```
929
+
3. Invalid Invocation Errors
930
+
931
+
Triggered if:
932
+
933
+
* Total arguments < 2 (must include `pattern` and at least one argument).•
934
+
*`pattern` is not of type `STRING`/`TEXT`.
935
+
936
+
```SQL
937
+
-- Example 1
938
+
IoTDB:database1>select format('%s') from table1 limit1
939
+
Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: Scalar function format must have at least two arguments, and first argument pattern must be TEXTor STRING type.
940
+
941
+
--Example 2
942
+
IoTDB:database1>select format(123, humidity) from table1 limit1
943
+
Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: Scalar function format must have at least two arguments, and first argument pattern must be TEXTor STRING type.
0 commit comments