Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a SQL Statement (see What you have tried). I would like to convert Preis to nvarchar before using the case.

I tried it with

select Kategorie, modell, bezeichnung, convert(nvarchar(20),(preis),0) as Preis =
case
        when '{state:RepEuro}' != 'Smart Repair' then Preis
        else '140'
    end 
from tblminderwerte
where id = '{state:MinderwertSelect}'


But this results in an error. What would be the Right way?

What I have tried:

select Kategorie, modell, bezeichnung, Preis =
case
        when '{state:RepEuro}' != 'Smart Repair' then Preis
        else '140'
    end 
from tblminderwerte
where id = '{state:MinderwertSelect}'
Posted
Updated 8-Nov-18 7:16am
Comments
MadMyche 8-Nov-18 11:12am    
Table structure would be nice

The value to be CONVERTed is the second element within the statement.
For your instance there looks to be different different methods to solve the combination of CASE & CONVERT
1. You can place the entire case statement as the converts value
2. You can place the converted value as the case value requiring conversion.

Proof of concept here leads me to believe that you will want the first option.
SQL
DECLARE @Temp TABLE (CaseEvaluation BIT, preis MONEY)

INSERT @temp 
VALUES (1, 123.45 ), (0, 543.21)

SELECT 
    Preis1 =
        CONVERT (
            NVARCHAR(20),
		    CASE
		        WHEN (CaseEvaluation = 1) THEN Preis
			    ELSE '140'
            END,
		  0
	   ),
    Preis2 =
        CASE
           WHEN (CaseEvaluation = 1) THEN  CONVERT (NVARCHAR(20), Preis, 0)
		   ELSE '140'
        END

FROM @Temp
--  Case   Preis1  Preis2
--  ------ ------  ------
--  True   123.45  123.45
--  False  140.00  140

For what's it is worth; you could probably use CAST in this instance.

If this query is being called by an external application and it is numeric/money, I would recommend returning the values without the convert, so that the application can easily work with the values presented and allow for proper regional display.
 
Share this answer
 
Comments
Sascha Manns 9-Nov-18 6:03am    
Hi all, thank you very much for all Solutions and comments. In my case the Solution2 from @MadMyche works perfectly. Thank you very much :-)
MadMyche 9-Nov-18 7:50am    
You're welcome
I'm not at all sure what you are trying to do there - your comparison will always suceed because the two strings will never match.
But if what you are trying to do is convert a numeric value to a NVARCHAR then it's trivial:
select Kategorie, modell, bezeichnung, 
    case
        when '{state:RepEuro}' != 'Smart Repair' then convert(nvarchar(20),(preis),0)
        else '140'
    end AS Preis
from tblminderwerte
where id = '{state:MinderwertSelect}'


But I'm a little worried that you are showing code from your app, and there is parameter substitution going on, replacing {state:RepEuro} with the variable content in your app.
If so, then don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 

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