Rules to detect constructs that are either broken, extremely confusing or prone to runtime errors.
Table of Contents

TO_DATE_TO_CHAR

Since: PMD 5.1

Priority: Medium (3)

TO_DATE(TO_CHAR(date-variable)) used to remove time component - use TRUNC(date-variable)

This rule is defined by the following XPath expression:

//FunctionCall[@Image='TO_DATE']
  [count(Arguments/ArgumentList/Argument) = 1]
  [Arguments/ArgumentList/Argument//FunctionCall[@Image='TO_CHAR']]

Example(s):

CREATE OR REPLACE PACKAGE BODY date_utilities
IS

-- Take single parameter, relying on current default NLS date format
FUNCTION strip_time (p_date IN DATE) RETURN DATE
IS
BEGIN
   RETURN TO_DATE(TO_CHAR(p_date));
END strip_time;


END date_utilities;
/

Use this rule by referencing it:

<rule ref="category/plsql/errorprone.xml/TO_DATE_TO_CHAR" />

TO_DATEWithoutDateFormat

Since: PMD 5.1

Priority: Medium (3)

TO_DATE without date format- use TO_DATE(expression, date-format)

This rule is defined by the following XPath expression:

//FunctionCall[@Image='TO_DATE']
  [count(Arguments/ArgumentList/Argument) = 1]

Example(s):

CREATE OR REPLACE PACKAGE BODY date_utilities
IS

-- Take single parameter, relying on current default NLS date format
FUNCTION to_date_single_parameter (p_date_string IN VARCHAR2) RETURN DATE
IS
BEGIN
   RETURN TO_DATE(p_date_string);
END to_date_single_parameter ;

-- Take 2 parameters, using an explicit date format string
FUNCTION to_date_two_parameters (p_date_string IN VARCHAR2, p_format_mask IN VARCHAR2) RETURN DATE
IS
BEGIN
   TO_DATE(p_date_string, p_date_format);
END to_date_two_parameters;

-- Take 3 parameters, using an explicit date format string and an explicit language
FUNCTION to_date_three_parameters (p_date_string IN VARCHAR2, p_format_mask IN VARCHAR2, p_nls_language VARCHAR2 ) RETURN DATE
IS
BEGIN
   TO_DATE(p_date_string, p_format_mask, p_nls_language);
END to_date_three_parameters;

END date_utilities;
/

Use this rule by referencing it:

<rule ref="category/plsql/errorprone.xml/TO_DATEWithoutDateFormat" />

TO_TIMESTAMPWithoutDateFormat

Since: PMD 5.1

Priority: Medium (3)

TO_TIMESTAMP without date format- use TO_TIMESTAMP(expression, date-format)

This rule is defined by the following XPath expression:

//FunctionCall[@Image='TO_TIMESTAMP']
  [count(Arguments/ArgumentList/Argument) = 1]

Example(s):

CREATE OR REPLACE PACKAGE BODY date_utilities
IS

-- Take single parameter, relying on current default NLS date format
FUNCTION to_timestamp_single_parameter (p_date_string IN VARCHAR2) RETURN DATE
IS
BEGIN
   RETURN TO_TIMESTAMP(p_date_string);
END to_timestamp_single_parameter;

-- Take 2 parameters, using an explicit date format string
FUNCTION to_timestamp_two_parameters (p_date_string IN VARCHAR2, p_format_mask IN VARCHAR2) RETURN DATE
IS
BEGIN
   TO_TIMESTAMP(p_date_string, p_date_format);
END to_timestamp_two_parameters;

-- Take 3 parameters, using an explicit date format string and an explicit language
FUNCTION to_timestamp_three_parameters (p_date_string IN VARCHAR2, p_format_mask IN VARCHAR2, p_nls_language VARCHAR2 ) RETURN DATE
IS
BEGIN
   TO_TIMESTAMP(p_date_string, p_format_mask, p_nls_language);
END to_timestamp_three_parameters;

END date_utilities;
/

Use this rule by referencing it:

<rule ref="category/plsql/errorprone.xml/TO_TIMESTAMPWithoutDateFormat" />