ISNUMERIC() does not work for decimals - sql-server-2008-r2

I am using the SQL Server 2008 R2 function ISNUMERIC() to determine if a string is a decimal or not.
It is returning '1' when the decimal as a comma or comma and period. I can fix for those issues but this query is part of a SSRS Report that will be distributed among many users and I cannot correct of all problems.
What can I use instead of ISNUMERIC() to determine if the string is a correctly formed decimal? I cannot use TRY_PARSE() because that is available in SQL SERVER 2012 and I am on 2008 R2.
UPDATE
I want to replace ISNUMERIC() with PATINDEX() to identify decimals.
Both of these examples return 0
SELECT PATINDEX('9,0', '%[0-9]%.%[0-9]%')
SELECT PATINDEX('9.0', '%[0-9]%.%[0-9]%')
What am I doing wrong?

Your parameter values for PATINDEX are reverse. This should work for you...
SELECT PATINDEX('%[0-9]%,%[0-9]%', '9,0')
SELECT PATINDEX('%[0-9]%.%[0-9]%', '9.0')
Noel

Related

Oracle to_char numeric masking to postgres

I'm porting a procedure from Oracle to Postgres.
In select of a query, I have TO_CHAR(v_numeric, '990.000')
It seems, the same TO_CHAR(v_numeric, '990.000') works in Postgres with same result.
Can someone please explain what the '990.000' in the query does?
TO_CHAR(123.4, '990.000') returns 123.400 in both Oracle and Postgres. Whereas TO_CHAR(1234.400, '990.000') returns ######## in Oracle and ###.### in Postgres. Does this ######## and ###.### hold the same numeric value which is inputted?
to_char is a function to format a number as string for output. The PostgreSQL function is there expressly for Oracle compatibility, but it is not totally compatible, as you see.
The format 990.000 means that there will be one to three digits before the decimal point and three digits after it. 9 means that a value of 0 in that position will result in a blank rather than a 0.
The # characters signify that the number cannot be represented in that format. The reason is that there are more than three digits before the decimal point.
The resulting string does not "hold" a number, it is the rendering of a number as a string. It doesn't hold anything but the characters it consists of.

What's the easiest way to always round a number down in SQL Server 2005?

I can't seem to find this anywhere, but what is the correct way to always round a number down, to a specific decimal precision, using SQL Server 2005?
Will I need to write my own function or is there already a function that does this?
I do know that SQL Server 2008 R2 has a ROUNDDOWN function, and it does exactly what I need. Does a similar function exist in 2005?
Rounding down to a specific decimal place is the same as truncating to a decimal place... and you can use round() to do this:
select round(123.456789, 4, 1)
Returns:
123.456700
Try FLOOR. Google "sql server 2005 floor"
http://msdn.microsoft.com/en-us/library/ms178531(v=sql.90).aspx
Have you tried FLOOR() ? (30 char minimum)

Crystal Reports 10: Is it possible to call a custom function from a command

I have created 4 custom functions in Crystal Reports 10. These take as input a date-time value from a DB record, and determine whether that date-time falls inside the current Fiscal Year or the previous Fiscal Year.
These calculations were previously executed as stored procedures on an SQL Server but we are moving to another ticketing application (hosted at a vendor site) and do not currently have DB to do these calculations on.
Here is an example of one of these functions:
// Function name: isCloseDateWithinCurrentFY
Function (DateTimeVar closeTime)
// This replaces dbo.fn_FiscalYear
// Determine if the incident close date falls inside the current Fiscal Year
DateTimeVar startCurrentFiscalYear;
NumberVar currentMonth;
StringVar returnVal;
currentMonth := Month(CurrentDate);
If currentMonth >= 2 Then
startCurrentFiscalYear := Date(Year(CurrentDate), 2, 1)
Else
startCurrentFiscalYear := Date(Year(CurrentDate)-1, 2, 1);
If (closeTime >= startCurrentFiscalYear) Then
"T"
Else
"F";
When these calculations were on the SQL Server, they were utilized from a Crystal Report SQL command
SELECT
category,
subcategory,
close_time,
tyCount
FROM (
SELECT
category=ISNULL(UPPER(category),'*Unspecified'),
subcategory=ISNULL(UPPER(subcategory),'*Unspecified'),
tyCount=SUM(CASE WHEN dbo.fn_FiscalYear(close_time)='T' THEN 1 ELSE 0 END)
FROM
incident_tickets
GROUP BY
UPPER(category),
UPPER(subcategory)
) tickets
WHERE
tycmCount>0
ORDER BY
category,
subcategory
In my case I would like to replace the call to dbo.fn_FiscalYear with a call to my custom function isCloseDateWithinCurrentFY.
But is it possible to call a custom function from an SQL command?
Or is there some other way to restrict the returned records based on a calculation made on Crystal Report side?
TIA
No, you cannot use any sort of Crystal code in a SQL command since they're two completely different beasts. The SQL Command is basically just the query that Crystal will send straight to the database in order to get the records to process, after which point those records will be available to use with Crystal code. The SQL server has no way to interpret the Crystal function and Crystal has no way of translating it into SQL.
The easiest way to do this would probably be to just replace dbo.fn_FiscalYear with more SQL hard-coded directly into the query. More specifically, translate isCloseDateWithinCurrentFY into the equivalent SQL.

SSRS 2008 passing multiple parameters Oracle 10g backend

after several years of using Cognos, we are in process of testing conversion of Cognos Reports (8.3) to SSRS 2008 reports. we use Oracle database version 10g. in many of our reports we are converting we pass multiple values in parameters, however i cannot get this working in SSRS pointing to the Oracle datasource.
i have created parameter and set it to allow multiple values. these columns are integer types. the SQL filter is set as follows for example, where vendor_id IN (:Vendor_id). yet when I test the SQL, i get errors. i enter parameter values as comma-seperated for example, 102, 105, 107. errors as follows.
ORA-01722: invalid number
i've tried wrapping value in single, double quotes with same result. is there a different format to meet oracle syntax requirement? does multiple values only work for SQL server databases?
thanks in advance.
joe
As pointed out in this post, multi value parameters are concatenated and used as follows:
Select * from Table WHERE column1 in (:CommaSeparatedListOfValues)
http://consultingblogs.emc.com/stevewright/archive/2009/08/14/14763.aspx
So Vendor_id has to be Varchar2. I guess you have the data type of Vendor_id as integer?.

Converting / Casting an nVarChar with Comma Separator to Decimal

I am supporting an ETL process that transforms flat-file inputs into a SqlServer database table. The code is almost 100% T-SQL and runs inside the DB. I do not own the code and cannot change the workflow. I can only help configure the "translation" SQL that takes the file data and converts it to table data (more on this later).
Now that the disclaimers are out of the way...
One of our file providers recently changed how they represent a monetary amount from '12345.67' to '12,345.67'. Our SQL that transforms the value looks like SELECT FLOOR( CAST([inputValue] AS DECIMAL(24,10))) and no longer works. I.e., the comma breaks the cast.
Given that I have to store the final value as Decimal (24,10) datatype (yes, I realize the FLOOR wipes out all post-decimal-point precision - the designer was not in sync with the customer), what can I do to cast this string efficiently?'
Thank you for your ideas.
try using REPLACE (Transact-SQL):
SELECT REPLACE('12,345.67',',','')
OUTPUT:
12345.67
so it would be:
SELECT FLOOR( CAST(REPLACE([input value],',','') AS DECIMAL(24,10)))
This works for me:
DECLARE #foo NVARCHAR(100)
SET #foo='12,345.67'
SELECT FLOOR(CAST(REPLACE(#foo,',','') AS DECIMAL(24,10)))
This is probably only valid for collations/culture where the comma is not the decimal separator (ie: Spanish)
While not necessarily the best approach for my situation, I wanted to leave a potential solution for future use that we uncovered while researching this problem.
It appears that the SqlServer datatype MONEY can be used as a direct cast for strings with a comma separating the non-decimal portion. So, where SELECT CAST('12,345.56' AS DECIMAL(24,10)) fails, SELECT CAST('12,345.56' AS MONEY) will succeed.
One caveat is that the MONEY datatype has a precision of 4 decimal places and would require an explicit cast to get it to DECIMAL, should you need it.
SELECT FLOOR (CAST(REPLACE([inputValue], ',', '') AS DECIMAL(24,10)))