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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Can't figure this out.

Author  Topic 

SamC
White Water Yakist

3467 Posts

Posted - 2003-12-01 : 00:11:36
I've got a string replacement function which is used (infrequently) in SQL to build email body text. Here's the complete function:

CREATE FUNCTION dbo.UDF_EmailReplaceText(@Text NVARCHAR (4000), @UserID INT, @CourseID INT, @TheDate DATETIME)
/*************************************************************************************************

*************************************************************************************************/
returns NVARCHAR (4000)
AS
BEGIN
DECLARE @ReturnText NVARCHAR (4000)
SELECT @ReturnText = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(IsNull(@Text, N'NULL Source'),
N'[CR]', NCHAR(10)+NCHAR(13)) ,
N'[Email]', U.Email) ,
N'[UserID]', CAST(U.UserID AS NVARCHAR)) ,
N'[CourseID]', CAST(CO.CourseID AS NVARCHAR)) ,
N'[Time]', RIGHT(CONVERT(NVARCHAR, @TheDate, 100), 7)) ,
N'[Date]', CONVERT(NVARCHAR, @TheDate, 107) ) ,
N'[Clientnamefull]', CL.Clientnamefull) ,
N'[Clientname]', CL.Clientname) ,
N'[Organization]', IsNull(C.Centername + ' - ' + B.Branchname, '')) ,
N'[Orgname]', IsNull(C.Centername + ' - ' + B.Branchname, N'')) ,
N'[Branchname]', IsNull(B.Branchname, '')),
N'[Centername]', C.Centername),
N'[Fullname]', U.Fullname),
N'[CRecordID]', CAST(CR.CRecordID AS NVARCHAR)),
N'[Acknowledge]', CASE WHEN CR.CRAcknowledge = 1 THEN N'Acknowledged' ELSE N'Denied' END),
N'[Coursename]', CO.Coursename)
FROM dbo.Users U
LEFT OUTER JOIN Centers C ON C.CenterID = U.CenterID
LEFT OUTER JOIN Branches B ON B.BranchID = U.BranchID
INNER JOIN Clients CL ON CL.ClientID = U.ClientID
LEFT OUTER JOIN Courses CO ON CO.CourseID = @CourseID
LEFT OUTER JOIN (SELECT TOP 1 * FROM dbo.CourseRecords WHERE CourseID = @CourseID AND UserID = @UserID ORDER BY CRecordStart DESC) CR ON CR.UserID = U.UserID
WHERE U.UserID = @UserID

RETURN (CASE WHEN @ReturnText IS NULL THEN IsNull(@Text, N'Null @Text') ELSE @ReturnText END)
END
GO


After some painful debugging, I found that if no row was found in the Branches table, the calculated @ReturnText would be NULL, even when no REPLACE operation was performed using any value from the Branch table. For example, the source string:

'[fullname]'

Would return a NULL string because Branches had no matching row.

I've fixed the NULL result by using IsNULL anywhere BranchName was referenced. Problem fixed --- but I don't understand the behavior?

Any thoughts? (Or any suggestion improvement to the function.)

Sam

nr
SQLTeam MVY

12543 Posts

Posted - 2003-12-01 : 01:32:53
Try
select replace('aaa','z',null)

You should get null - so if any replacement values are null the whole expression will be null.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2003-12-01 : 07:38:09
I should've tried that test before posting. I'm very surprised that it behaves that way.

Thanks,

Sam
Go to Top of Page
   

- Advertisement -