Oracle VALIDATE_CONVERSION() Function
Oracle VALIDATE_CONVERSION() is a built-in function that is used to determine whether a given expression can be converted to a given data type.
Oracle VALIDATE_CONVERSION() Syntax
Here is the syntax for the Oracle VALIDATE_CONVERSION() function:
VALIDATE_CONVERSION(expr AS type_name
[, fmt [, 'nlsparam' ] ])
Parameters
expr-
Required. It specifies an SQL expression.
type_name-
Required. It specifies the data type to which
exprshould be converted. fmt-
Optional. A format string.
'nlsparam'-
Optional. You can use this parameter to set the parameter in the form
'NLS_DATE_LANGUAGE = language', wherelanguageis the language name.
For type_name, you can specify the following data types:
-
BINARY_DOUBLEIf
BINARY_DOUBLEis specified,exprcan be any character string that evaluates to aCHAR,VARCHAR2,NCHAR, orNVARCHAR2data type, or a numeric value that is of typeNUMBER,BINARY_FLOAT, orBINARY_DOUBLE. The optionalfmtandnlsparamparameters have the same effect as theTO_BINARY_DOUBLEfunction. For more information, see TO_BINARY_DOUBLE. -
BINARY_FLOATIf
BINARY_FLOATis specified,exprcan be any character string that evaluates to aCHAR,VARCHAR2,NCHAR, orNVARCHAR2data type, or a numeric value that is of typeNUMBER,BINARY_FLOAT, orBINARY_DOUBLE. The optionalfmtandnlsparamparameters have the same effect as theTO_BINARY_FLOATfunction. For more information, see TO_BINARY_FLOAT. -
DATEIf
DATEis specified,exprcan be any character string that evaluates to aCHAR,VARCHAR2,NCHAR, orNVARCHAR2data type. The optionalfmtandnlsparamparameters have the same effect as theTO_DATEfunction. For more information, see TO_DATE. -
INTERVAL DAY TO SECONDIf
INTERVAL DAY TO SECONDis specified,exprcan be any character string expression that evaluates to aCHAR,VARCHAR2,NCHAR, orNVARCHAR2data type, and must contain a value in SQL interval format or ISO duration format. For this data type, the optionalfmtandnlsparamparameters do not apply. For more information about SQL interval formats and ISO duration formats, see TO_DSINTERVAL. -
INTERVAL YEAR TO MONTHIf
INTERVAL YEAR TO MONTHis specified,exprcan be any character string expression that evaluates to aCHAR,VARCHAR2,NCHAR, orNVARCHAR2data type, and must contain a value in SQL interval format or ISO duration format. For this data type, the optionalfmtandnlsparamparameters do not apply. For more information about SQL interval formats and ISO duration formats, see TO_YMINTERVAL. -
NUMBERIf
NUMBERis specified,exprcan be any character string expression that evaluates to theCHAR,VARCHAR2,NCHAR, orNVARCHAR2data type, or a value of theNUMBER,BINARY_FLOAT, orBINARY_DOUBLEtype. The optionalfmtandnlsparamparameters have the same effect as theTO_NUMBERfunction. For more information, see TO_NUMBER.If
expris a value of theNUMBERtype, theVALIDATE_CONVERSIONfunction validates whetherexpris a valid number. Ifexpris not a valid number, the function returns 0. This enables you to identify corrupt numeric values in the database. -
TIMESTAMPIf
TIMESTAMPis specified,exprcan be any character expression that evaluates to theCHAR,VARCHAR2,NCHAR, orNVARCHAR2data type. The optionalfmtandnlsparamparameters have the same effect as theTO_TIMESTAMPfunction. Iffmtis omitted,exprmust use the default format of theTIMESTAMPdata type, which is determined by theNLS_TIMESTAMP_FORMATinitialization parameter. For more information, see TO_TIMESTAMP. -
TIMESTAMP WITH TIME ZONEIf
TIMESTAMP WITH TIME ZONEis specified,exprcan be any character expression that evaluates to theCHAR,VARCHAR2,NCHAR, orNVARCHAR2data type. The optionalfmtandnlsparamparameters have the same effect as theTO_TIMESTAMP_TZfunction. Iffmtis omitted,exprmust use the default format of theTIMESTAMP WITH TIME ZONEdata type, which is determined by theNLS_TIMESTAMP_TZ_FORMATinitialization parameter. For more information, see TO_TIMESTAMP_TZ. -
TIMESTAMP WITH LOCAL TIME ZONEIf
TIMESTAMP WITH LOCAL TIME ZONEis specified,exprcan be any character expression that evaluates to theCHAR,VARCHAR2,NCHAR, orNVARCHAR2data type. The optionalfmtandnlsparamparameters have the same effect as theTO_TIMESTAMPfunction. Iffmtis omitted,exprmust use the default format of theTIMESTAMPdata type, which is determined by theNLS_TIMESTAMP_FORMATinitialization parameter. For more information, see TO_TIMESTAMP.
Return Value
If expr can be successfully converted, the Oracle VALIDATE_CONVERSION() function returns 1; otherwise, it returns 0.
If expr evaluates to null, this function returns 1.
If an error occurs while evaluating expr, this function returns an error.
Oracle VALIDATE_CONVERSION() Examples
Here are some examples that demonstrate the usage of the Oracle VALIDATE_CONVERSION() function.
BINARY_DOUBLE
The following statement validates whether the given value can be converted to the BINARY_DOUBLE data type:
SELECT VALIDATE_CONVERSION(1000 AS BINARY_DOUBLE) Result
FROM DUAL;
输出:
RESULT
_________
1BINARY_FLOAT
The following statement validates whether the given value can be converted to the BINARY_FLOAT data type:
SELECT VALIDATE_CONVERSION('1234.56' AS BINARY_FLOAT) Result
FROM DUAL;
输出:
RESULT
_________
1DATE
The following statement validates whether the given value can be converted to the DATE data type:
SELECT VALIDATE_CONVERSION(
'July 20, 1969, 20:18' AS DATE,
'Month dd, YYYY, HH24:MI',
'NLS_DATE_LANGUAGE = American'
) Result
FROM DUAL;
输出:
RESULT
_________
1INTERVAL DAY TO SECOND
The following statement validates whether the given value can be converted to the INTERVAL DAY TO SECOND data type:
SELECT VALIDATE_CONVERSION('200 00:00:00' AS INTERVAL DAY TO SECOND) Result
FROM DUAL;
输出:
RESULT
_________
1INTERVAL YEAR TO MONTH
The following statement validates whether the given value can be converted to the INTERVAL YEAR TO MONTH data type:
SELECT VALIDATE_CONVERSION('P1Y2M' AS INTERVAL YEAR TO MONTH) Result
FROM DUAL;
输出:
RESULT
_________
1NUMBER
The following statement validates whether the given value can be converted to the NUMBER data type:
SELECT VALIDATE_CONVERSION(
'$100,00' AS NUMBER,
'$999D99',
'NLS_NUMERIC_CHARACTERS = '',.'''
) Result
FROM DUAL;
输出:
RESULT
_________
1TIMESTAMP
The following statement validates whether the given value can be converted to the TIMESTAMP data type:
SELECT VALIDATE_CONVERSION(
'29-Jan-02 17:24:00' AS TIMESTAMP,
'DD-MON-YY HH24:MI:SS',
'NLS_DATE_LANGUAGE = American'
) Result
FROM DUAL;
输出:
RESULT
_________
1TIMESTAMP WITH TIME ZONE
The following statement validates whether the given value can be converted to the TIMESTAMP WITH TIME ZONE data type:
SELECT VALIDATE_CONVERSION(
'1999-12-01 11:00:00 -8:00' AS TIMESTAMP WITH TIME ZONE,
'YYYY-MM-DD HH:MI:SS TZH:TZM'
) Result
FROM DUAL;
输出:
RESULT
_________
1TIMESTAMP WITH LOCAL TIME ZONE
The following statement validates whether the given value can be converted to the TIMESTAMP WITH LOCAL TIME ZONE data type:
SELECT VALIDATE_CONVERSION(
'11-May-16 17:30:00' AS TIMESTAMP WITH LOCAL TIME ZONE,
'DD-MON-YY HH24:MI:SS'
) Result
FROM DUAL;
输出:
RESULT
_________
0NULL Parameters
If the given expression is NULL, VALIDATE_CONVERSION() always returns 1, regardless of the given data type.
SELECT
VALIDATE_CONVERSION(NULL AS NUMBER)
FROM dual;
输出:
VALIDATE_CONVERSION(NULLASNUMBER)
____________________________________
1Here’s another example using a different data type:
SELECT
VALIDATE_CONVERSION(NULL AS DATE)
FROM dual;
输出:
VALIDATE_CONVERSION(NULLASDATE)
__________________________________
1In this example, we used the SET NULL 'NULL'; statement to display NULL values as the string 'NULL'.
Conclusion
Oracle VALIDATE_CONVERSION() is a built-in function that is used to determine whether a given expression can be converted to a given data type.