Sybase NNTP forums - End Of Life (EOL)

The NNTP forums from Sybase - forums.sybase.com - are now closed.

All new questions should be directed to the appropriate forum at the SAP Community Network (SCN).

Individual products have links to the respective forums on SCN, or you can go to SCN and search for your product in the search box (upper right corner) to find your specific developer center.

Division

4 posts in General Discussion Last posting was on 2011-10-06 04:47:42.0Z
JT Posted on 2011-10-05 15:20:06.0Z
Sender: cbc.4e8c72ef.1804289383@sybase.com
From: JT
Newsgroups: sybase.public.ase.general
Subject: Division
X-Mailer: WebNews to Mail Gateway v1.1t
Message-ID: <4e8c75a6.d5d.1681692777@sybase.com>
NNTP-Posting-Host: 10.22.241.41
X-Original-NNTP-Posting-Host: 10.22.241.41
Date: 5 Oct 2011 08:20:06 -0700
X-Trace: forums-1-dub 1317828006 10.22.241.41 (5 Oct 2011 08:20:06 -0700)
X-Original-Trace: 5 Oct 2011 08:20:06 -0700, 10.22.241.41
Lines: 8
Path: forums-1-dub!not-for-mail
Xref: forums-1-dub sybase.public.ase.general:30572
Article PK: 72745

Very simple but below statement returns 0 rather than
0.729007634 even with conversion. Appreciate a little
guidance as I am a newbie to programming.

select 19100/26200
go
-----------
0


Sherlock, Kevin [TeamSybase] Posted on 2011-10-05 15:40:12.0Z
From: "Sherlock, Kevin [TeamSybase]" <kevin.sherlock@teamsybase.com>
Newsgroups: sybase.public.ase.general
References: <4e8c75a6.d5d.1681692777@sybase.com>
Subject: Re: Division
Lines: 21
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5512
X-RFC2646: Format=Flowed; Original
NNTP-Posting-Host: vip152.sybase.com
X-Original-NNTP-Posting-Host: vip152.sybase.com
Message-ID: <4e8c7a5c$1@forums-1-dub>
Date: 5 Oct 2011 08:40:12 -0700
X-Trace: forums-1-dub 1317829212 10.22.241.152 (5 Oct 2011 08:40:12 -0700)
X-Original-Trace: 5 Oct 2011 08:40:12 -0700, vip152.sybase.com
X-Authenticated-User: teamsybase
Path: forums-1-dub!not-for-mail
Xref: forums-1-dub sybase.public.ase.general:30573
Article PK: 72746

You are dividing two integers. An integer will be your result.

Try:

select 19100.00 / 26200.00

or

select convert(numeric(20,10),19100) / 26200

<JT> wrote in message news:4e8c75a6.d5d.1681692777@sybase.com...
> Very simple but below statement returns 0 rather than
> 0.729007634 even with conversion. Appreciate a little
> guidance as I am a newbie to programming.
>
> select 19100/26200
> go
> -----------
> 0


Michael Peppler [Team Sybase] Posted on 2011-10-06 04:47:42.0Z
From: "Michael Peppler [Team Sybase]" <mpeppler@peppler.org>
Organization: Peppler Consulting SARL
Subject: Re: Division
User-Agent: Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity.)
Message-ID: <pan.2011.10.06.04.47.39.602890@peppler.org>
Newsgroups: sybase.public.ase.general
References: <4e8c75a6.d5d.1681692777@sybase.com> <4e8c7a5c$1@forums-1-dub>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
NNTP-Posting-Host: vip152.sybase.com
X-Original-NNTP-Posting-Host: vip152.sybase.com
Date: 5 Oct 2011 21:47:42 -0700
X-Trace: forums-1-dub 1317876462 10.22.241.152 (5 Oct 2011 21:47:42 -0700)
X-Original-Trace: 5 Oct 2011 21:47:42 -0700, vip152.sybase.com
Lines: 20
Path: forums-1-dub!not-for-mail
Xref: forums-1-dub sybase.public.ase.general:30575
Article PK: 73464


On Wed, 05 Oct 2011 08:40:12 -0700, Sherlock, Kevin [TeamSybase] wrote:

> You are dividing two integers. An integer will be your result.
>
> Try:
>
> select 19100.00 / 26200.00
>
> or
>
> select convert(numeric(20,10),19100) / 26200

or even

select 19100 / 26200e

(where the e indicates that it's a float)

Michael


Bret Halford Posted on 2011-10-05 15:52:17.0Z
From: Bret Halford <bret@sybase.com>
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1
MIME-Version: 1.0
Newsgroups: sybase.public.ase.general
Subject: Re: Division
References: <4e8c75a6.d5d.1681692777@sybase.com>
In-Reply-To: <4e8c75a6.d5d.1681692777@sybase.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: vip152.sybase.com
X-Original-NNTP-Posting-Host: vip152.sybase.com
Message-ID: <4e8c7d31$1@forums-1-dub>
Date: 5 Oct 2011 08:52:17 -0700
X-Trace: forums-1-dub 1317829937 10.22.241.152 (5 Oct 2011 08:52:17 -0700)
X-Original-Trace: 5 Oct 2011 08:52:17 -0700, vip152.sybase.com
Lines: 91
Path: forums-1-dub!not-for-mail
Xref: forums-1-dub sybase.public.ase.general:30574
Article PK: 72747


On 10/5/2011 9:20 AM, JT wrote:
> Very simple but below statement returns 0 rather than
> 0.729007634 even with conversion. Appreciate a little
> guidance as I am a newbie to programming.
>
> select 19100/26200
> go
> -----------
> 0

What kind of conversions have you tried?

You need to become aware of what datatypes
are in play, their place in the hierarchy of datatypes
and how implicit conversion (or lack thereof) affects the choice of
datatype of the result.

http://infocenter.sybase.com/help/topic/com.sybase.infocenter.dc36271.1550/html/blocks/blocks13.htm


And the rules for how the parser decides on the datatype for
a literal value
http://infocenter.sybase.com/help/topic/com.sybase.infocenter.dc36271.1550/html/blocks/blocks9.htm?



As written, the ASE parser reads this as
an INT divided by an INT, which will yield an INT.

If you add a decimal point to either value, it tells
the parser that the literal value can't be an INT
and so has to be something that supports fractional
values such as REAL, DOUBLE, or NUMERIC.

1> select 19100.0/26200
2> go

--------------------
0.729007633587

(1 row affected)

A trick I've long used to determine the datatype
chosen for a particular literal value is to select
the value into a temp table and see what datatype
was assigned to the column:

select 19100 as "col1" into #foo
go
sp_help #foo
go

Name Owner Object_type Object_status Create_date
--------------------- ----- ----------- ------------- -------------------
#foo00000200010050317 dbo user table -- none -- Oct 5 2011 9:24AM

(1 row affected)
Column_name Type Length Prec Scale Nulls Default_name Rule_name
Access_Rule_name Computed_Column_object Identity
----------- ---- ------ ---- ----- ----- ------------ ---------
---------------- ---------------------- ----------
col1 int 4 NULL NULL 0 NULL NULL


I feel it is good practice to always specify the datatype
you intend a literal to have.

1>
2>
3> select convert(float, 19100) / convert(float,26200)
4> go

---------------------------
0.729008

(1 row affected)

Specifying the datatype is particularly important if NULL is
involved. (Pop quiz: If you saw "select ... NULL as col1 ..."
in some code, is the resulting datatype what the programmer
intended? )

You might even want to specify the datatype of the result

select convert (numeric(12,4),
convert(float, 19100) / convert(float,26200)
)