SQL error trying to convert varchar to decimal - sql

I'm trying to figure out why I'm getting the following error. Why is it trying to convert to an int when I'm trying to tell it to convert to a decimal?
Conversion failed when converting the varchar value '938.00' to data type int.
Here is the business end. If I filter out the period, it does work but I need that period for when I import the data into Excel.
convert(decimal(9,2),'"+$WorkSheet.Range('D13').Text.replace("'","").replace(" ","").replace("$","").replace(",","").replace("-","")+"'+0)

Presumably because you are using + 0, which means that + is treated as addition rather than concatenation. I think simple solution would be replace:
replace('convert(decimal(9,2), #val)',
'#val',
$WorkSheet.Range('D13').Text.replace("'", "").replace(" ", "").replace("$", "").replace(",", "").replace("-", "")
)

This is what I came up with. It is gnarly looking, but seems to work. Testing will continue. Thanks again.
convert(decimal(9,2), ISNULL(NULLIF('"+$WorkSheet.Range('D14').Text.replace("'","").replace(" ","").replace("$","").replace(",","").replace("-","")+"', ''),'0')),

Related

The best way to parse str to float in pandas

I got a money values columns erroneously displayed as string:
money value as string
I've tried to parse that column applying usual methods, unsuccessfully:
astype(float) and pd.to_numeric(df.col, errors=['coerce'])
Finally, I can only win if I use string manipulation techniques. As you see, too verbose:
apply split casting
So, what's happened here? There's no graceful way to solve this parsing?
ps: using pd.read_csv(path, dtype={'receita': float} I also got wrong

Sybase STR function changes zeroes to nines

I'm converting the contents of a numeric column to text. The column's properties are NUMERIC(29,14). This is the SELECT statement I am running
SELECT TRIM(STR(<my_column>, 44, 14))
FROM <my_table>;
The conversion to String is transforming the trailing zeroes to nines. So that 295.737462 is being converted to a String '295.73746199999999' when it should be '295.73746200000000'.
What is happening here?
Thank you Hans, that link answered my question
I was also able to get around the issue by simply using CAST(<my_column> AS VARCHAR)

how to convert this String to Decimal

i have this String '5666,232343' and i want to convert it to Decimal, i use cast('5666,232343' as decimal(7,5)) but it returns NULL value.
Do you know why it doesn't work with CAST
Zorkolot is right. The current precision and scale that you've used is not sufficient for the value you've provided.
If you're using SQL Server 2012 or higher and you want to keep the comma in the value, then you can use the TRY_PARSE function and set a culture. It will return NULL if it encounters an error instead of not completing the statement and returning red text. This also allows you to add basic error handling to the statement, if you wanted, by getting failed conversions to return the value of zero. For example:
This is your original query (which is currently erroring) with my error handling fix:
select coalesce(try_parse('5666,232343' as decimal(7,5) using 'en-GB'),'0') as [DecimalValue]
This is the same thing as above but I've amended the decimal precision and scale so that the value is successfully converted:
select coalesce(try_parse('5666,232343' as decimal(16,6) using 'en-GB'),'0') as [DecimalValue]
This should prevent you having to perform a REPLACE either manually or by using the SQL function.
You need to cast to a decimal that can hold the value of 5666.232343.
DECIMAL(7,5) allows numbers in this format: ##.#####. The biggest number you can have then is 99.99999. You also need to take the comma out and replace it with a period:
SELECT CAST('5666.232343' as decimal(16,6)) AS [DecimalValue]
The problem is probably the comma. In some databases, some of the functions are not as internationally-sensitive as (I think) they should be. So try:
cast(replace('5666,232343', ',', '.') as decimal(7, 5))

Data output syntax in Webi XI3.1

I am trying to duplicate a detail object from Desktop to its Webi equivalent.
I am familiar with the syntax differences, i.e. using semicolons instead of commas and using [] rather than <> to enclose references to other dimensions/measures/detail objects.
Given the working formula from the Deski report:
=ToDate(7+"/1/"+<Current FY>-1 ,"mm/dd/yyyy")
I tried to convert this using the syntax I know from Webi:
=ToDate(7+"/1/"+[Current FY]-1 ; "mm/dd/yyyy")
I faced the error message
The expression or sub-expression at position 8 in the '-' function uses an invalid data type
I am guessing this has something to do with trying to convert a date datatype into an integer in order to subtract "1." However, I do not know what kind of function this requires.
Thanks in advance!

Why does ISNUMERIC('.') return 1?

Recently I was working with ISNUMERIC in SQL Server, when I encountered a problem, which led to finding this snippet of code.
SELECT ISNUMERIC('.')
This returns 1, as in true, shouldn't this return 0 as in false?
See IsNumeric() Broken? Only up to a point.
SELECT CAST('.' AS MONEY)
returns 0.00 (though the cast fails for int and float)
ISNUMERIC just checks that the value can be cast to any one of the numeric datatypes which is generally useless. Usually you want to know whether it can be cast to a specific type.
Additionally it doesn't even seem to do that task correctly for all possible inputs.. ISNUMERIC(' ') returns 0 despite casting successfully to both int and money. Conversely ISNUMERIC(N'8') returns 1 but does not cast successfully to anything that I tried.
Some useful helper functions for that are here IsNumeric, IsInt, IsNumber.
SQL Server 2012 introduced TRY_PARSE and TRY_CONVERT that help with this greatly.
Because "." is used in a decimal number !
see here
isnumeric for '-' & '.' Why isnumeric('-') & isnumeric('.') returning
1?
Answer: Because "-" means negative and "." is used in a decimal
number. I have no clue why they named it ISNUMERIC though. They
should have named it, ISNUMBERRELATED.
I think it also interprets a number of other non-numeric fields as numeric, there's further info here -
http://classicasp.aspfaq.com/general/what-is-wrong-with-isnumeric.html