Link to home
Start Free TrialLog in
Avatar of pacificdb
pacificdbFlag for Australia

asked on

DateSerial in SQL Server 2000

Hi,

Is there a way to use the DateSerial function in SQL Server 2000? I have installed the enterprise edition plus Analysis Services, but (although the Books Online say I can use it), I can't seem to get DateSerial to work. SQL Server just says that it is an unrecognized function.

- Graham
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Can you post how you try to use it?
Avatar of pacificdb

ASKER

angel111,

SELECT DateSerial(2001,1,1)

However, I received an email from a friend saying that DateSerial only works within Analysis Server. That would be consistent with what I found.

- Graham
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
angel111,

Thanks for your response. I wanted to use the DateSerial function so I could work out the start of the next financial quarter. I ended up using the following, but since I'm doped up to the eyeballs with medication at the moment, I'm unsure about whether my code will do the job. What do you think?

SELECT @dte='01/' + convert(char(2),((month(getdate())-1)/3)*3+1)  + '/' + convert(char(4),year(getdate()))
          SELECT @NextQuarter = convert(smalldatetime, @dte)

-- BTW, @dte is a char(10).

- Graham
Not sure, because of the case if the next quarter is in the next year...

There fore, try this:

SELECT @dte='01/' + convert(char(2),((month(getdate())-1)/3)*3)  + '/' + convert(char(4),year(getdate()))
SELECT @NextQuarter = dateadd(month,3,convert(smalldatetime, @dte))

Cheers
Avatar of CleanupPing
CleanupPing

pacificdb:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
Hello!
Here is a real DateSerial function to SQL:
--------------------------------------------------------------------------
CREATE FUNCTION DateSerial(@iYear int, @iMonth int, @iDay int)
RETURNS datetime
AS
BEGIN
DECLARE @dResult datetime

SET @dResult = CONVERT(datetime, CAST(@iYear as nvarchar(10)) + '-01-01')
SET @dResult = DateAdd(m, @iMonth - 1, @dResult)
SET @dResult = DateAdd(d, @iDay - 1, @dResult)

RETURN @dResult
END
-----------------------------------------------------------------------
call it so:
SELECT dbo.DateSerial(2007, 2, 31)

Enjoy! :)
I don't know what else you'd like me to do. I had accepted angelIII's solution back in 2001, and there appears to be no other way to close this question.