CaptainMyCaptain


Hello All,

I hope that some one here can help. I should note that I have very limited experience in SQL. We are upgrading from a Redbrick (IBM) warehouse to a SQL server version. In an effort to avoid rewriting 3600 reports I am trying to create a user defined function in SQL that looks like a Redbrick function. Rebrick uses the function SUBSTR() where sql uses SUBSTRING(). I simply want to define a function in sql that uses SUBTRING when is sees SUBSTR.

On a separate note Redbrick also uses CURRENT_DATE (reserved in SQL). I want to get the system to see getdate instead. This is what I came up with, but it does not appear to work:

ALTER FUNCTION [dbo].[CURRENT_DATE]

(

)

RETURNS DATETIME

AS

BEGIN

-- Declare the return variable here

--DECLARE GETDATE DATETIME

-- Add the T-SQL statements to compute the return value here

--SELECT GETDATE()

-- Return the result of the function

RETURN GETDATE()

END

Any help would be appreciated.

Thanks,

Rog




Re: User Defined Function Help Needed

K H Tan


you can't use getdate() a non-deterministic function in UDF.

there are other work around like creating a view and then in the UDF selecting from the view

create view today
as
select [now] = getdate()

select [now] from today






Re: User Defined Function Help Needed

K H Tan

are you using SQL 2000

using the getdate() in UDF will works in SQL 2005 but not in 2000






Re: User Defined Function Help Needed

CaptainMyCaptain

I am using SQL 2005.



Re: User Defined Function Help Needed

K H Tan

so . . what is the problem then Are you getting any error message





Re: User Defined Function Help Needed

CaptainMyCaptain

Sample Query:

select * from umr.dbo.medicalclaimlineitem where paiddate<=dbo.CURRENT_DATE

Result:

Msg 156, Level 15, State 1, Line 1

Incorrect syntax near the keyword 'CURRENT_DATE'.





Re: User Defined Function Help Needed

K H Tan

select * from umr.dbo.medicalclaimlineitem where paiddate<=dbo.CURRENT_DATE()






Re: User Defined Function Help Needed

CaptainMyCaptain

Same error message occurs.



Re: User Defined Function Help Needed

K H Tan

select * from umr.dbo.medicalclaimlineitem where paiddate<=dbo.[CURRENT_DATE]()






Re: User Defined Function Help Needed

CaptainMyCaptain

We were close.  The correct answer is:

select * from umr.dbo.medicalclaimlineitem where paiddate<=umr.dbo.[CURRENT_DATE]()

 

Thank you so much for your assistance on this matter!

 

Any ideas for the subtr vs. substring

Roger





Re: User Defined Function Help Needed

K H Tan

what about substr vs substring





Re: User Defined Function Help Needed

CaptainMyCaptain

Again, I'm a novice here...

This is what I have so far, but it does not work:

CREATE FUNCTION dbo.substr

(

-- Add the parameters for the function here

@Expression varchar

@Start bigint

@Length bigint

)

RETURNS varchar

AS

BEGIN

-- Declare the return variable here

--DECLARE @Result varchar, nvarchar, text, ntext, varbinary, image

-- Add the T-SQL statements to compute the return value here

--SELECT @Result = @Expression

-- Return the result of the function

RETURN substring(@Expression @Start @Length)

END

GO

Here is the error:

Msg 102, Level 15, State 1, Procedure substr, Line 10

Incorrect syntax near '@Start'.

Msg 137, Level 15, State 2, Procedure substr, Line 23

Must declare the scalar variable "@Expression".

I want SUBSTR to function EXACTLY like SUBSTRING() would. I tried forming the same parameters but it does not like them.





Re: User Defined Function Help Needed

CaptainMyCaptain

Nevermind. I got it.

CREATE FUNCTION dbo.substr

(

-- Add the parameters for the function here

@Expression as varchar,

@Start as bigint,

@Length as bigint

)

RETURNS varchar

AS

BEGIN

-- Declare the return variable here

--DECLARE @Result varchar, nvarchar, text, ntext, varbinary, image

-- Add the T-SQL statements to compute the return value here

--SELECT @Result = @Expression

-- Return the result of the function

RETURN substring(@Expression, @Start, @Length)

END

GO