Conversion of decimal separator - DB2 SQL - sql

I want to convert a double value column in DB2 SQL with ',' comma decimal separator to '.' dot decimal separator. My local settings are German. I can't change the DB settings since many applications are involved.
Eg : column value =0,81234 I want it to be displayed as 0.81234.
SELECT CAST(CAST(COLUMNNAME AS DECIMAL(8,2)) AS VARCHAR(25))
I tried converting it to decimal(8,2) first and then to varchar. This results in 0.81 (with '.' as expected). But I need all the numbers right of the decimal separator like 0.81234. So, I tried with decimal(7,6) which results in SQL0413N OVERFLOW OCCURRED DURING NUMBER DATA TYPE CONVERSION error.
Is there any other way to display the decimal separator as '.'?

The separator is not actually stored in the DB for numeric columns.
Configure whatever tool you are using to look at the data to use your separator of choice.
Optionally you can run
set option decmpt = *PERIOD;
before running your select.

if you use JDBC you can modify it by adjusting your Connection string. Just add this
:decimalSeparator=1;
for point as decimal separator or 2 for comma as decimal separator

Related

Replace comma (,) with dot (.) in SQL and Float Datatype

I created a Table with numeric values like 9,35 or 10,5 in it. The Datatype is float. The table looks like this in the short version:
Currency | Euro | 2018 |
USD | 1 | 9,35 |
Now I want to update my table and replace all komma (,) with a dot (.)
I tried it with this code:
update dbo.[Table]
set [2018] = replace([2018], ',','.')
It says that 24 Rows are affected but when I Update my table it changed nothing.
If I use this code:
select replace ([2018],',','.') from dbo.[Table]
Then it works fine but it don't update my table...
Numeric columns do not contain a separator - they use a separator when the data is displayed. The SQL server was probably set up with a culture that uses commas instead of decimals when it displays data. The coma is not stored with the value.
But, all you need to do is specify the format when you display the data, meaning in a report, form, app, whatever. That's where you specify how to format the values.
I would not format the data in the actual SQL query (e.g. converting the data to a string and specifying the format), since it makes it harder to do aggregations and other numeric operations on the client, and takes up more space in memory (which may not be a problem until you get to a massive scale).

When Select replacing comma with dot is truncating values

I'm trying to convert a varchar column where decimal separator is ','.
The workaround found was replace comma to dot but SQL Server is automatically rounding it up. See below example:
2018-10-08 -8679.95 -8679,94711560794
select DATA, REPLACE([PL]*1,',','.') , PL from TB_BOOK
Please, anyone know How can a get the value with all decimals?
Your approach to replace , with . is ok.
However on top of it you need to explicitly CAST the string to a number with enough decimals (FLOAT, DECIMAL(p, s), ...).
SELECT DATA, CAST(REPLACE([PL],',','.') AS FLOAT), PL from TB_BOOK

Removing a trailing period from improperly joined Teradata column

I have a Teradata table that I've inherited that was not formatted in a great manner.
ID
123456789.
234567890.
I've tried:
TRIM(new_card_srgt_id (FORMAT 'Z(17)9'))
but my version of Teradata gave a funny error:
Format string has combination of numeric, character, and GRAPHIC values.
Any suggestions welcomed.
UPDATE: The suggestion to use TRIM(trailing '.' from ID) results in a numeric overflow when I went to cast it. Any other way to fix it.
When you load data from a decimal field into a character field without specifying the format, teradata does the type conversion. It inherently adds '.' to the trailing value.
If the ID field is char and it already contains '.' and you want to extract it as decimal without the '.' then the syntax is as follows
SELECT cast(id as decimal(15,0)) form table;
If the ID field is decimal and you want to extract it as character without the '.' then the syntax is as follows
SELECT TRIM(ID (FORMAT 'ZZZZZZZZZZZZZ9')) from table;
Solved it with:
cast(TRIM(trailing '.' from ID) as decimal(18,0))

Format the double precision value in Postgresql

I want to get the value from postgresql with formating. The formatting is user option like they want comma separator or not and with decimal place is 2,3 or 4 like.
So now i wrote query like this.
Select to_char(rate,'FM999.00') as BasicSaleRate from table1
Its return ans 220.00. How to write the query with comma separator value like this '#,0.00'. That means the value more than thousands return with comma separator or not.
Am using postgresql 9.3
Thank you
See this reference http://www.postgresql.org/docs/9.3/static/functions-formatting.html
to_char(rate,'FM999,999.00')

Insert decimal with comma is interpreted as two values

I'm creating an ssrs report and creating a mockdata sql script.
In the script I want to insert a decimal value like so:
declare #tempTable table(
aString varchar(50),
aDecimal decimal(5,2)
)
insert into #tempTable
values ("somestring", 1,23)
The values after "someString" are actually one decimal value with a comma as decimal separator. SQL interprets this as separate values though and hence throws an error about too many values for the number of columns. How to go about this?
UPDATE
To clarify: I'm in a region where '.' is a thousand separator and ',' is a decimal separator.
Putting it in between quotes doesn't work either. It gives me the Error converting data type varchar to numeric message
To specify a number as a constant in a SQL script, you should follow SQL syntax rules, not locale settings. And the syntax dictates that you use a . as a decimal separator. As for thousand separators, they are not used at all.
Note that this is strictly about coding your data in a script, not displaying them. Displaying is where your locale settings do matter, and if they are in order you should get your output formatted accordingly: decimal separators as commas, thousand separators as periods.
Commas are used to separate values when inserting SQL. I don't know what type of field this is, but if it is a INT or DECIMAL it won't work.
To get it working, put quotes around them, like so:
insert into #tempTable
values ("somestring", '1,23')
The best would be to:
If it's a decimal, use . as comma and the manipulate the string afterwards.
If it's a 1000-sepeartor, remove it, you can do this when displaying it
Actually add parameter solved same issue for me.
cmd.CommandText = "INSERT dbo.tickets (places,date,client_id,price,trip_id) VALUES (" + PlaceQuontity.Value + ",#date " + "," + id + "," + "#price" + "," + TripId + ")";
cmd.Parameters.Add("#date", SqlDbType.Date).Value = TicketDate.Value.Date;
cmd.Parameters.Add("#price", SqlDbType.Decimal).Value = FinalPrice;
Where cmd is my sql comand and finalprice is decimal.