Insert decimal with comma is interpreted as two values - sql

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.

Related

Conversion of decimal separator - DB2 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

Can't remove empty space on SQL table using replace function

I received a csv file with a column called "Amount", which should be of a MONEY type inside my table.
First step I took was loading the csv file as is. So my table uses a string type for that Amount column, just because I know it will not be formatted as money on the source. Due to some spaces on that column, I can't convert from NVARCHAR to MONEY.
Here's the initial table structure:
CREATE TABLE #TestReplace (
Amount NVARCHAR(100)
)
Here's an example to what the client inserted as value for the column:
INSERT INTO #TestReplace VALUES('2 103.74')
Because there is a space into that string, I need to remove it so I can convert it to the MONEY type.
However, if I try the REPLACE SQL function, nothing happens. It's like the value does not change
SELECT REPLACE(Amount, ' ','') FROM #TestReplace
Amount after the replace command is still: 2 103.74
Am I missing something that does not catch the space after the number 2? Is there a better way to remove that space and convert from NVARCHAR to MONEY?
Appreciate all the help!
You have a character that is not a space but looks like one. If you are using 8-bit ASCII characters, you can determine what the value is using:
select ascii(substring(amount, 2, 1))
If this is an nvarchar() (as in your example):
select unicode(substring(amount, 2, 1))
Once you know what the character is, you can replace it.
To add to Gordon's answer, once you know the integer ASCII/Unicode value, you can use that in the replace function with the CHAR() and NCHAR() functions like so:
--For ASCII:
REPLACE(Amount, CHAR( /*int value*/ ), '')
--For Unicode:
REPLACE(Amount, NCHAR( /*int value*/ ), '')

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

How do I correctly implement quotename in SQL Server?

Background Info:
I have a stored procedure that is populating an SSRS report. The SSRS report is ran and exported as a CSV. It is then opened as textfile and ran through a 3 party vendor application.
The output of the text book should look like this:
lid, status, i_flag,Count, pDate, iDate
62558633,"Text Value","08/16",11,"08/16","08/16"
78013526,"Text Value","",,"08/16""08/16"
My results look like this:
lid, status, i_flag,Count,pDate,iDate
19007442,"'Dir,MgmtII'",'',2,'','02/16'
17343623,'Text','',0,'11/15','02/16'
Now the code that I'm using is:
SELECT
quotename(isnull(i_flag,''''), '''') as i_flag,
isnull(lid, 0) as lid,
quotename(isnull(status,''''), '''') as status,
isnull(Count, 0) as Count,
quotename(isnull(p_Date,''''), '''') as p_Date,
quotename(isnull(i_Date,''''), '''') as i_Date
FROM
#Table
Any ideas on how I can fix this. Been stumped on this for a bit. Thanks.
If I'm understanding your question correctly (which I'm quite possibly not), I think you want:
SELECT
QUOTENAME(ISNULL(i_flag,''), '"') AS i_flag,
ISNULL(lid, 0) AS lid,
QUOTENAME(ISNULL([status],''), '"') AS [status],
ISNULL([Count], 0) AS [Count],
QUOTENAME(ISNULL(p_Date,''), '"') AS p_Date,
QUOTENAME(ISNULL(i_Date,''), '"') AS i_Date
FROM
#Table
It sounds like you have some values in fields which you wish to wrap in double quotes " for the purpose of exporting to CSV, plus in some cases the values in these fields might be NULL.
My suggestion above handles this by first using ISNULL to replace any NULL values with an empty string and then using QUOTENAME to wrap the resultant value in double quotes.
The crucial differences to your posted code are:
When using ISNULL I replace the NULL with an empty string '' instead of a string containing a single quote character '''' (two consecutive single quotes within a string represent an escaped literal single quote character)
When using QUOTENAME to wrap the values in double quotes, I specify a string containing a double quote '"' in the second parameter, instead of a string containing a single quote character ''''.
I hope that helps you - if you're still having problems, perhaps you could provide some sample rows from your #Table temp table and the output you're expecting from the query so people can help you further.
As an aside, it's not good practice to use SQL reserved keywords like status or count as column names or aliases, but if you must, I'd recommend enclosing them in brackets (i.e. [status]) for readability (especially in SSMS or any IDE with SQL syntac highlighting or colour-coding) as I have done above.

In db2 how to select a column in integer having aligned it to right with leading spaces?

I need to extract a report from some tables using db2 having pgm(dnatiaul).
Using a query i want to get the below output with first char to be spaces.
ex: integer(16)
54457750
49457750
o/p: char(int(16))
54457750
49457750
As i am trying to convert it to char it is aligning to left.
I tried Lpad which gives me **extra length i.e (18) + '.' also
Help me
LPAD is the right choice, but you would need to specify how long the result string needs to be. This can be done using CAST. Here I cast the result to 10 characters.
db2 "select cast(lpad(123422,10,' ') as char(10)) as testme from sysibm.sysdummy1"
TESTME
----------
123422
1 record(s) selected.
DIGITS(integer_column_name) will give you a character string of CHAR(10) with the numeric value right-justified and left-filled with zeroes. Thus, an integer column containing 543210 will become a character string containing 0000543210.
Likewise, DIGITS(small_integer_column_name) will give you a character string of CHAR(5)