Add two prices to make one - linq-to-sql

Morning,
I would like to know how i could add two prices from my database to make one price. My current code is below.
orderPriceTotal = oi.price + oi.shippingPrice,
All help is much appreciated, thanks :)

It should work.. but i think if these aren't in numerical values then first u need to convert into numerical then do addition.

If oi.price and oi.shippingPrice are numeric (hopefully decimal) values then what you have should just work.
If they are already strings then go back to the data source and return them as decimal values.
If you can't do that then you'll need to convert them back to numerical values, add them and then convert back into a string.
System.Globalization.CultureInfo culture = <your culture>;
decimal price;
if (decimal.TryParse(oi.price, NumberStyles.Currency, culture, out price);
The NumberStyles.Currency will allow you to parse a string that might contain a currency symbol.
Do the same for shippingPrice then:
orderPriceTotal = (price + shippingPrice).ToString("C", culture);
to convert back to a string complete with the correct currency symbol.

Related

How to store integer data with comma in it?

I am having some difficulties about storing integer data with commas, I have prices, which is like 4,600 So I need to store it with commas but when I try to send it as Integer it cut after first number. I tried to change column type. BigInt or Double but it doesn't effect any. Any possible way to do that?
Also tried to change comma to dot "." but with this, mysql delete the "0" at last... I don't know why...
Prices
------
4,500
2,300
1,500
Because you're using a comma, MySQL most likely interprets the number as two fields, separated by the comma. For example:
Prices,Unspecified
------,-----------
4 ,500
2 ,300
1 ,500
In the numbers in question: If the comma is a thousands separator, remove it (via String replace) before trying to store the number. If it's a decimal point, replace it with a period (via String replace) and store it as a DOUBLE (or DECIMAL if you need high accuracy for large numbers).
If you want to display the number with a comma, use String formatting (possibly a number-formatting function other than String.format() or sprintf()) after retrieving the value from the database.
If you want to be able to do calculations using SQL queries with those numbers, then you need to store the price either use the DECIMAL type or use integer types and multiply the number by e.g. 1000 before saving.
Double or any other floating point representation of numbers are not suitable for price calculations/storage.
If you use DECIMAL need to convert the number form your local format 4,5000 to the format the database expects when you store it in the database, and convert it back to the local format when you retrieve your data.
If you store it as string then you can keep your local format but that's the worst solution, and should never be used.

questions about the datetime type values

I have several things which I want to discuss with you guys.
Since, they were just simple questions, so no dataset here.
Suppose I have a datetime type column which called started_date, the value in it was like:
yyyy-mm-dd hh:mm:ss. So, if I want to select some IDs which were larger than one specified day (let's say June/01/2017), can I just using
select ID, started_date
from table1
where started_date>"2017-06-01";
Does this work?
I tried some samples, and it worked indeed in the mysql. However, someone told me that I cannot compare the datetime column with string values without converting their format. And it confused me. Because I thought the value "2017-06-01" here was date type value, so it does not need convert. Or am I thinking wrong?
Another thing was about the double quote and single quote, I understand that the single quote was used for string values. However, in this case, when I used double quote to quote "2017-06-01", it works. So, does it mean the double quote can quote date values?
I am just asking, so any response is welcome.
Thanks.
Your query is fine. You are using a safe date/time format for the string. In other words, if you have to store the value as a string, then use that format.
I would write the code as:
where started_date >= '2017-06-01'
I see no reason to exclude midnight on 2017-06-01 (although you might have a reason). Second, single quotes are the standard delimiter for strings.
That said, you can store the value as a string.
As a best practice, I stay away from comparing time-stamps to date-stamps. In this case you can be explicit and truncate the start date. And yes, use single quotes instead.
where SUBSTR(started_date, 1, 10) > '2017-06-01'
To make sure it works you could just convert the date time to a string first and compare the two strings:
to_char(started_date,'YYYY-MM-DD') >= '2017-06-01'
The Strings will compare just fine in that format.

Format Function returns wrong value

We have a field in a query that should be left-padded with zeroes if it is too short, and we accomplish this using the Format() function. However, there are some values that produce bizarre results.
Format("14425112-8","00000000-00")
Returns the value "00019330-78"
For most inputs, the string gets formatted as expected, 8 digits, hyphen, two digits. But in a few rare cases, the value is modified. Is this repeatable for anyone else? Does anyone have an explanation?
Thanks for your help.
This is an example of access trying to be too helpful. It looks like it is interpreting these values as dates, but since you didn't use any date indicators in the format e.g: (dd,mm,yyyy), it converted 1-1 to a date, and then tried to display it in decimal form:
debug.print Format("1-1","000000-00")
returns 000427-36 which is the decimal value 42736 which, if you convert to a date, becomes 1/1/2017. This is what access interpreted "1-1" as.
it seems that access has reserved the - character as symbolizing a date format, despite what their website says. This function is only useful for formatting actual dates, or numeric values, such as prices. If you are set on using the format function, you will have to change you separator to a decimal point, which apparently is the only character that will get you what you want with the leading and trailing zeros.
Otherwise, you may have to build your own function for this.
You cannot format a string like a number this way. Try this:
PaddedNumber = Right(String(8, "0") & "14425112-8", 10)

Mysql don't sort correct value

I have problem with mysql sorting so if some one can help me out to solve this i will be
so happy before i dont understand it at all
weird info
Try doing this:
ORDER BY (best_feralhog + 0.0)
It will sort numerically, but it will ignore the suffix letters.
These are sorted.
They are varchar(10). They are sorted using string comparison. The string "1000" is smaller than the string "999" because the first character of the first string, "1", is smaller than the first character of the second string, "9".
If these are numbers, you should store them as such in the database. Consider using the decimal type.
If you really need to store as varchar, you will need to convert (cast) the values to decimal values before sorting.

could the MySQL show percent sign (%) in table

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))+'%'