input from form being converted to decimal on insert - ruby-on-rails

I have a db column for upc codes setup as a 'numeric' data type in postgres.
I generated scaffolding to input values into this table, but rails is converting the input in FLOAT for some reason,
eg, a 13 digit number entry 1234567890000 is converted to 1234567890000.0
from the logs:
INSERT INTO "upc_codes" ("upccode") VALUES (1234567890000.0) RETURNING "id"
Where is the data type for the SQL statement being set, or not set as the case may be?

What data type are using for this column? Try changing the column type to an integer in a migration:
change_column :upc_codes, :upc_code, :integer

max integer value for an mysql integer should be 2147483647 .
i will suppose this could cause an errors somewhere .
try to change the coulm into a bigint .
aniway ,from my experience is better to handle bignum using string in rails (in this manner you could saftly change db). you could always use to_i later in your code .
sorry for my english.

When you set the column type to NUMERIC are you specifying the precision/scale like this: NUMERIC(13,0)? (13 is precision, 0 is scale)
I submitted this again as an answer because I guess I commented when I should have answered.

Related

Determine the DateTime encoding or format

I'm writing a Delphi program that needs to interact with data stored in a SQLite database that belongs to another program. Everything ok so far, except that I'm unable to get the date/time value from the data store in a column in the SQLite database, with the data type 'datetime'.
I do know that the data type of fields is not relevant in SQLite, and that everything is stored as strings, and perhaps that is possibly the reason why I find myself in this predicament.
Below is a sample of a few rows of the data stored in the SQLite database (column 3) vs. the corresponding date value displayed in the program (column 2) that reads and writes to this SQLite database:
1 11/7/1971 621939168000000000
2 3/17/1976 623314656000000000
3 5/4/1996 629667648000000000
4 9/21/2007 633259296000000000
5 11/17/1972 622264032000000000
6 2/7/1996 629592480000000000
7 6/13/2000 630964512000000000
My requirement: Once I read the value in column 3 from my Delphi program, how do I translate it to the same date/time value as displayed in column 2? I have tried the UnixToDateTime function, but it does not result in the same value (as column 2). Can anyone help?
Thanks in advance!
The value in column 3 is the number of 100 nanoseconds since 1/1/0001. You can convert it into a Delphi TDateTime like this:
theDate := (Value/864000000000) - 693593;

adds leading zero to a column value Etext template

How to format the column so that it adds leading zero to a column value.
for example , in the etext template i want to add this logic. using which function can i do this ?
that is if job_code is 2900 then it should come as 002900. I tried adding "0000000" under the format column, but it didnt work.
You could format the column in Data Model sql so it comes to the template already formated..
Select LPAD(COLUMN_NAME, 6, '0') as ALIAS...
Here is an example of LPAD function: ERROR: Function SUBSTR requires a character expression as argument 1. and adding zeroes in front of data

Informix set string column default value to current date

When creating a table, I would like to set the default value of a char(12) field with todays date. For example, '2016-08-25'
How do I do this in Informix?
If you manage to find the documentation on the DEFAULT clause of CREATE TABLE, then you'll see that the options for what can be a default value are quite limited.
Further, there are type-based restrictions on what's allowed:
CREATE TABLE x(y CHAR(12) NOT NULL DEFAULT TODAY);
This generates the semantic error:
SQL -591: Invalid default value for column/variable (y).
That has the expanded meaning:
The specified default value is the wrong type or is too long for a column
or an SPL-routine variable.
To specify a valid default value for a column, use the DEFAULT clause in a
CREATE TABLE statement. To specify a valid default value for a variable in an
SPL routine, use the DEFAULT clause in a DEFINE statement.
Basically, you can't create a default of TODAY for a CHAR-type column.
It will be much simpler to use a DATE column (where you can apply the default of TODAY validly), and when necessary, select that value as a string. There are some minor details like locale and presentation of the data — you might find DATETIME YEAR TO DAY better than DATE because it enforces the ISO 8601-style notation 2016-08-25 for the values.

How to convert postgres column type from string to float

I have a rails app with a basic postgres db, but I realized that some of my columns are strings and it'd be best if they were floats. I'm trying to convert columns for latitude and longitude from varchar to floating-point.
I've tried this post Rails - gmaps4rails gem on postgres but I kept getting this error, ERROR: invalid input syntax for type double precision: "". I'm willing to try anything else, and I've seen solutions for ways to do it with postgres queries, but I'm uncertain of how to implement them. It's a straightforward problem; "-73.88537758790638" I want to become -73.88537758790638. I just can't seem to find a working solution or one that I understand how to implement.
Empty strings cannot be converted to a number for obvious reasons.
You have to account for that. Replace all occurrences with NULL or 0 or something compatible.
For the number of fractional digits in your example you want the data type numeric, not float - neither real (float4) nor double precision (float8). Those are lossy types and not exact enough. See:
Fetch records that are non zero after the decimal point in PostgreSQL
Try for yourself:
SELECT '-73.88537758790638'::real AS _float4
,'-73.88537758790638'::double precision AS _float8
,'-73.88537758790638'::numeric AS _numeric;
Result (up to Postgres 11):
_float4 | _float8 | _numeric
---------+-------------------+-------------------
-73.8854 | -73.8853775879064 | -73.88537758790638
db<>fiddle here
Display improved in Postgres 12 (more extra_float_digits by default):
db<>fiddle here
Numeric types in the manual.
Solution
Single SQL statement (replacing empty strings with NULL):
ALTER TABLE tbl
ALTER COLUMN col1 TYPE numeric USING NULLIF(col1, '')::numeric;
This is a really simple thing to do in rails using a native ORM approach:
change_column :restaurants, :column_name, ‘double precision USING CAST(column_name AS double precision)'

Rails column type for numbers

I'm storing numbers such as
2,000
5,000
10,000
in a database with an 'integer column'. When I display them on the app, they're showing up like
2.0
5.0
Is there a column type I can use to make them show up "as is." I tried string before but string doesn't sort well.
Integers should come out like integers. If you try to insert "2,000" internally rails is effectively doing to do a to_i on it which will be 2. (try this on irb "2,000".to_i and "2_000".to_i to see how ruby deals with integer conversions).
Now if you insert 2000 and you get 2000.0 that is not natural or expected for an integer column. I suspect there is some post processing going on on the attribute or that your database is decimal/float column.
Rails Console =>
"2,000".to_i => 2
"2_000".to_i => 2000
helper.number_to_currency(2000) => "$2,000.00"
to fix this, I entered the number in the database without the comma. Then the numbers showed up on the app like 10000.0 and I used number_to_currency to change the number to make it look like a dollar value
In Ruby, you can also enter in integers like this
10_000
5_000
2_000

Resources