i want to show some data in percent.
i have a mathematics formula like:
(qty(S) + qty(B))/qty(id)*100%
could i show the result for example like 25%? how do i do that?
Databases are used for storing data. Presentation of data should not be in its responsibilities. By that, I mean you should very rarely thing about storing a string value in the database like '75%'.
If you want specific formatting, the best place to do it is after extracting the data:
select concat(your_column,'%') as percent ...
Because concat expects strings, numeric values are automagically cast into string before joining them together.
It's a presentation thing, but it's handled in the same fashion. You need to change the data type of the result to a string based one:
CAST((qty(S) + qty(B))/qty(id)*100 AS CHAR(2))+'%'
Related
I'm using SSIS to separate good data from unusable date. In order to do that I used derived columns, script task and conditional split where I assigned certain conditions. One of the conditions I need to apply is that none of the numbers in one column cannot be negative. I'm guessing that the best way to solve this would be using conditional split, but I cannot get it to work. I'm new to SSIS, so any help would be appreciated.
You'd have an Expression like
[MyCaseSensitiveColumnName] < 0
and then name the output path something like BadData_NegativeValue
From the comments
that is what I did before, but I'm getting an error saying that The data types "DT_WSTR" and "DT_I4" are incompatible for binary operator ">"
That error message indicates that you are attempting to compare a unicode string (DT_WSTR) and an integer (DT_I4) and that the expression language does not allow it.
To resolve this type incompatibility, you would need to first convert the value of MyCaseSensitiveColumnName from DT_WSTR to an integer.
I'd likely add a Derived Column Component to my data flow and create a new column called MyCaseSensitiveColumnNameAsInteger with an expression like
(DT_I4) [MyCaseSensitiveColumnName]
Now, that may be perilous depending on the quality of your source data. I don't know why you are pulling numeric data in as a string. If there could be non whole numbers in the data set, then we will need to check before making the cast. If there are NULLs in that dataset, those too may cause issues.
That would result in our conditional split check becoming
[MyCaseSensitiveColumnNameAsInteger] < 0
In a project I am working on, I have a column in my database named Amount. This is a money format, but there is no need for it to have a dollar sign in front.
I would like every value that is entered in here to be this format:
123456 -> 123,456.00
How can I achieve this?
No, the stored value should not be formatted, it should be stored as a plain number, otherwise you can't do any arithmetic calculations.
When the number is displayed, that's when you format its appearance either in the application layer (that's the preferred approach) or you can use MySQL's own format() function in the select statement to achieve this:
select format(amount) from your_table
I have tried using 'LIKE' but it runs into problems which i will explain below.
i have a string column that looks like any of these.
"1010, 2020, 3030"
"1010"
""
I want to be able to see if this string contains a single ID. e.g 2020. if it does then return the row. I tried using like but if the id is 20 it will return the row because 2020 contains 20.
Selecting the entire db and then using a delimiter to go through all the strings will take far too much time. Is it possible to implement this?
This is why you don't store multiple values in a single field. Because your bad design, this is the query structure you'll have to use EVERY SINGLE TIME to compensate for it:
WHERE
foo = 2020 // exact match, only value in field
OR foo LIKE '2020,%' // value is at start of field
OR foo LIKE '%,2020,%' // value is somewhere in the middle of the field
OR foo LIKE '%,2020' // value is at the end of the field
Or you could have had a properly normalized design, and just done
WHERE childtable.foo = 2020
and be done with it.
First, you should not store lists of things in string variables. SQL has a very nice data structure for lists. It is called a table. Each row in such a table would have an id and one value from the list.
That said, sometimes you are stuck with data like this. In that case, you can use find_in-set():
where find_in_set('20', replace(stringcolumn, ', ', ',')) > 0;
You can also do the logic with like, but MySQL has the convenient built-in function for this.
EDIT:
If you want to do this with like:
where concat(',', stringcolumn, ',') like '%,20,%'
Note that the delimiters "protect" the values, so 20 is not confused with 2020.
I've inherited a database and am using views in MySQL to return specific information from a database. I'm having a little trouble one portion- turning the returned numeric value that is a decimal into a percentage.
I don't want to do this in the php- I am specifically trying to format everything in the view first. I realize this is not necessarily the way everyone does this, but for my specific situation it is the best overall solution.
The example value in the database: 0.46
What I'd like to have returned: 46%
Relevant portions of current query:
SELECT CONCAT(a.fa_rate, '%') as rate_percent
FROM accounts a;
This returns 0.46%. Is it possible to return the result I want to have without converting the number into a string? The current data type is a float.
try this
SELECT CONCAT(a.fa_rate * 100 , '%') as rate_percent
FROM accounts a;
I have a situation where i need to store "Error Type" which has following options
I want to know what is the best way I should use to create my table "Error".
So either I take a "VARCHAR" data type and store values like "1,3,4" (Comma Separated) if "Take Off, Details and Legend" is selected and parse it when getting in view
OR
Take separate column for each field in table with data type "TINYINT" like "IsTakeOff" , "IsSpecifications" ,"Details" etc.
Please advice
Thanks
If user with id 1 select 1,3 and 4 then you can use following
Don't use multiple columns unless you are very confident the number won't grow.
Otherwise use a many-to-many association table - one with columns for the id of the item and the id of the error
One way to go might be to do it as an int and then in your code do something like this PHP, I don't know language you are using but most languages have a switch so it shouldn't be to hard to translate to another language.
$row // The row from the database.
switch ($row['Error_Type'])
{
case ('0'):
{
// Do something.
break;
}
...
}
You can use comma seperated column here that is varchar because you know that no of records will not grow here. And this is limited to only 5 values. And while searching the field you can use MySQL FIND_IN_SET which is very effective for this kind of situations. FIND_IN_SET will take two parameters. Your search keyword and the comma seperated string.