Column changing numeric values to scientific notation by default - sql

In sql server, I am trying to select insert data from one table into another. The code reads as:
Insert into TABLE2 (
Id, document_id
) select id, document_id from TABLE1
These two tables are basically identical. The document id field is in nvarchar50 since we will occasionally get values with a letter in them.
How can i get these to insert as numeric values, instead of scientific notation?
Thank you!

I assume the columns in table1 are of some varchar variant and hold the numbers in scientific notation. You can try to convert them to real (and if necessary to some other numeric or varchar variant form there).
INSERT INTO table2
(id,
document_id)
SELECT convert(real, id),
convert(real, document_id)
FROM table1;

This seems to be an Excel issue, where Excel treats the document id as a number instead as a text. The number formatting of Excel can potentially destroy information, as the same number can be represented in different ways. E.g. what was the original format of 500.2? Was it 0500.200, 000000000500.20 or something else? Also, Excel might even drop decimals, e.g. "5023423423423450" is displayed as "5.02342E+15". There is no way to restore this information in SQL.
You must handle this in Excel by either
entering the document id with a leading apostrophe (') to tell Excel not to interpret it in some way
or by
formatting the document fields as Text before entering the document id.

Related

Is there any option to store a number as text with a specific format (comma as thousands separator and period as decimal separator) in Snowlake?

Due to project requirements I need to store a number as text, since depending on a column I round it to some decimal places or others, and with a specific format: comma as thousands separator and period as decimal separator.
If for example I had to round to two digits and I have this 12500.987589 I would need to get this another 12,500.98.
The only solution I have found in Snowflake is something similar to this:
SELECT
TO_VARCHAR(TO_NUMBER(TO_VARCHAR(ROUND(12500.987589 ,2)),'9,999,999.99',38,2))
FROM DUAL;
Do you know any option to do this?
Thank you very much and greetings,
Why do you need to use the round function at all? You should be able to use
select to_varchar(12500.987589, '9,999,999.99')
this produces: 12,500.99
Number should be stored as number data type. Formatting of the number is a matter of application layer. Storing them as a text could lead to problems with implicit conversion, arithmetic operations, etc.
If that is not possible you could:
Create a view on top of a table that provides additional column with formatted string
CREATE VIEW v_tab
AS
SELECT *, ... AS number_formatted
FROM tab;
Create a computed column:
ALTER TABLE tab
ADD COLUMN number_formatted VARCHAR(100) AS (...);
The expression to get desired formatting:
SELECT TO_VARCHAR(TO_NUMBER(ROUND(12500.987589,2),38,2),'9,999,999.99')

Problem with the integer value in an SQL syntax: #1366 error

I was making a php website with some forms that let you make a form which let's you make an account, but for some reason when I was checking the SQL I was using inside phpmyadmin...
The SQL code it gave me an error that of
#1366 Incorrect integer value
Here is the SQL code I was checking:
INSERT INTO Users VALUES ('','$username','$password','0','empty')
The weird thing about this is the fact that the integer ID, which is in auto-increment, as I've learned it should be blank in the syntax like I've done (the ID column is the first one in my table)
When you are inserting rows into a table, the best practice is to always list the columns:
INSERT INTO Users (col1, col2, col3, col4)
VALUES ('', '$username', '$password', '0', 'empty');
I don't know what your column names are, so you have to fill them in.
If you are writing application code, another best practice is to use proper parameters rather than munging a query string with values.

Check if contents of TEXT column are purely numeric

I've got an Sqlite DB where the data looks purely numeric (integers) and the column is typed TEXT. I would like to type it as INTEGER if possible.
What query can check if every cell of a certain column can be successfully casted to INT?
SELECT * FROM table WHERE INT(column) != NULL
Alternatively I would like to check if the cells are numeric (don't have any letters/symbols)
SELECT * FROM table WHERE column NOT LIKE "%a-z%"
As a side note, I wanted to do this to reduce the size of the DB, but since Sqlite uses dynamic typing (per cell typing) would this have ANY effect on the size of the DB?
You have to check whether all values can be converted into an integer:
SELECT *
FROM MyTable
WHERE CAST(MyColumn AS INTEGER) IS NOT MyColumn;

Save All Results to Excel

I have run a query using Eclipse from a Sybase db. I need to eliminate duplicate entries but the results have mixed types - INT and TEXT. Sybase will not do distinct on TEXT fields. When I Save All results and paste that into Excel some of the TEXT field bleeds into the INT field columns - which makes Excel -Remove Duplicates tough to do.
I am thinking I might create an alias for my query, add a temp table, select the distinct INT column values from the alias and then query the alias again, this time including the TEXT values. Then when I export the data I save it into Word instead. It would look like this:
SELECT id, text
FROM tableA, TableB
WHERE (various joins here...)
AS stuff
CREATE TABLE #id_values
(alt_id CHAR(8) null)
INSERT INTO #id_values
(SELECT DISTINCT id
FROM stuff)
SELECT id, text
FROM stuff a
WHERE EXISTS (SELECT 1 FROM #id_values WHERE b.alt_id = a.id )
If there was a way to format the data better in Excel I would not have to do all this manipulation on the db side.I have tried different formats in the Excel import dialog..import as tab-delimited, space-delimited with the same end result.
Additional information: I converted the TEXT to VARCHAR but I now need a new column which has up to 5 entries per id sometimes. ID -> TYPE is 1-many? The distinct worked on the original list but now I need to figure out how to show all the new column values in one row with each id. The new column is CHAR(4).
Now my original select looks like this:
SELECT DISTINCT id, CONVERT(VARCHAR(8192), text), type_cd
FROM TableA, TableB
...etc
And I get multiple rows again for each type_cd attached to an id. I also realized I don't think I need the 'b.' alias in front of *alt_id*.
Also, regardless of how I format the query (TEXT or VARCHAR), Excel continues to bleed the text into the id rows. Maybe this is not a sql problem but rather with Excel, or maybe Eclipse.
You are limited in how much data you can past into an Excel cell anyway, so convert your text to a varchar:
SELECT distinct id, cast(text as varchar(255)) as text
FROM tableA, TableB
WHERE (various joins here...)
I'm using 255, because that is the default on what Excel shows. You can have longer values in Excel cells, but this may be sufficient for your purposes. If not, just make the value bigger.
Also, as a comment, you should be using the proper syntax for joins, which uses the "on" clause (or "cross join" in place of a comma).

Insert Comma Separated Values to SQL

I would like to take a list that I have in a text file, of values which are separated by commas:
Example - 1,2,3,4,5,6,7,8,9,0
then put the values in a database table(I'm going to use this table for auto-complete with jQuery).
I would have done an array for the auto-complete but I have something like 1000 values so I think its better to pull from SQL(am i right?)
Try to explain it to me slowly cause I'm a novice and this is so confusing :)
If those are 1000 constant values (like countries), put them in array.
If they are fairly dynamic, put them in a table
Assuming the table is called T1 and has one field F1, you need to transform the string
1,2,3,4,5,6,7,8....N
to
INSERT INTO T1
VALUES (1),(2),(3),(4),(5),(6),(7),(8)......(N);