简体   繁体   中英

Cannot convert the datetime to char in sql

I have the following query in SQL:

SELECT DISTINCT Id,Name,Date, WeekOffDate 
FROM tblEmployee 
LEFT JOIN tblWeekOff

So clearly, Id and Name come from tblEmployee and WeekOffDate comes from tblWeekOff .

Now, in the Date column, I am writing this piece of query:

SELECT DISTINCT 
    Id, Name, 
    (CASE WHEN Date = WeekOffDate THEN 'WO' END) 
FROM tblEmployee

But I get this error saying:

Conversion failed when converting date and/or time from character string.

So I to overcome this error, I tried this

SELECT DISTINCT 
    Id, Name, 
    (CASE WHEN CAST(Date AS nvarchar) = CAST(WeekOffDate AS nvarchar) THEN 'WO')
FROM tblEmployee

ie I tried casting. Similarly I tried CONVERT() function, but no luck.

My goal is to show the "WO" status where the date matches with the WeekOffDate in a table. But I am really surprised why is this happening?

EDIT:

The WeekOffDate data looks like this:

休息日

Where am I lacking?

Could you try this.

(CASE WHEN CONVERT(NVARCHAR(10), CONVERT(DATETIME, CONVERT(DATE, DATE)) , 103) = CONVERT(NVARCHAR(10), CONVERT(DATETIME, CONVERT(DATE, WeekOffDate))  , 103) THEN 'WO' ELSE 'YES'END)

EDIT: Warning is nothing more than a warning in this case. It is just saying the fact. You can overcome this with adding an else statement in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM