Wrox Programmer Forums
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 27th, 2005, 10:00 PM
Registered User
 
Join Date: Jun 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default rounding in sql

I am having some problem with this query:

SELECT Jaso.dan_LOBTB028PATENT,
 (DAYS(CURRENT_DATE)-DAYS(jason.datecreated))/30
FROM Video

In the line where the days
function starts this whole line is getting the diffrence between todays date
and the date in the column(Datecreated) field. Right now I am getting a
number like 12 or 6. I cannot get decimals so how can I get the decimals to
show also.For example like 12.6 or 6.3.





Thanks a lot,
daisy

 
Old June 28th, 2005, 12:43 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Looks like you are dealing with UDFs. Show the code for them.
 
Old June 28th, 2005, 08:53 AM
Registered User
 
Join Date: Jun 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

no I am not using user defined function:
(DAYS(CURRENT_DATE)-DAYS(video.datecreated))/30

There is just one typo instead of jason it should have been video and DAYS and CURRENT_DATE is a function in SQL language.

Thanks
Daisy

 
Old June 28th, 2005, 09:03 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

CURRENT_DAYS and DAYS are not SQL language functions. They must be specific to the SQL Server you are using.
 
Old June 28th, 2005, 07:45 PM
Registered User
 
Join Date: Jun 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

http://www.oreilly.com/catalog/sqlnut/chapter/ch04.html

The functions are listed on this site.

 
Old June 28th, 2005, 07:50 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by daisy_polly
 I am having some problem with this query:

SELECT Jaso.dan_LOBTB028PATENT,
(DAYS(CURRENT_DATE)-DAYS(jason.datecreated))/30
FROM Video

In the line where the days function starts this whole line is getting the diffrence between todays date and the date in the column(Datecreated) field. Right now I am getting a number like 12 or 6. I cannot get decimals so how can I get the decimals to show also. For example like 12.6 or 6.3.
The DAY function (that's what you meant, right? There is no DAYS function) returns the day portion of a datetime as an integer. Since you are computing the difference between two integers, the result is an integer. You then divide by an integer (30) so by default the division is integer division and the result is an integer. This conversion truncates any fractional part.

Try dividing by a decimal number to force the expression to be evaluated as a decimal number instead; your expression would then be something like:
Code:
SELECT (DAY(current_date)-DAY(jason.datecreated)) / 30.0
This by default give the result to 4 decimal places; you can use CAST/CONVERT to alter the precision of the result.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old June 28th, 2005, 08:06 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

BTW, I just looked a bit closer at your expression and realized it almost certainly will not give you what you are probably looking for. The DAY function simply returns the day portion of a date. You are computing the difference between the day of the month of one date with the day of the month of another. This will be a negative number sometimes. If you compute your expression early in the month aginst a date later in its month, the result would be negative. If you happen to execute the expression on the same day of the month as your other date, even if it is in a diferent month and years apart, the result would be zero.

I think what you may want to use instead is the DATEDIFF function, as:
Code:
SELECT (DATEDIFF(d , jason.datecreated , current_date)) / 30.0

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old June 29th, 2005, 08:36 AM
Registered User
 
Join Date: Jun 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok thanks for the help I will try this.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Rounding JasperGIS Beginning VB 6 1 December 14th, 2005 03:09 PM
rounding up SqlDbType.Money TYPE in sql server olambe BOOK: ASP.NET Website Programming Problem-Design-Solution 2 December 9th, 2005 04:46 PM
Rounding kilika SQL Server 2000 4 June 1st, 2005 03:36 PM
Rounding in C# cjo ASP.NET 1.0 and 1.1 Basics 3 November 3rd, 2003 04:12 PM
Rounding Droopy Classic ASP Basics 3 August 14th, 2003 08:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.