Click here to Skip to main content
15,914,163 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
i need some solution about my sql query.
my xml is:

HTML
<main>
    <data>
        <counter>A</counter> 
    </data>
</main>



my query is :

C#
CREATE TABLE #temp
(
   
    COUNTER VARCHAR(1)
);
 
INSERT #temp
SELECT    
    Data.value('(/COUNTER/text())[1]','VARCHAR(100)') AS COUNTER,

FROM
    @XML.nodes('/main/Data') AS AdviceData (Data);

INSERT INTO SECONDTABLE(COUNTER)
SELECT CASE T.COUNTER WHEN 'A' THEN 1 END AS COUNTER
FROM #temp T



The above query is working fine.
but i don't want to use case statement. why because sometimes counter is Z then i should show 26.
without case statement how can i achieve this.

thanks in advance

What I have tried:

how to achieve count instead of case statement in sql?
Posted
Updated 15-Jun-16 3:07am
v3
Comments
Richard Deeming 15-Jun-16 9:22am    
NB: XML is case-sensitive. Data won't match a node called data, and COUNTER won't match a code called counter.

1 solution

You can make use of the ASCII function and convert it that way.

SQL
declare @counter as nvarchar
set @counter = 'A'

select ascii (@counter) -64 as Counter


The acsii value for uppercase A is 65, so simply knock 64 off the result. Bear in mind that lowercase values are different so you may want to force it to upper unless you're confident that the value in the xml is always uppercase.

In your case

SQL
INSERT INTO SECONDTABLE(COUNTER)
select ascii (t.COUNTER) -64 as Counter
FROM #temp T
 
Share this answer
 
v2
Comments
Maciej Los 15-Jun-16 13:35pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900