Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello CodeProject,

currently i'm writing an app for an landlord/lesser.
I have created a table which have a column "Muell", defined as decimal(6,2).
I'm using an Inputfield with
SQL
SELECT distinct CONCAT(e.Miete, " €") AS Miete,
as default content. If the user changes the field, and leaves the "€" i'm running into an error.

I'm using that statement:

SQL
UPDATE tbl_nebenkosten
    SET Muell = '$Muell',
        Wasser = '$Wasser',
        Versicherung = '$Versicherung',
        Strom = '$Strom',
        Hausverwaltung = '$Hausverwaltung',
        Heizung = '$Heizung'
    WHERE MieterID = '${state.MieterID}'


I read MySQL SUBSTRING_INDEX() function - w3resource[^]. On that page just a example with "SELECT" is given.

In my case i need UPDATE instead.
Sadly i just started programming with SQL, so many is new for me.

What I have tried:

SQL
UPDATE tbl_nebenkosten
    SET Müll = SELECT SUBSTRING_INDEX('$Muell',' ',1)
        Wasser = '$Wasser',
        Versicherung = '$Versicherung',
        Strom = '$Strom',
        Hausverwaltung = '$Hausverwaltung',
        Heizung = '$Heizung'
    WHERE MieterID = '${state.MieterID}'


That one gives me an error.
Posted
Updated 23-Apr-18 2:07am
v3

Validate the values (and optionally clean them up like removing a currency symbol) before passing them to the database. That allows you also to show specific error messages upon invalid input. Otherwise you are stuck to SQL error messages which are not meaningful for most users.

How to do the validation depends on the used language and how data are passed (e.g. from a form or importing from a file).

You should also use parametrised SQL queries. With those data are passed as objects and not as strings which avoids SQL injection - Wikipedia[^].
 
Share this answer
 
Comments
Sascha Manns 23-Apr-18 7:28am    
In general the Inputfield is connected to a data capture container. There the value will be used in a variable and then i'm trying to save it.
The Inputfield itself uses a database query (the mentioned CONCAT command) to get the "old" price from the database. So the user can see what value are stored and it is visible in the Inputfield. If the user changes nothing, the variables will be written into the database.
What would be a good statement to remove the "€" if exists?
Jochen Arndt 23-Apr-18 7:50am    
That can be only answered when knowing the used programming language or framework.
Sascha Manns 23-Apr-18 7:52am    
I'm using a composer like this https://www.engomo.com/de/home/
Jochen Arndt 23-Apr-18 8:05am    
Then you should ask there for support. Input validation and clean up should be done by the framework, not by the database. There will be some framework used by the composer. There may be options to access that, options for input validation, or nothing. But only they would know.

In any case you can move the display of the Euro symbol out of the input box by placing it right of it or within the label.
Sascha Manns 23-Apr-18 8:08am    
Hi Jochen. I found a solution (See below). That works as expected.
I found it out. That snipped works as expected:

SQL
UPDATE tbl_nebenkosten
    SET Müll = TRIM(TRAILING ' €' FROM '$Muell'),
        Wasser = TRIM(TRAILING ' €' FROM '$Wasser'),
        Versicherung = TRIM(TRAILING ' €' FROM '$Versicherung'),
        Strom = TRIM(TRAILING ' €' FROM '$Strom'),
        Hausverwaltung = TRIM(TRAILING ' €' FROM '$Hausverwaltung'),
        Heizung = TRIM(TRAILING ' €' FROM '$Heizung')
    WHERE MieterID = '${state.MieterID}'

TRIM does a great job :-)
 
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