tags:

views:

20

answers:

2

I am sure that I am overlooking some simple thing, I admit that I am still learning various aspects of SQL and the Datepart function is one..

I am inputting a date and returning an invalid quarter, here is the code:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION Dbo.FN_Get_Quarter (
  -- the parameters for the function here
  @date                  varchar(10)
) RETURNS BIT
AS
BEGIN

  RETURN datepart(qq,@date)

END

Here is the test I am using and the returned value...

select dbo.FN_GET_QUARTER('07-14-2010') 

...returns 1

+1  A: 

datepart returns an int, but you've declared your function return type as bit

mfabish
+1: You were first
OMG Ponies
A: 

Returning a BIT means the value can only be 0 or 1. So you have to change your data type to INT to get the actual value being returned:

ALTER FUNCTION Dbo.FN_Get_Quarter (
  @date                  varchar(10)
) RETURNS INT
AS
BEGIN

  RETURN datepart(qq,@date)

END
OMG Ponies
Oh so simple! I feel a bit foolish for overlooking that, thanks!
JMS49