Search code examples
sqlsql-server-ce

Error converting data type SQL Server CE


I have an object with the following data:

{
    "ItemID": "0000000",
    "ConsignmentID": "0000000",
    "CountryCreateDate": "24/05/2013 3:20:02 a.m.",
    "Reference": "00000000",
    "Packaging": "0",
    "Weight": "0",
    "WeightCubic": "0",
    "Length": "0",
    "Width": "0",
    "Height": "0",
    "seqNumber": "0",
    "DatePrinted": "",
    "ad_Excess": "0",
    "Price_Cost": "",
    "Price_Other": "",
    "Price_OtherTypeID": "",
    "FailedReason": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "packedInCustomID": "",
    "UpdateDateUTC": "6/06/2013 9:55:05 p.m.",
    "PrintedByCustomerUserID": ""
}

This is inserted directly into a SQL Server CE database using ExecuteNonQuery, nothing is wrong with that or the way its being inserted and has been tested and used a lot.

When inserting this data into a table with the following schema I get the error:

There was a syntax error while converting from one data type to another. [ Expression = ]

enter image description here

I've looked over and over the data and there seems to be nothing out of place. Should I be converting the dates or something? Everything is being inserted as type NVarChar as they are all strings.

The date format is also in the format d/MM/yyyy h:mm:ss tt

The insert query I use is:

INSERT INTO example
(
    ItemID, 
    ConsignmentID, 
    CountryCreateDate, 
    Reference, 
    Packaging, 
    Weight, 
    WeightCubic, 
    Length, 
    Width, 
    Height, 
    seqNumber, 
    DatePrinted, 
    ad_Excess, 
    Price_Cost, 
    Price_Other, 
    Price_OtherTypeID, 
    FailedReason, 
    PackedInCustomID, 
    UpdateDateUTC,
    PrintedByCustomerUserID
) VALUES (
    @ItemID, 
    @ConsignmentID,
    @CountryCreateDate, 
    @Reference, 
    @Packaging, 
    @Weight, 
    @WeightCubic, 
    @Length, 
    @Width, 
    @Height, 
    @seqNumber, 
    @DatePrinted, 
    @ad_Excess, 
    @Price_Cost, 
    @Price_Other, 
    @Price_OtherTypeID, 
    @FailedReason,
    @PackedInCustomID, 
    @UpdateDateUTC,
    @PrintedByCustomerUserID
)

Solution

  • You need to change date format: try 20130524 instead of 24/05/2013

    SQL server considers the format your date is in to be ambiguous, because some countries do DD/MM and others do MM/DD, accepted formats are listed here: Date and Time Formats. I typically go with 'YYYYMMDD', but dashes are fine in there.

    Edit: Additionally, the NUMERIC data type cannot accept a blank string. You'll have to choose a different data type for Price_Cost and Price_Other fields or feed them a '0' instead of empty string ''.