views:

99

answers:

1
CREATE TABLE [dbo].[MembershipModule](
 [Id] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
 [ParentId] [uniqueidentifier] NULL,
 [TargetId] [int] NULL,
 [WebContentId] [uniqueidentifier] NULL,
 [Name] [varchar](35) NOT NULL,
 [NameUpper]  AS (isnull(upper([Name]),'')) PERSISTED NOT NULL,
 [UriPrefix] [varchar](max) NULL,
 [UriText] [varchar](max) NULL,
 [UriComputed]  AS ??? PERSISTED,
 [Description] [varchar](100) NULL,
 [Created] [date] NOT NULL,
 [Modified] [datetime2](7) NOT NULL,
 [MenuItem] [bit] NOT NULL,
 [Enabled] [bit] NOT NULL,
 [Position] [smallint] NULL,
 CONSTRAINT [PK_MembershipModule] PRIMARY KEY CLUSTERED 
(
 [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

So far the UriComputed field is computed like this:

lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-'))

This produces output like the following

Results

Now, I'd want to terminate all UriComputed values with '/'. This would be easily accomplished by adding + '/' to the computed field, except for the fact that "textless" uris, would be terminated like //, which I don't want happening.

since the sql I can put into a computed field is quite limited (and I don't really know the extents of these limitations) I thought I'd ask here how to add this.

basically I want the output in the image to be

/a/login/
/a/announcements/
/a/
/

my closest attempt at doing this has been:

isnull(convert(varchar(MAX),nullif(len(coalesce([UriText],[Name])),0)),'/')

Which does kind of a mess, and adds a number if it should terminate in '/', and adds '/' when it should, what I'd need is the opposite ( that is, '/' when the length is 0, '' otherwise)

If there's an inline if or something like that I could use that would basically be it, but I don't know about that.

Thank you!

+2  A: 

This worked for me:

[UriComputed]  AS (CASE 
                     WHEN RIGHT(lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-')), 1) = '/' THEN
                       lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-'))
                     ELSE
                       lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-')) +'/'
                   END) PERSISTED,

Full CREATE TABLE statement:

CREATE TABLE [dbo].[MembershipModule](
 [Id] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
 [ParentId] [uniqueidentifier] NULL,
 [TargetId] [int] NULL,
 [WebContentId] [uniqueidentifier] NULL,
 [Name] [varchar](35) NOT NULL,
 [NameUpper]  AS (isnull(upper([Name]),'')) PERSISTED NOT NULL,
 [UriPrefix] [varchar](max) NULL,
 [UriText] [varchar](max) NULL,
 [UriComputed]  AS (CASE 
                     WHEN RIGHT(lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-')), 1) = '/' THEN
                       lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-'))
                     ELSE
                       lower(replace(isnull([UriPrefix],'/')+coalesce([UriText],[Name]),' ','-')) +'/'
                    END) PERSISTED,
 [Description] [varchar](100) NULL,
 [Created] [date] NOT NULL,
 [Modified] [datetime2](7) NOT NULL,
 [MenuItem] [bit] NOT NULL,
 [Enabled] [bit] NOT NULL,
 [Position] [smallint] NULL)
OMG Ponies
+1, worked for me. OMG, you gave me my first rep points a few weeks back, so... thanks! :)
adrift
@adrift: Thanks--You're welcome, too.
OMG Ponies
I made a user-defined function for it, then just [UriComputed] AS dbo.Func([Id]) PERSISTED
Nico