Home All Groups Group Topic Archive Search About

Fractional truncation of Input Paramater in a UDF

Author
9 Aug 2006 9:50 PM
Mav
When passing a fractional/decimal value like for example 1.5, the following
UDF (User defined function) truncates the 1.5 to 1.  Why?
ALTER FUNCTION dbo.OTimCalcChk  (@X numeric(4,2))
RETURNS numeric(4,2)
AS
BEGIN
RETURN @X
END
I'm using this UDF to test SQL 2000's method of operation.  I ended up with
because I was getting unexpected results from a more complicated UDF.  The
joke of it is that if you SET @X = 1.5, then it returns the correct result. 
Pls help me!
--
Mav

Author
9 Aug 2006 10:12 PM
Chris Lim
Mav wrote:
> When passing a fractional/decimal value like for example 1.5, the following
> UDF (User defined function) truncates the 1.5 to 1.  Why?
> ALTER FUNCTION dbo.OTimCalcChk  (@X numeric(4,2))
> RETURNS numeric(4,2)
> AS
> BEGIN
> RETURN @X
> END

It works fine for me:

select dbo.OTimCalcChk(1.5)

1.50
Author
9 Aug 2006 11:17 PM
Aaron Bertrand [SQL Server MVP]
Where are you seeing this truncation?  Are you using SELECT
dbo.OTimCalcChk(1.5) in Query Analyzer, or is it possible you are returning
this inline with part of another SQL statement, where datatype precedence
might be playing a role?

A



Show quoteHide quote
"Mav" <M**@discussions.microsoft.com> wrote in message
news:28A9A0C5-3EC9-48C1-88ED-6BF889E51970@microsoft.com...
> When passing a fractional/decimal value like for example 1.5, the
> following
> UDF (User defined function) truncates the 1.5 to 1.  Why?
> ALTER FUNCTION dbo.OTimCalcChk  (@X numeric(4,2))
> RETURNS numeric(4,2)
> AS
> BEGIN
> RETURN @X
> END
> I'm using this UDF to test SQL 2000's method of operation.  I ended up
> with
> because I was getting unexpected results from a more complicated UDF.  The
> joke of it is that if you SET @X = 1.5, then it returns the correct
> result.
> Pls help me!
> --
> Mav
Author
10 Aug 2006 4:05 AM
Steve Kass
Mav,

It sounds to me like this function is being called from a
computer whose regional settings are set for a decimal
separator other than period (.), probably a comma (,).
One solution might be to pass the function a string
(in other words, pass '1.5' instead of 1.5), so the
conversion is done by SQL Server. If all else fails,
you may have to change the regional settings, but how
to proceed depends on where and how this function
is called.

Steve Kass
Drew University
www.stevekass.com

Mav wrote:

Show quoteHide quote
>When passing a fractional/decimal value like for example 1.5, the following
>UDF (User defined function) truncates the 1.5 to 1.  Why?
>ALTER FUNCTION dbo.OTimCalcChk  (@X numeric(4,2))
>RETURNS numeric(4,2)
>AS
>BEGIN
>RETURN @X
>END
>I'm using this UDF to test SQL 2000's method of operation.  I ended up with
>because I was getting unexpected results from a more complicated UDF.  The
>joke of it is that if you SET @X = 1.5, then it returns the correct result. 
>Pls help me!

>
Author
10 Aug 2006 6:20 AM
Mav
a. I have checked my regional settings: English South Africa.  The decimal
symbol is a period, so it’s not that.
b. I’m developing from within Access 2003 as I’m creating an .adp project.
c. I tested it a bit more and got the following results:
If I call the function from within a view(query), it gives the correct
result, therefore your suggestion of ‘select dbo.OTimCalcChk(1.5)’ works. 
However, the interesting thing is that when I run the function from Access
2003’s function designer, it prompts me for input, I enter the 1.5, and then
it truncates the input.  I can now see that there is a flaw in using that
route to test the UDF.  I guess Access 2003 does some implicit conversion
before passing it to SQL Server.
Thanks!

PS: Sorry for the late reply, I think the time shift means I sleep when
you’re awake.

--
Mav


Show quoteHide quote
"Steve Kass" wrote:

> Mav,
>
> It sounds to me like this function is being called from a
> computer whose regional settings are set for a decimal
> separator other than period (.), probably a comma (,).
> One solution might be to pass the function a string
> (in other words, pass '1.5' instead of 1.5), so the
> conversion is done by SQL Server. If all else fails,
> you may have to change the regional settings, but how
> to proceed depends on where and how this function
> is called.
>
> Steve Kass
> Drew University
> www.stevekass.com
>
> Mav wrote:
>
> >When passing a fractional/decimal value like for example 1.5, the following
> >UDF (User defined function) truncates the 1.5 to 1.  Why?
> >ALTER FUNCTION dbo.OTimCalcChk  (@X numeric(4,2))
> >RETURNS numeric(4,2)
> >AS
> >BEGIN
> >RETURN @X
> >END
> >I'm using this UDF to test SQL 2000's method of operation.  I ended up with
> >because I was getting unexpected results from a more complicated UDF.  The
> >joke of it is that if you SET @X = 1.5, then it returns the correct result. 
> >Pls help me!
> > 
> >
>
Author
10 Aug 2006 10:26 PM
Steve Kass
Mav,

This could be a bug in the Access 2003 function designer. If the tool
you are talking about is
http://msdn.microsoft.com/library/en-us/dnsmart01/html/sa01g8figure4.jpg,
I wouldn't be surprised if it had bugs - the SQL Server Management Studio
designers that look like this are terribly buggy.

If there is an alternative to this designer, try using it.  (In SQL
Server, the
alternative would be a CREATE FUNCTION statement in SQL.) If not,
you might post to or browse the relevant Access newsgroups.

SK

Mav wrote:

Show quoteHide quote
>a. I have checked my regional settings: English South Africa.  The decimal
>symbol is a period, so it’s not that.
>b. I’m developing from within Access 2003 as I’m creating an .adp project.
>c. I tested it a bit more and got the following results:
>If I call the function from within a view(query), it gives the correct
>result, therefore your suggestion of ‘select dbo.OTimCalcChk(1.5)’ works. 
>However, the interesting thing is that when I run the function from Access
>2003’s function designer, it prompts me for input, I enter the 1.5, and then
>it truncates the input.  I can now see that there is a flaw in using that
>route to test the UDF.  I guess Access 2003 does some implicit conversion
>before passing it to SQL Server.
>Thanks!
>
>PS: Sorry for the late reply, I think the time shift means I sleep when
>you’re awake.
>

>