Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 DAY COUNT

Author  Topic 

annamaria
Starting Member

30 Posts

Posted - 2007-04-17 : 14:16:29
review

sshelper
Posting Yak Master

216 Posts

Posted - 2007-04-17 : 14:34:50
To get the difference in days, you will use the DATEDIFF function. To convert your column to a DATETIME data type, you can do the following:

SELECT CAST(CAST(20070417 AS VARCHAR(8)) AS DATETIME)

SQL Server Helper
http://www.sql-server-helper.com
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-17 : 14:38:09
Huh?

What are you to do with dates 20070331 and 20070815. How many days are they apart according to your business rules?
What are you to do with dates 20070410 and 20070531?


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-17 : 15:39:56
Create this function in your database
CREATE FUNCTION dbo.fnGiorno360
(
@Date1 INT,
@Date2 INT
)
RETURNS INT
AS
BEGIN
DECLARE @Months INT,
@Days INT,
@dt1 DATETIME,
@dt2 DATETIME

IF @Date1 > @Date2
SELECT @Days = @Date1,
@Date1 = @Date2,
@Date2 = @Days

SELECT @dt1 = CAST(@Date1 AS VARCHAR),
@dt2 = CAST(@Date2 AS VARCHAR),
@Months = DATEDIFF(MONTH, @dt1, @dt2)

IF @Months > 0
SET @Months = @Months - 1

SELECT @Days = 30 - CASE WHEN DATEPART(DAY, @dt1) > 30 THEN 30 ELSE DATEPART(DAY, @dt1) END,
@Days = @Days + CASE WHEN DATEPART(DAY, @dt2) > 30 THEN 30 ELSE DATEPART(DAY, @dt2) END

RETURN @Days + 30 * @Months
END
Then use it with
select dbo.fnGiorno360(20070331, 20070815)


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-04-17 : 15:44:08
>> created to simplify manual calculations

Accountants have a complicated way of making things simple.





CODO ERGO SUM
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-17 : 16:16:37
Hi Anna! Le cose sono grandi in Svezia. È molto piena di sole e la temperatura è superiore a 20 gradi. Come sono le cose a Verona? Il miei girlfriend ed io stanno pensando ad avere una vacanza in Italia questa estate. Il mio girlfriend ha una zia in Italia del Nord e possiamo prendere in prestito la sua casa. Non è lontano dalla città di Rimini ed è di soltanto 500 tester al mare adriatico!


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-04-17 : 16:38:49
What is QMF? DB2?

Tara Kizer
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-04-17 : 17:00:31
quote:
Originally posted by annamaria

[quote]Originally posted by Michael Valentine Jones

>> created to simplify manual calculations
Accountants have a complicated way of making things simple.

I wonder if you are talking about accountants in general or if you
are relating to the way I explained the 30/360 day count to Peter.
In fact I could have been clearer but hopefully he has managed to
solve the problem in spite of it.
I have another problem. This is the second time I post in the forum
and I've noticed that there are differences between the commands you use and the ones I use at the office.
I use QMF for Windows and, for examples, I don't think we use the carachter @. There also differences with ( ) and others.
I don't know if I will able to use Peter's function. Could you telle me how to translate your synthax into mine?

What's the meaning of CODO? And what's a PDA?
Hope to hear from you soon.
Anna from Verona (Italy)



If you are using QMF for Windows, that means that you are probably using a DB2 database. This web site deals only with Microsoft SQL Server database questions, so it is very likely that the answers that have been provided will not work with DB2.

If you are using DB2, you should post your questions on a site that answers DB2 questions.

You might try posting your questions on the DB2 forum at
http://www.dbforums.com/





CODO ERGO SUM
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-04-17 : 17:08:28
You've posted on a Microsoft SQL Server site. I only know of one person here who uses DB2. You'd probably have better success with your questions if you posted in a DB2 forum. There is one over at dbforums.com.

Tara Kizer
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -