Friday, November 19, 2010

Sql Server CASE/WHEN return type

This post is related to how the Case/When statement return value after the executing condition available.

Problem
Recently I am working on the query where I have to sort data based on the user input but each column in table has different datatype. So to meet with this type of requirement I use case when.. block of the sql server. something as below..

there is not syntax error when you check for the syntax error but when execute code you find there is error

ERROR:Conversion failed when converting datetime from character string.

the problem here is first two having databype Varchar and last one having datatype DateTime. So when you executing its found that one branch having datetime so its gives error even its not executing that.


Solution

Solution 1. 

So to avoid this problem you require to convert dateTime to the string



Solution 2.

Divide the Case when in multiple statement

No comments:

Post a Comment