Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Error converting data type varchar to datetime"

Tags:

sql-server

How can I solve the error in the following procedure?

CREATE PROCEDURE cards
    @salesman VARCHAR(10),
    @RCV10 INT,
    @RCV09 INT,
    @RCV15 INT,
    @GPRS15 INT,
    @RCV20 INT,
    @RCV25FTT INT,
    @RCV25 INT,
    @RCV31 INT,
    @RCV30 INT,
    @RCV35 INT,
    @RCV50 INT,
    @RCV55 INT,
    @SIM INT,
    @VTOPSIM INT,
    @VTOPBAL INT,
    @THREEGSIM INT,
    @entrydate DATETIME
AS
BEGIN
    IF EXISTS(
           SELECT *
           FROM   CardsIssued
           WHERE  salesman = @salesman
                  AND RCV10 > @RCV10
                  AND RCV09 > @RCV09
                  AND RCV15 > @RCV15
                  AND GPRS15 > @GPRS15
                  AND RCV20 > @RCV20
                  AND RCV25FTT > @RCV25FTT
                  AND RCV25 > @RCV25
                  AND RCV31 > @RCV31
                  AND RCV30 > @RCV30
                  AND RCV35 > @RCV35
                  AND RCV50 > @RCV50
                  AND RCV55 > @RCV55
                  AND SIM > @SIM
                  AND VtopSim > @VTOPSIM
                  AND VtopBal > @VTOPBAL
                  AND ThreeGSim > @THREEGSIM
                  AND EntryDate = @entrydate
       )
    BEGIN
        INSERT Cards_Returned
        VALUES
          (
            @salesman,
            @RCV10,
            @RCV09,
            @RCV15,
            @GPRS15,
            @RCV20,
            @RCV25FTT,
            @RCV25,
            @RCV31,
            @RCV30,
            @RCV35,
            @RCV50,
            @RCV55,
            @SIM,
            @VTOPSIM,
            @VTOPBAL,
            @THREEGSIM,
            @EntryDate
          )
    END
    ELSE
        PRINT'CARDS RETURNED CANNOT BE GREATER THAN CARDS ISSUED'
END

Run as:

execute cards 'S001',50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,'28/1/2013'

gives the following error:

Msg 8114, Level 16, State 5, Procedure cards, Line 0
Error converting data type varchar to datetime
like image 370
user1994734 Avatar asked Jan 31 '13 11:01

user1994734


1 Answers

'28/1/2013' is an ambiguous format - SQL Server might interpret it as dd/mm/yyyy or as mm/dd/yyyy. In this case, it's done the latter, and doesn't know what the 28th month of the year is.

Use '20130128' instead. This is always interpreted as yyyymmdd.

like image 61
Damien_The_Unbeliever Avatar answered Oct 26 '22 05:10

Damien_The_Unbeliever