Msg 206, Level 16, State 2, Line 9 Operand type clash: int is incompatible with date (I want only year not full date then how I get)

pallavi potnuru 0 Reputation points
2025-05-03T13:48:28.6233333+00:00

CREATE TABLE employe (

ID INT,

YEar DATE,

FirstName VARCHAR(50),

LastName VARCHAR(50),   

);

Insert into employe

values (1,2020, 'John', 'Doe'),

(2, 2021,'Jane', 'Smith'),

(3, 2022,'Alice', 'Johnson')

SQL Server Training
SQL Server Training
SQL Server: A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.Training: Instruction to develop new skills.
15 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 120.3K Reputation points MVP
    2025-05-03T15:33:40.6633333+00:00

    Wagner's answer looks like a Chat GPT session that completely derailed off the tracks and forgets to answer the question.

    Date is date, that is year, month and date. If you only want to store year, use the data type int instead.

    If you want to store only the year in a column of type date, you need to say '2020-01-01' to add a dummy month and day. (For which 1 of Jan seems like the obvious choice.) But that gets messy quite quickly, so I recommend against that.

    As for the error message as such, it is a reminder that you need to be careful with data types. Date literals must always be enclosed in single quotes. You cannot specify integer values for the date data type. (Confusingly, you can for the older datetime data type. However, the result may not be what you expect, so don't do that.)

    1 person found this answer helpful.
    0 comments No comments

  2. Wagner Silva 0 Reputation points Microsoft Employee
    2025-05-03T13:56:59.4166667+00:00

    The error message “Msg 206, Level 16, State 2, Line 9: Operand type clash” in SQL Server indicates that there’s a mismatch between data types in your query. This typically occurs when an operation involves incompatible data types, such as attempting to assign or compare an integer (int) to a date (date) column.

    Common Causes

    1. Implicit Data Type Conversion: SQL Server may attempt to implicitly convert data types, leading to errors when the conversion isn’t straightforward or possible.
    2. Incorrect Default Values in CASE Statements: Using a numeric default like 0 in a CASE expression that returns dates can cause this error.
    3. Mismatched Column and Value Types in INSERT Statements: Inserting values into a table without ensuring that the data types match the column definitions can lead to this error.

    How to Resolve

    1. Ensure Consistent Data Types: Verify that all operands in your expressions are of compatible data types. For instance, if you’re working with date columns, ensure that you’re not assigning or comparing them to integers.
    2. Use Proper Default Values in CASE Statements: When using CASE expressions that return dates, ensure that all possible return values are of the date type. For example:

    CASE

        WHEN condition THEN SomeDateColumn

        ELSE CAST('1900-01-01' AS date)

    END

    1. Avoid using numeric defaults like 0 in such scenarios.
    2. Check INSERT Statements: Ensure that the values you’re inserting into a table match the data types of the corresponding columns. For example, if a column is of type date, make sure you’re inserting a date value, not an integer.
    3. Use Explicit Casting When Necessary: If you need to convert between data types, use explicit casting functions like CAST() or CONVERT() to make the conversion clear and controlled.

    Example

    Problematic Query:

    SELECT

        CASE

            WHEN EntryDate BETWEEN @StartDate AND @EndDate THEN EntryDate

            ELSE 0

        END AS ResultDate

    Issue: The ELSE clause returns an integer (0), which is incompatible with the date type returned in the THEN clause.

    Corrected Query:

    SELECT

        CASE

            WHEN EntryDate BETWEEN @StartDate AND @EndDate THEN EntryDate

            ELSE CAST('1900-01-01' AS date)

        END AS ResultDate

    By ensuring that both the THEN and ELSE clauses return values of the same data type, the error is resolved.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.