mysql calculation result forcefully rounded - mysql

I have this field:
user_id VARCHAR(20);
insert_date DATE;
fgps DECIMAL(5,2);
in a table, and is calculating the value using these (simplified) statements:
-- #num1, 2 and 3 are SET with some query to get the values
SET #gps_average := ((#num1 + #num2 + #num3) / 3.0);
INSERT INTO user_gps VALUES(:user_id, :insert_date, #gps_average);
The script is working fine, except for one thing -- the result of #gps_average is forced to be rounded by the mysql server. i.e. if I have the values
#num1=3, #num2=4, #num3=4
I should get 3.67 stored in fgps, but instead 4.00 is what I got.
What have I done wrong or how do I force the server to no round up my values?
I've done similar calculations on some other fields and the values are stored with decimal values correctly, but not in this case, this is driving me crazy...
Thanks in advance.

Related

Get value between from to dataset columns ssrs

I have a data set like that:
Data Set Contents
From To Comment
----+---+--------
0 50 Bad
50 70 Good
70 100 Excellent
If I have a value of 75, I need to get Excellent by searching the Dataset.
I know about the lookup function but it is not what I want. How can I do that?
The values should be in percentage.
Note : the value (75) is Average of a column (Calculated) it
calculate student grade from max and student mark Version SQL Server
2016
Note 2 : the dataset is from database not static values
Thank You
Assuming you only ever have a fixed number of 'grades' then this will work. However, I would strongly recommend doing this type of work on the server where possible.
Here we go...
I created two datasets
dsGradeRange with the following sql to recreate your example (more or less)
DECLARE #t TABLE (low int, high int, comment varchar(20))
INSERT INTO #t VALUES
(0,49,'Bad'),
(50,69,'Good'),
(70,100, 'Excellent')
SELECT * FROM #t
dsRandomNumbers This just creates 30 random numbers between 0 and 100
SELECT *
FROM (SELECT top 30 ABS(CHECKSUM(NEWID()) % 100) as myNumber FROM sys.objects) x
ORDER BY myNumber
I added a table to the report to show the grades (just for reference).
I then added a table to show the dsRandomNumbers
Finally I set the expression of the 2nd column to the following expression.
=SWITCH
(
Fields!myNumber.Value < LOOKUP("Bad", Fields!comment.Value, Fields!high.Value, "dsGradeRange"), "Bad",
Fields!myNumber.Value < LOOKUP("Good", Fields!comment.Value, Fields!high.Value, "dsGradeRange"), "Good",
True, "Excellent"
)
This gives the following results
As you can see we only need to compare to the high value of each case, the first match will return the correct comment.
Right click on your dataset and add a calculated field. Go to Field Properties > Fields > Add and add the following expression, which descripes your scenario:
=IIF(Fields!Number.Value < 50, "Bad", "Good")

I need a trigger to create id's in my sql database with a string and some zeros

I'm currently using this trigger which adds id's with 3 zeros and two zeros and then the id from the sequences table.
BEGIN
INSERT INTO sequences VALUES (NULL);
SET NEW.deelnemernr = CONCAT('ztmr16', LPAD(LAST_INSERT_ID(), 3, '0'));
END
I changed the 3 to 4 but then it didn't increment the id anymore, resulting in and multiple id error. It stayed at ztmr16000. So what can I do to add more zeros and still get the id from the sequencestable?
The MySQL LPAD function limits the number of characters returned to the specified length.
The specification is a bit unclear, what you are trying to achieve.
If I need a fixed length string with leading zeros, my approach would be to prepend a boatload of zeros to my value, and then take the rightmost string, effectively lopping off extra zeros from the front.
To format a non-negative integer value val into a string that is ten characters in length, with the leading characters as zeros, I'd do something like this:
RIGHT(CONCAT('000000000',val),10)
As a demonstration:
SELECT RIGHT(CONCAT('000000000','123456789'),10) --> 0123456789
SELECT RIGHT(CONCAT('000000000','12345'),10) --> 0000012345
Also, I'd be cognizant of the maximum length allowed in the column I was populating, and be sure that the length of the value I was generating didn't exceed that, to avoid data truncation.
If the value being returned isn't be truncated when it's inserted into the column, then what I think the behavior you observe is due to the value returned from LAST_INSERT_ID() exceeding 1000.
Note that for a non-negative integer value val, the expression
LPAD(val,3,'0')
will allow at most 1000 distinct values. LPAD (as I noted earlier) restricts the length of the returned string. In this example, to three characters. As a demonstration of the behavior:
SELECT LPAD( 21,3,'0') --> 021
SELECT LPAD( 321,3,'0') --> 321
SELECT LPAD( 54321,3,'0') --> 543
SELECT LPAD( 54387,3,'0') --> 543
There's nothing illegal with doing that. But you're going to be in trouble if you depend on that to generate "unique" values.
FOLLOWUP
As stated, the specification ...
"adds id's with 3 zeros and two zeros and then the id from the sequences table."
is very unclear. What is it exactly that you want to achieve? Consider providing some examples. It doesn't seem like there's an issue concatenating something to those first five fixed characters. The issue seems to be with getting the id value "formatted" to your specification
This is just a guess of what you are trying to achieve:
id value formatted return
-------- ----------------
1 0001
9 0009
22 0022
99 0099
333 0333
4444 4444
55555 55555
666666 666666
You could achieve that with something like this:
BEGIN
DECLARE v_id BIGINT;
INSERT INTO sequences VALUES (NULL);
SELECT LAST_INSERT_ID() INTO v_id;
IF ( v_id <= 9999 ) THEN
SET NEW.deelnemernr = CONCAT('ztmr16',LPAD(v_id,4,'0'));
ELSE
SET NEW.deelnemernr = CONCAT('ztmr16',v_id);
END IF;
END

insert data into one table from another table on specific condition

I have "table1" in which master data store like -
id name create_date validity expire_date
1 A 2015-08-01 3 2015-11-01
2 B 2015-09-01 12 2016-08-01
3 C 2015-09-15 1 2015-10-15
But now want to insert data in "table2" for expire_date according to validity period like without changing in front end. using trigger or procedure want to achieve this task.
id parent_id expire_date
1 1 2015-09-01
2 1 2015-10-01
3 1 2015-11-01
How can I achieve this using procedure or trigger.
It's hard to be specific because your question is not specific.
In general, here's the procedure to follow to design a query to insert stuff from one table into another.
First, write a SELECT query yielding a resultset containing the rows and columns you want inserted into your table. Use a list of columns to get the right columns, and appropriate WHERE clauses to get the right rows. Eyeball that query and that resultset to make sure it contains the correct information.
Second, prepend that debugged SELECT query with
INSERT INTO target_tablename (col, col, col)
Test this to make sure the correct rows are being inserted into your target table.
Third, create yourself an EVENT in your MySQL server to run the query you have just written. The event will, at the appropriate times of day, run your query.
If you take these steps out of order, you'll probably be very confused.
Can achieve the task using Store Procedure -
CREATE DEFINER=`root`#`localhost` PROCEDURE `addexpire`(IN uname varchar(50), IN cdate date, IN vm int)
BEGIN
insert into table1 (name,create_date,validity) values (uname,cdate,vm);
BEGIN
declare uparent_id INT;
declare v_val int default 0;
SET uparent_id = LAST_INSERT_ID();
while v_val < vm do
BEGIN
declare expire_date date;
SET expire_date = DATE_ADD(cdate,INTERVAL v_val+1 MONTH);
insert into table2 (parent_id,expire_date) values (uparent_id,expire_date);
set v_val=v_val+1;
END;
end while;
END;
END

Perform MySQL select on unsorted digits

I am working on an application that requires me to validate if 3 randomly generated numbers match a 3 digit string that has been entered into a database from user input. I also need to preserve the exact order that the user enters the string, so sorting on input is not an option.
For example, the randomly generated digits may be 6 4 0, and in the database a string may show as '406'.
Is there an easy way this can be accomplished in a single query without enumerating the options or adding an extra column/view?
maybe you could try
create table y (z varchar(10));
insert into y values ('406');
insert into y values ('604');
insert into y values ('446');
insert into y values ('106');
insert into y values ('123');
and then
SELECT * from y where FIND_IN_SET(Substring('640',1,1),MAKE_SET(7,Substring(z,1,1),Substring(z,2,1),Substring(z,3,1))) and FIND_IN_SET(Substring('640',2,1),MAKE_SET(7,Substring(z,1,1),Substring(z,2,1),Substring(z,3,1))) and FIND_IN_SET(Substring('640',3,1),MAKE_SET(7,Substring(z,1,1),Substring(z,2,1),Substring(z,3,1)));
returns
406
604
Sum the three random digits
Something like
Select * From Triplets Where (Ascii(Substring(Number,0,1)) - 48) + (Ascii(substring(Number,1,1)) -48) +
(Ascii(substring(Number,2,1)) -48) = MySumOfNumber
easy is a state of mind isn't it, Storage requirement of an extra "CheckSum" int, versus the high cost of a query like this.

mysql double values checking problem

I wanna make a query that fetches only the rows that has 'cost' value grader than zero.The cost column has double data type.When i write a query like that,
select cost from xxx where cost>0;
it retrieves the rows only that has value grader than or equal to one.For example it doesnt take like 0.02 or 0.3 values.The query sees these type values as zero.How can i achieve my goal?
Thanks for advance...
I can't replicate your problem using mysql 5.41.
Show us the result of describe table xxx;
What happens if you issue the query:
select cost from xxx where cost > 0.0;
Is your query actually:
select ceil(cost) from xxx where cost > 0.0;
If so, for values of cost > 0 but <= 1, you'd get a result set of 1.