Click here to Skip to main content
13,768,884 members
Rate this:
 
Please Sign up or sign in to vote.
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 8-Nov-18 6:06am
Updated 8-Nov-18 8:16am
Comments
MadMyche 8-Nov-18 11:12am
   
Table structure would be nice
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
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.
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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:
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:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
DROP TABLE MyTable;
A perfectly valid "delete the table" command
--'
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?
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.181117.1 | Last Updated 8 Nov 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100