Fill column to max value in MySQL? - mysql

I want to know if its possible to automatically fill a column to its max value if the value inside it isn't already at the max value. For example, if I have a column called score that takes an INT(11) and the data in that column is just 23, is there a way I can get it to add a bunch of numbers to the end of it so its size is actually 11 and not 2? so in the end it would look something like 23000000000?

What you ask about is called padding. You could do that at the database schema level, using a varchar column, but I suggest against it for several reason, including no auto-increment numbers. You can easily pad the number as you like while you query the database, for instance using:
SELECT LPAD('columnname',11,'0')

Related

how to populate column from the content of other columns after a row is inserted in the same table in mysql

I have a table traffic with 7 columns, namely toll_id, date1, shift, car_single, car_return, car_local and car_total.
How could I populate first 5 columns manually, and then store a value in column car_total, which will be the sum of car_single and car_return?
Here is the image of my table:
Just to add a 3rd and 4th ways of achieving the desired outcome:
If you have at least MySQL v5.7.6, you can use a generated column as car_total.
Alternatively, you can choose not to store car_total at all, but calculate this value on the fly while querying the table.
Having a column to store the results of the calculation is good if you regularly have search based on that field because you can use indexes to speed up the searches. Calculating the results on the fly may be better, if you just need to display the result of the calculation, but there is no need to filter on it.
There are two ways to do this:
Add the logic in the application itself, so that it calculates total before inserting the record. (Recommended)
Write an after insert trigger (http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html) which calculates the count when record is inserted.

MySQL use "Auto Number Field Value" in another column on insert

Does MySQL allow you to use whatever the next auto number is in another column in the same query?
Column A (auto Number)
Column B (text)
Column C (int)
Column D (text)
INSERT INTO table VALUES ('','some text','THIS AUTO NUMBER','some text')
We are trying to use our "auto number" as the default value for another field. If this is possible can you share the syntax.
Of course if its not possible I can just run three queries, first insert, query for auto number used, then update, but if the functionality exist it would be helpful.
Thank you for your time.
If I got it right, you need to have the value of one column to be computed based on the just obtained auto incremented identifier. This does not seem to be possible directly, especially if you want that value to be changed afterwards (otherwise a computed column might do the trick).
Several answers are indicated here:
use a trigger is one option, but I would not recommend it for this simple case
OR
create a Transaction, perform the insert and then update using generated auto incremented number

add variable number of empty columns to a tablix

I have a tablix with a column group so that it will create a column for that field provided any row has data for that column.
I need to create a version of this report that contains some empty columns on the end.
I want the number of columns to be added to be based on some factor of the non empty columns with some min/max constraints also. (my question is not how to get the number of columns required)
so far i've tried.
1 - adding individual empty columns to the tablix and setting the visibility condition on each column.
a bit long winded and a bit of a faff.
2 - creating another column group and grouping on the same field, this creates
cant vary the number of columns returned.
am i missing a simple way of adding x empty rows or columns to a tablix? where x can be calculated somehow from the values in the dataset.
You will have to fib this scenario in your data. The column-wise grouping works with known data including those falling in the column group value with null values. There is no way to grow your groups without data unless you add some column group footer logic that would be pretty weird.
I would look into producing phantom NULL value records that will push out your columns.

Time counter MSQl

I have a column of TIME type in my db, the column is supposed to hold the sum of two other TIMEs, the problem is that when the number of hours of the result exceeds 24 hours the column is reseted to 00:00:00 again, instead of viewing 25:00:00 which is the result that I want to see, any help on how can I get that without changing the column type?
If you want to store numbers that are bigger than a possible time then that data type is inappropriate.
Use another way to store the data like the sum of seconds. You could use an unsigned int for that.

How do I select a specific column based on a variable in a MS Access query?

I have a large table with the following fields:
Date
Product_ID
AmountEUR_Field1
AmountEUR_Field2
AmountEUR_Field3
AmountEUR_Field4
AmountEUR_Field5
where each AmountEUR field represents the sales amount for a product.
The reason for having 5 different AmountEUR fields is that they are based on different Currency Rates (in example BeginingOfMonthRate, AverageMonthRate, EndOfMonthRate etc.).
I now want to copy a specific AmountEUR field to another table, but the AmountEUR field to be copied varies over time (sometimes it is AmountEUR_Field2, other times it is AmountEUR_Field5).
Therefore I need to select a specific column based on a variable from another table. (that variable should then have value between 1 and 5).
I have been thinking about making a new field called AmountEUR_ToBeUsed that is updated with the correct AmountEUR_Field, but that brings me back to the same problem of selecting the specific column I want copied.
Can a solution be made within the Access query designer, or do I need some VBA code?
You can just make this with the Access Query designer.
Specifically you will need the function IIF.
For instance, if you want to specify that before a date you wish to use AmountEUR_Field1, and otherwise AmountEUR_Field5 you can say:
IIF(somedate<#1/1/2011#,AmountEUR_Field1,AmountEUR_Field5)
Note, depending on the settings of your PC, you may have to say:
IIF(somedate<#1/1/2011#;AmountEUR_Field1;AmountEUR_Field5)