Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Conversion failed when converting the varchar value 'SELECT ISDATE(@P_Input)' to data type int

ALTER FUNCTION[dbo].[DateValidation]
(
@P_Input NVARCHAR(55)
)
RETURNS NVARCHAR(55)
AS
BEGIN
    DECLARE @V_Results DATE
    DECLARE @V_Result INT
    DECLARE @V_ERROR NVARCHAR(MAX)
    set @V_Result='SELECT ISDATE(@P_Input)'
    IF @V_Result=1
    BEGIN
     SET @V_Results='SELECT CONVERT(P_Input,getDate(),2)'
      RETURN @V_Results
    END
    SET @V_ERROR ='NOT IN DATE FORMAT'
    RETURN @V_ERROR
END
GO

>Solution :

First of all you are setting variable @V_Result with string (Command). If you need to make query dynamic then you should execute it:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

EXEC ('SELECT ISDATE(@P_Input)')

But in your case, no need to create dynamic command, See the improved format of your function:

CREATE OR ALTER FUNCTION dbo.DateValidation (
    @P_Input NVARCHAR(55)
)
RETURNS NVARCHAR(55)
AS
BEGIN
    DECLARE @V_Result NVARCHAR(55);

    IF (ISDATE(@P_Input) = 1)   
        SET @V_Result = CAST(@P_Input AS DATE)
    ELSE
        SET @V_Result = N'NOT IN DATE FORMAT';

    RETURN @V_Result;
END;
GO
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading