Avatar of metropia
metropia
Flag for United States of America asked on

UDF not working - passing a date parameter

I created a UDF named: [dbo].[RG_DailyShippingStatusReportMonthToDate]

When I call the function like:

SELECT * FROM [dbo].[RG_DailyShippingStatusReportMonthToDate] (getdate())

I get: Line 1: Incorrect syntax near '('.

Then I replace GETDATE() with the values Ialso used when testing the function with hard-coded values:

SELECT * FROM [RGMFG].[dbo].[RG_DailyShippingStatusReportMonthToDate] (‘2013-04-01’)

But I get zero results.

I have included a copy of this UDF from when I was using hard coded values. When running it that way, I  do get results.

I am also including a copy of it, not using hard coded values.

Would someone be willing to take a look at this and see if you can locate where I am making the mistake?

Thank you very much
udf-with-hard-coded-values.txt
udf-with-out-values.txt
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
metropia

8/22/2022 - Mon
Anthony Perkins

I get: Line 1: Incorrect syntax near '('.
Not having your tables, I tested your Table-Valued UDF with minimal code and I had no problems.  But if I were to guess I suspect the problem lies in the following code:
        SELECT  @startFiscalMonth = dbo.RG_GetFiscalMonthStart(@dt),
                @endFiscalMonth = dbo.RG_GetFiscalMonthEnd(@dt),
                @fiscalYearNumber = dbo.RG_GetFiscalYearName(@dt),
                @fiscalMonthNumber = dbo.RG_GetFiscalMonthKeyFiscal(@dt),
                @currentDateMinusOne = dbo.minusOneDay(@dt)
SOLUTION
Lowfatspread

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
metropia

ASKER
I will post the functions tomorrow. I apologize, I need the help still but don't have the code at this moment.

Thanks.
metropia

ASKER
here are the functions:

ALTER FUNCTION [dbo].[RG_GetFiscalYearName] (@lookupDate AS datetime)
RETURNS VARCHAR(15)
AS
begin
	DECLARE @intDate BIGINT, @yearName VARCHAR(15)
	SELECT @intDate = DAY(@lookupDate) + (MONTH(@lookupDate)) * 100 + (YEAR(@lookupDate) * 10000)


	SELECT @yearName = MIN(t.FiscYearName)
	FROM 
		dbo.DIM_Time_PC t
	WHERE 
		t.id = @intDate
		
	RETURN @yearName
END

ALTER FUNCTION [dbo].[RG_GetFiscalMonthStart] (@lookupDate AS datetime)
RETURNS datetime
AS
begin
	DECLARE @intDate BIGINT, @monthStart DATETIME
	SELECT @intDate = DAY(@lookupDate) + (MONTH(@lookupDate)) * 100 + (YEAR(@lookupDate) * 10000)


	SELECT @monthStart = MIN(t2.formatDate)
	FROM 
		dbo.DIM_Time_PC t INNER JOIN
		dbo.DIM_Time_PC t2 ON t.FiscYearKey = t2.FiscYearKey AND t.MonthKeyFisc = t2.MonthKeyFisc
	WHERE 
		t.id = @intDate
		
	RETURN @monthStart
END

ALTER FUNCTION [dbo].[RG_GetFiscalMonthKeyFiscal] (@lookupDate AS datetime)
RETURNS VARCHAR(15)
AS
begin
	DECLARE @intDate BIGINT, @monthStart VARCHAR(15)
	SELECT @intDate = DAY(@lookupDate) + (MONTH(@lookupDate)) * 100 + (YEAR(@lookupDate) * 10000)


	SELECT @monthStart = MIN(t2.MonthKeyFisc)
	FROM 
		dbo.DIM_Time_PC t INNER JOIN
		dbo.DIM_Time_PC t2 ON t.FiscYearKey = t2.FiscYearKey AND t.MonthKeyFisc = t2.MonthKeyFisc
	WHERE 
		t.id = @intDate
		
	RETURN @monthStart
END

ALTER FUNCTION [dbo].[minusOneDay] 
( 
    @dt DATETIME 
) 
RETURNS DATETIME 
AS 
BEGIN 
    SET @dt = DATEADD(DAY, -1, DATEDIFF(dd, 0,  @dt))
    
    
    
    RETURN @dt 
END

Open in new window



Thanks for your help.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Éric Moreau

where is RG_DailyShippingStatusReportMonthToDate?
metropia

ASKER
That is the UDF located in the same database as the other UDFs.
I attached to this question.
SOLUTION
Éric Moreau

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
metropia

ASKER
amongst the several errors I have found so far, there is one that I am not sure how to fix.

Error:

Syntax error converting the varchar value '*' to a column of data type int.

it comes from this operation within a CASE statement:

SUM((CASE
		WHEN col.PRODUCT_CODE IN ('8500', '9000') THEN 0
		ELSE
			CASE
				WHEN RG_SALESFORECASTBYPRODUCT_WEBSTER.FiscalYear = @fiscalYearNumber
					AND RG_SALESFORECASTBYPRODUCT_WEBSTER.FiscalMonth = @fiscalMonthNumber
					THEN ROUND((shl.SHIPPED_QTY * shl.UNIT_PRICE), 4)
				ELSE 0
			END
		END)) AS 'TotalActualShip$$'

Open in new window


Is there any way to work around this issue?


Thanks a lot.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Anthony Perkins

Syntax error converting the varchar value '*' to a column of data type int.
One of your columns has bad data (it contains a * which cannot be converted). Identify which one it is and what you want to happen in the query.
metropia

ASKER
It was the fiscal year parameter

I decalred it as a varchar without no specific length.

I changed it to an int data type and that fixed that problem.

One thing I noticed is that, when I try my function in SQL Server 2000 in the following manner:

select * from [dbo].[Test] (getdate())

I keep getting the error: Line 1: Incorrect syntax near '('.

Although I have tested each query that make up the function, separately. They all work, just not when they are run as a function.

So I tried it on SQL Server 2008, and the function runs without throwing that error, same code, same everything. just different version of SQL Server.

Can GETDATE() be used a an argument when calling the function?


Please advice.


Thank you much.
ASKER CERTIFIED SOLUTION
Anthony Perkins

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
metropia

ASKER
I am somehow relief that you were able to duplicate the error.
Tomorrow I will test as you show in your post.

One issue I will face assuming that your recommendation works, is that the whole idea of mine to put all the code into a UDF, was to run it from crystal reports.

Having problems with the date parameter, do you think I will be able to run it from CR?

Thanks.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
SOLUTION
Lowfatspread

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
metropia

ASKER
Sort of. Ended up creating a view that contains the current date

SELECT     GETDATE() AS CurrentDate

and then declaring a new datetime variable in the function that is set to the value from the date view.

no need to pass any date parameter anymore.
metropia

ASKER
Thank you everyone for your help.