Skip to content

Microsoft ODBC Driver 13: "Invalid character value for cast specification" inserting SqlBool #35

@mclark1129

Description

@mclark1129

I seem to be having issues trying to insert a Bool value into a bit field in MSSQL. I'm receiv'ing the following error when I execute the INSERT:

SqlError {seState = "["22018"]", seNativeError = -1, seErrorMsg = "execute execute: ["0: [Microsoft][ODBC Driver 13 for SQL Server]Invalid character value for cast specification"]"}

I have no issues reading out bit values using SELECT, although it looks like these values are being pulled into a SqlChar instead of a SqlBool so not sure if that's a clue. The SqlChar is converted to Bool on my record type, so at least it's compatible.

When I convert my record type into [SqlValue], the value for the Bool is represented as a SqlBool False instead of SqlChar '\NUL' and this fails on INSERT.

Below is some sample code that illustrates the issue:

type ItemId = Integer
data TodoItem = TodoItem { itemId :: ItemId, description :: String, dueDate :: LocalTime, completed :: Bool } deriving (Show, Generic)
instance FromJSON TodoItem
instance ToJSON TodoItem

-- Maps the record type to SqlValues
fromItem :: TodoItem -> [SqlValue]
fromItem = flip (\x -> map (\f -> f x)) $ [toSql . description, toSql . dueDate, toSql . completed]

insertItem :: IConnection conn => IO conn -> TodoItem -> IO ()
insertItem ioconn item = do
    conn <- ioconn
    stmt <- prepare conn "INSERT INTO Items (Description, DueDate, Completed) VALUES (?, ?, ?)"
    execute stmt $ fromItem item
    commit conn

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions