Not have a minimum MySQL INT Value - mysql

When I add a number beginning with 0 into my MySQL database, it automatically gets converted to a single digit. These are mobile numbers, so I need to keep it starting with 0.

Store phone numbers as strings, not integers. (related: Common MySQL fields and their appropriate data types )

Try storing the numbers as varchars instead. When you retreive them from the database you could cast them using (int) if needed.

Related

What datatype should I use for software version in mysql? [duplicate]

What data type should i use for a SQL column to store Product version eg.
Version
0.1
0.1.1
0.2
1.1
1.1.647
2.0
.....
In query i should be able to sort them based on version number and i want an optimal query to find highest number.
Thanks
I would consider storing each part of the number in a separate TINYINT/SMALLINT field.
Perhaps have three or four numeric fields to the version table:
Major, Minor, Revision, Build
A good solution would be to use an integer building the value to store like so:
MAJOR * 10000 + MINOR * 100 + Revision
Assuming each one can range from 0..99. If you want to go 0..999 use
MAJOR * 1000000 + MINOR * 1000 + Revision
This will sort properly, will query easily, is compact (1 int column), is easily decomposed and can even be decomposed visually.
Storing in separate numeric fields is a good idea. Storing as a string in a single field will break sorting when one of the parts reaches 1000. For example, 1.2.999 will appear before (or shown as newer than) 1.2.1000 when it should appear after.
You may want to store the versions in two separate columns. One as VARCHAR which would store the version value verbatim (to display in UI) and one as DECIMAL for sorting.
How to store a version as a DECIMAL in MySQL which is sortable
Assuming each part of a version can be a maximum of 4 digits, a version can be stored as:
"#{major}.#{format(minor)}#{format(revision)}"
Where the format() function is defined as:
sprintf("%0.4d", i)[0,4]
This value can be stored in a DECIMAL(12,8) column.
E.g., version 2021.7.115 would be stored as 2021.00070115.
If each part of a version can be maximum 5 digits (sprintf("%0.5d", i)[0,5]), above format would require a DECIMAL(15,10) column.
E.g., version 2021.7.115 would be stored as 2021.0000700115.
Why DECIMAL and not INTEGER data type?
The advantage of DECIMAL is that more digits can be appended to the right side in future without affecting sorting, which is not possible for INTEGER columns. E.g., {major}.{minor}{revision} can be changed to {major}.{minor}{revision}{morestuff} without affecting sorting (which wouldn't be possible for INTEGER columns without updating existing values).
PS: How to store a version as VARCHAR in MySQL which is sortable
Tricks from above can also applied to store the version in a string column.
E.g., a format
"#{format(major)}#{format(minor)}#{format(revision)}"
can store version values as a string in a VARCHAR column. Storing versions in a VARCHAR column would require more storage space than DECIMAL but it might be a necessity if version values include non-numeric characters.
E.g.,
Version 1.2.4 would be stored as "000100020004".
Version 2021.7.115 would be stored as "202100070115".

Can I use NUMERIC instead of BIGINT?

I have an old database table with column, which type is BIGINT. There's a lot of stored procedures and views that use that table and that column.
For some reason I need to change the type of that column to NUMERIC(38,0).
Is it safe to do it? Should I cast in any stored procedure and view existing BIGINT to NUMERIC(38,0)?
According to me numeric data type is identical with decimal which represents a fixed precision number, which will scale numeric data from -10^38 +1 through 10^38 –1
I don't think that the number types you mention are using fixed precision number and therefore BIGINT is probably the most efficient way to store the number especially if you want to perform some computation in your application.
I don't see really any use for computation with those number and therefore you may even use a string of appropriate length which requires more space in the database but you may be able to allow grouping characters in the numbers.
using BIGINT datatype instead of string you can create efficient indexes.
As you write you're already using numeric datatype and therefore if you upgrade to SQL 2008R2 / 2012 you should consider switching to BIGINT as you don't need fraction in your number. The BIGINT data type is intended for use when integer values might exceed the range that is supported by the int data type.
EDIT:
You can change the data type from BIGINT to NUMERIC(38,0) but be ensure that a Arthimetic overflow error shouldn't occur while converting.
Yes, it is.
According to this table on MSDN an numeric(38,0) has an higher capacity than a bigint.
I calculated the maximum values based on the numbers in the matrix:
9223372036854775808 (bigint, 2^63-1, 8 bytes)
1000000000000000000000000000000000000000 (numeric(38,0), 10^38–1, 17 bytes)

Conversion of strings with binary values

This is a question of converting strings from DB2 to SQL Server.
On DB2 you can have a column that contains a mix of strings and binary data (e.g. using REDEFINS in COBOL to combine string and decimal values into a DB2 column).
This will have unpredictable results during data replication as the binary zero (0x00) is treated as string-terminator (in the C family of software languages).
Both SQL Server and DB2 are able to store binary zero in the middle of fixed length char columns without any issue.
Has anyone any experiences with this problem? The way I see it, the only way to fix it, is to amend the COBOL program and the database schema, so if you have a column of 14 chars, where the first 10 is a string and the last 4 a decimal, split this up into two columns containing one "part" each.
If you want to just transfer the data 1:1, I'd just create a binary(x) field of equal length, of varbinary(x) in case the length differs.
If you need to easily access the stored string and decimal values, you could create a number of computed columns that extract the string/decimal values from the binary(x) field and represents them as normal columns. This would allow you to do an easy 1:1 migration while having simple and strongly typed access to the contents.
The optimal way would be to create strongly typed columns on the SQL Server database and then perform the actual migration either in COBOL or whatever script/system is used to perform the one time migration. You could still store a binary(x) to save the original value, in case a conversion error occurs, or you need to present the original value to the COBOL system.

Padding the beginning of a mysql INT field with zeroes

I have an INT field in a large MySQL database containing incremental numbers in an INT field. These numbers are currently regular autoincrement numbers (1, 2, 3) but I need to pad them to three digits with zeroes at the beginning (so I get 001, 002, 003.. 010, 011, etc).
What commands can I run on my database to change this column into the format I need?
You can add a ZEROFILL attribute to the column to pad the data in the database or, when querying,
SELECT LPAD(CONVERT(`col`,VARCHAR(3)),3,'0')
to retreive the data formatted as a 3 digit number
There is no such thing as having leading zeroes on data in a numeric field in the database; the data just isn't stored that way, any more than it is stored in roman numerals. All you've got is the number three; so if you want to get the string "003" out, you've got two options:
Change to use a string field in the database: not recommended because you can't easily get incrementing numbers.
Format the number as you retrieve it from the database to add leading zeroes: better, but it has its own disadvantages - e.g. comparisons will be slower because they aren't indexed.

MySQL 5.1.41 leading zero is deleted

I have a MySQL database where I want to store phone numbers among other things.
The fieldtype is INT(10)
When I try to insert a number starting with a 0, like 0504042858 it's stored like 504042858. This only happens with phone numbers with leading zeros. When the number start with any other number, it's stored correctly.
What am I doing wrong?
You should probably store phone numbers as a varchar. Phone numbers are only numeric by accident.
You may also be interested in checking out the following Stack Overflow posts:
What datatype should be used for storing phone numbers in SQL Server 2005?
Common MySQL fields and their appropriate data types
You can give length INT(11) with attribute value UNSIGNED_ZEROFILL. it will fill all 11 digits and if any digit length is less than 11, it will add zero itself before the value.
This might solve your problem.
it is removing the leading zero because mathematically they are the same and removing the leading zero is a quick storage optimization. In addition it also makes the numbers easier to read imagine a number padded with several leading zeros in a column of several hundred numbers.
I agree with Daniel change your column to a varchar.