简体   繁体   中英

Change the column's content of a query result

Here is the kind of result I have after executing my request.

Category

K
O
I
P
Q
K

Here is the kind of result I need.

Category

Key
Non-Key
Intercompany
Non-Key
Non-Key
Key

I don't care if it is ordered or not. To do this I have this xml file.

<BPCategories>
<BPCategory name="Core">
    <type value ="C"/>
</BPCategory>
<BPCategory name="Intercompany">
    <type value="I"/>
</BPCategory>
<BPCategory name = "Key">
    <type value="K"/>
</BPCategory>
<BPCategory name = "Non-Key">
    <type value="0"/>
    <type value="_"/>
    <type value="L"/>
    <type value="N"/>
    <type value="O"/>
    <type value="P"/>
    <type value="Q"/>
    <type value="S"/>
    <type value="V"/>
</BPCategory>
<BPCategory name="WOV">
    <type value="W"/>
</BPCategory>
</BPCategories>

So my question is, Is it possible to do it within an sql query ? You must know that I don't have a table to do the correspondence between the categories. Even if it is possible, Is it not better to do it by doing a for loop on the result set to change the values. In fact I'm looking for the best way to do this in c#.

Thanks in advance ;).

查看SQL CASE表达式

You could easily do that with a CASE in your select statement

Select
CASE Category
WHEN 'K' THEN 'Key'
WHEN 'O' THEN 'Non-Key'
WHEN 'I' THEN 'InterCompany'
WHEN 'P' THEN 'Non-Key'
WHEN 'Q' THEN 'Non-Key'
WHEN 'K' THEN 'Key'
END as Category
FROM ...

assuming this is MS-SQL server: http://msdn.microsoft.com/en-us/library/ms181765.aspx

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