How to insert french number format in oracle SQL table - sql

I am facing the issue to insert the french number in a number field of oracle.I am using SQL Developer IDE. When i insert the number(say 3,4) its says invalid number. Specifically,
I don't want to replace the value 3,4 to 3.4.
I tried with changing the NLS Setting also (using command
Alter session set NLS_NUMERIC_CHARACTERS=',';
If I use this command I am able to insert directly in editor but insert command is not working due to comma, Oracle assume that its another value.
Any help would be appreciated.

You can't and you don't need to.
The number format for SQL literals requires the . for the decimal separator.
In the column itself the decimal separator isn't stored at all. You just need to change the display format of the number. This is ideally done in your application, not on SQL level. But if you require this in the SQL output, use to_char() to format your numbers:
select to_char(your_number_column, '9999D99')
from your_table;
The D in the format mask will be replaced with the decimal separator defined by the current session's NLS settings.
A dot . and , are returned literally:
select to_char(your_number_column, '9999,99')
from your_table;
More details on the to_char() format mask can be found in the manual: https://docs.oracle.com/cd/E11882_01/server.112/e41084/sql_elements004.htm#BABIGFBA

You application/ide may be performing an implicit conversion of character data into number, which uses the session default nls_numeric_characters.
You can force the insert command to perform an explicit conversion as follows using to_number:
SQL> alter session set nls_language = ENGLISH;
Session altered
SQL> alter session set nls_numeric_characters = ',.';
Session altered
SQL> create table t1 (num_col number);
Table created
SQL> insert into t1 (num_col) values ('3.4');
insert into t1 (num_col) values ('3.4')
ORA-01722: invalid number
SQL> -- this is equivalent to:
SQL> insert into t1 (num_col) values (to_number('12345,678', '999999D999', 'NLS_NUMERIC_CHARACTERS=.,'));
insert into t1 (num_col) values (to_number('12345,678', '999999D999', 'NLS_NUMERIC_CHARACTERS=.,'))
ORA-01722: invalid number
SQL> -- now converting explictly
SQL> insert into t1 (num_col) values (to_number('12.345,678', '999999D999', 'NLS_NUMERIC_CHARACTERS=,.'));
1 row inserted
SQL> insert into t1 (num_col) values (to_number('12345,678', '999999D999', 'NLS_NUMERIC_CHARACTERS=,.'));
1 row inserted
in NLS_NUMERIC_CHARACTERS the first character is the decimal separator and the second one is the thousands grouping marker.
The docs have more info:
https://docs.oracle.com/cd/B28359_01/olap.111/b28126/dml_functions_2117.htm

Related

Why does my CAST to a float give an "invalid number" error in Oracle?

I have the following example:
create table test_1(c1 float);
insert into test_1 values(cast('000000000000001425.6' as float));
When I want to insert the value, I have
ORA-01722: invalid number
How can I pass over this error, please?
Most likely reason is that your session's nls_numeric_characters is set to ,. meaning that comma is the decimal separator and dot is the thousands separator.
One easy way around that, if you really must accept varchar2 as input, is to apply translate first, to convert all dots to commas and vice versa.
In the illustration below, first I use an alter session statement to make my session behave like yours. Then I show what you are doing now, and at the end I show how you can fix it - notice the translate function:
alter session set nls_numeric_characters = ',.';
Session altered.
create table test_1(c1 float);
Table TEST_1 created.
insert into test_1 values(cast('000000000000001425.6' as float));
Error starting at line : 10 in command -
insert into test_1 values(cast('000000000000001425.6' as float))
Error report -
ORA-01722: invalid number
insert into test_1 values(cast(translate('000000000000001425.6', ',.', '.,')
as float));
1 row inserted.

Netezza timestamp data type for column

I'm trying to create a table with one column which is supposed to have a timestamp as the datatype. An example of the value I want to insert into it is this:
2018-12-01T00:00:00Z.
How can I accomplish this? Right now I have it stored as a varchar so when I make joins with this table, in order to convert to timestamp, I need to do this:
to_timestamp(dateColumn, 'YYYY-MM-DDT00:00:00Z)
I'd like to avoid doing this every time I do joins so is there an option maybe when I insert to have this value 2018-12-01T00:00:00Z stored as a Date? It would be fine if the value when inserted into the table appeared as this: 2018-12-01 00:00:00.
EDIT:
Here is my insert statement:
INSERT INTO TEST
SELECT * FROM
EXTERNAL 'C:\...\Desktop\testTable.csv'
USING
(
DELIMITER ','
DATESTYLE 'YMD'
Y2BASE 2000
REMOTESOURCE 'ODBC'
SKIPROWS 1
MAXERRORS 1
LOGDIR 'C:\...\Desktop'
ENCODING 'internal'
DATEDELIM '-'
);
I get the error for the date column: Expected field delimiter or end of record, "2018-12-01" [T]

SQL - How to change data type float to nvarchar and remove scientific notation

How do I change the data type float to nvarchar in order to remove the scientific notation and still keep precision? Consider the following:
CREATE TABLE ConversionDataType (ColumnData FLOAT);
INSERT INTO ConversionDataType VALUES (25566685456126);
INSERT INTO ConversionDataType VALUES (12345545546845);
INSERT INTO ConversionDataType VALUES (12345545545257);
When I do a simple read I get the following data, as expected:
select * from ConversionDataType
ColumnData
------------------------------------
25566685456126
12345545546845
12345545545257
Now when I try update the data type to an nvarchar, it gets stored in scientific notation which is something I don't want:
update ConversionDataType
set ColumnData = CAST(ColumnData AS NVARCHAR)
The result set is as follows:
25566700000000
12345500000000
12345500000000
It replaces some digits and adds zeros after the 6th index. How can I go about this? I had a look at the Convert function but that is only for converting date time data types.
Being valid what others said in comment, if you just want to convert float to varchar without scientific notation, you need to convert to numeric. You can try this:
SELECT CAST(CAST(CAST(25566685456126291 AS FLOAT) AS NUMERIC) AS NVARCHAR)
Output:
C1
------------------------------
25566685456126292
Whereas
SELECT CAST(CAST(25566685456126291 AS FLOAT) AS NVARCHAR) AS C1
gives:
C1
------------------------------
2.55667e+016
If you need to change datatype, I think you should add a new column, update it and (if you want) delete the old column and rename the new column at the end.
CREATE TABLE TEST1 (C1 FLOAT)
INSERT INTO TEST1 VALUES (25566685456126291);
ALTER TABLE TEST1 ADD C2 VARCHAR(18)
UPDATE TEST1 SET C2=CAST(CAST(C1 AS NUMERIC) AS VARCHAR)
SELECT * FROM TEST1
Output:
C1 C2
---------------------- ------------------
2.55666854561263E+16 25566685456126292
FLOAT was a very bad decision as this is not a precise data type. If you wanted to store the phone numbers as numbers, you'd have to go for DECIMAL instead.
But you'll have to use NVARCHAR instead. And this is the only reasonable design, as phone numbers can have leading zeros or start with a plus sign. So the first thing is to introduce an NVARCHAR column:
ALTER TABLE ConversionDataType ADD ColumnDataNew NVARCHAR(30);
The function to convert a number into a string in SQL Server is FORMAT. It lets you state the format you want to use for the conversion, which is integer in your case (a simple '0'):
update ConversionDataType set ColumnDataNew = format(ColumnData, '0');
At last remove the old column and then rename the new one with the same name. SQL Server lacks an ALTER TABLE syntax to rename a column, so we must call sp_RENAME instead (at least this is what I have read on the Internet; here is a link to the docs: https://msdn.microsoft.com/de-de/library/ms188351.aspx).
ALTER TABLE ConversionDataType DROP COLUMN ColumnData;
EXEC sp_RENAME 'ConversionDataType.ColumnDataNew', 'ColumnData', 'COLUMN';
Here you can see the results: http://rextester.com/GLLB27702
SELECT CONVERT(NVARCHAR(250), StudentID) FROM TableA
StudentID is your Float Column of database
or Simply use
SELECT CONVERT(NVARCHAR(250), yourFloatVariable)

SQL Plus : insert into numeric values with single quotes around them gives error

A colleague of mine has created an export with INSERT INTO queries using Oracle SQL Developer. This export has put single quotes around every value, not just VARCHARs.
So let's say we have a simple table containing these columns (among others):
SOME_TEXT VARCHAR2(256 BYTE)
SOME_NUMBER NUMBER(15,2)
The export has an INSERT INTO like this:
Insert into MY_TABLE (SOME_TEXT,SOME_NUMBER) values ('test text','123,45');
You may note two things about the decimal value ('123,45'):
It has single quotes around it
It has a comma instead of a dot (the database of my colleague is Dutch, and mine is English)
The second is easy to fix, I can insert it in Oracle SQL Developer like this:
-- dot as thousand separator, comma as decimal point
alter SESSION set NLS_NUMERIC_CHARACTERS = '.,';
Insert into MY_TABLE (SOME_TEXT,SOME_NUMBER) values ('test text','123,45');
-- All other insert queries
alter SESSION set NLS_NUMERIC_CHARACTERS = ',.';
Which works fine.
However, some of the .sql files are very big, and can't be opened in SQL Developer. So instead I want to use SQL Plus to execute the .sql files containing the insert-statements.
However, even when NLS_NUMERIC_CHARACTERS is changed, I'm currently getting the following error in SQL Plus due to the single quotes around the numeric value:
SQL> #"C:\my\path\test_insert_statement.sql"
values ('test text','123,45')
*
ERROR at line 2:
ORA-01722: invalid number
I see two possible solutions, but I don't know if either is possible:
Is there a way to allow single quotes around numeric values in SQL Plus for Oracle databases?
Or is there a way to export a database without single quotes for numeric values in Oracle SQL Developer (then I can let my colleague generate another .sql file)?
You can use single quotes around a numeric value. The error has occurred due to the decimal point character. All you need to change the decimal point character which can be done, as you have shown, as given below.
SQL> create table tbl1(
SOME_TEXT VARCHAR2(256 BYTE),
SOME_NUMBER NUMBER(15,2)
); 2 3 4
Table created.
SQL> alter SESSION set NLS_NUMERIC_CHARACTERS = ',.';
Session altered.
SQL> Insert into tbl1 (SOME_TEXT,SOME_NUMBER) values ('test text','123,45');
1 row created.
SQL> select * from tbl1;
SOME_TEXT SOME_NUMBER
------------- -----------
test text 123,45

SQLSTATE[22P02]: Invalid text representation

I'm using Postgresql and PHP 5.3.x with PDO to access the DB.
I have this the SQL query (stripped down version), with a placeholder for PDO to fill in:
INSERT INTO t_articles (a_article_id) VALUES (?) RETURNING a_id
I want a_article_id to be either a number, like 5, or else it should be the result of the subquery:
((SELECT max(a_article_id) FROM t_articles) + 1)
However, PDO says:
SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "(SELECT max(a_article_id) FROM t_articles) + 1"
And I've tried to set the subquery as the default value, but it is not allowed apparently:
ERROR: cannot use sub query in default expression
How can I insert the result of this sub query (or what can be done to achieve the same result)?
You'd have to use INSERT...SELECT for that:
insert into t_articles (a_article_id)
select max(a_article_id) + 1
from t_articles
returning id
Or if you don't need contiguous values for a_article_id, use a sequence for it:
Create a sequence, we'll call it article_id_sequence.
-- Get the current max(a_article_id)+1 to use instead of FIRST_VAL below
create sequence article_id_sequence
start FIRST_VAL
owned by t_articles.a_article_id;
Set the default value for t_articles.a_article_id to nextval('article_id_sequence').
alter table t_articles
alter column a_article_id
set default nextval('article_id_sequence');
Use the default value when inserting:
insert into t_articles (a_article_id)
values (default)
returning id;