OLEDB CASE WHEN in SELECT Query [closed]

大憨熊 提交于 2020-01-05 16:52:52

问题


How can use case when statement with oledb to excel file ?
Like select prodid, case prodid when 1 then 'fine' when 2 then 'good' end


回答1:


You'll have to use IIF instead of CASE when querying Excel, eg

SELECT prodid, IIF(prodid = 1, 'fine', IIF(prodid = 2, 'good', '')) FROM MyExcel

As you can see, it can get messy quickly. It's not too bad if you've only got a two-way evaluation, but in your code it's not clear whether you only have 2 possible values for prodid or more than that. My example assumes there are other values, hence the nested IIF.




回答2:


OLEDB is only a "relay" of sort and merely repeats the query to the underlying source.

I don't believe Excel supports the CASE WHEN construct, and thence you cannot use it, even through OLEDB.

You can instead use an Excel-specific idiom, the IIF() function, as described in CodeByMoonlight's answer. As noted the need to nest IIF() calls makes is a rather messy proposal for situations that require more than 3 or 4 cases.



来源:https://stackoverflow.com/questions/1741939/oledb-case-when-in-select-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!