Skip to content

Commit fa51433

Browse files
committed
Fix #100 - Added new rule G-9040: Try using FX in string to date/time conversion format model to avoid fuzzy conversion.
1 parent ce7d621 commit fa51433

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

docs/4-language-usage/9-function-usage/g-9010.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ create package body employee_api is
3232
,in_dob_str in varchar2) is
3333
begin
3434
update employees
35-
set date_of_birth = to_date(in_dob_str,'YYYY-MM-DD')
35+
set date_of_birth = to_date(in_dob_str,'FXYYYY-MM-DD')
3636
where employee_id = in_employee_id;
3737
end set_dob;
3838
end employee_api;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# G-9040: Try using FX in string to date/time conversion format model to avoid fuzzy conversion.
2+
3+
!!! tip "Minor"
4+
Reliability, Testability
5+
6+
## Reason
7+
8+
The default [string-to-date conversion rules](https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/Format-Models.html#GUID-5B755E80-3CB2-4901-BBCF-F0FC764E0BB5) allow fuzzy conversion when converting from strings to `date` or `timestamp` datatypes (using `to_date`, `to_timestamp`, `to_timestamp_tz` or `cast` to any of those datatypes). For example you can omit punctuation characters, use any non-alphanumeric character for punctuation, use month name instead of number, or various other rules.
9+
10+
In practice you almost always expect a truly fixed format and want the database to enforce the format model and raise an error if the data does not match the format model. This you can achieve by adding the format modifier FX (format exact).
11+
12+
The exception to this rule can be if you are converting textual input typed by a user, in which case the fuzzy conversion may be what you want.
13+
14+
## Example (bad)
15+
16+
``` sql
17+
create package body employee_api is
18+
procedure set_dob(in_employee_id in employees.employee_id%type
19+
,in_dob_str in varchar2) is
20+
begin
21+
update employees
22+
set date_of_birth = to_date(in_dob_str,'YYYY-MM-DD')
23+
where employee_id = in_employee_id;
24+
end set_dob;
25+
end employee_api;
26+
/
27+
```
28+
29+
## Example (good)
30+
31+
``` sql
32+
create package body employee_api is
33+
procedure set_dob(in_employee_id in employees.employee_id%type
34+
,in_dob_str in varchar2) is
35+
begin
36+
update employees
37+
set date_of_birth = to_date(in_dob_str,'FXYYYY-MM-DD')
38+
where employee_id = in_employee_id;
39+
end set_dob;
40+
end employee_api;
41+
/
42+
```

docs/9-appendix/appendix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,4 @@ n/a | 8510 | Always use dbms_application_info to track program process transient
130130
n/a | 9010 | Always use a format model in string to date/time conversion functions. | Major | ✘ | | ✘ | | ✘ | | ✘ | ✘
131131
n/a | 9020 | Try to use a format model and NLS_NUMERIC_CHARACTERS in string to number conversion functions. | Major | ✘ | | ✘ | | ✘ | | ✘ | ✘
132132
n/a | 9030 | Try to define a default value on conversion errors. | Minor | | | ✘ | | ✘ | | | ✘
133+
n/a | 9040 | Try using FX in string to date/time conversion format model to avoid fuzzy conversion. | Minor | | | | | ✘ | | | ✘

0 commit comments

Comments
 (0)