Q:

Validate url with a check constraint

I would like to create a CHECK CONSTRAINT which validates the url.  
Here is my first approach, but it also takes hashtags. 
CHECK ([ServiceURL] LIKE 'https://[a-z0-9]%.[a-z0-9]%')

Another idea was this
CHECK ([ServiceURL] LIKE 'https://[a-z0-9].[a-z0-9]')

But this doesn't work as well.  any ideas? 

A:

Taking Gary's example further, the version below shows how we can write the function without the use of a cursor and how we can add the check constraint to the table.   You'd likely want to extend this to cope with http as well as https, and possibly other modifications (like allowing the / character)
CREATE FUNCTION dbo. IsValidURL (@Url VARCHAR(200))
RETURNS INT
AS
BEGIN
    IF CHARINDEX('https://', @url) <> 1
    BEGIN
        RETURN 0;   -- Not a valid URL
    END

    -- Get rid of the http:// stuff
    SET @Url = REPLACE(@URL, 'https://', '');

    -- Now we need to check that we only have digits or numbers
    IF (@Url LIKE '%[^a-zA-Z0-9]%')
    BEGIN
        RETURN 0;
    END

    -- It is a valid URL
    RETURN 1;
END

-- Create the table
CREATE TABLE dbo. MyTableOnlyLettersAndDigits 
(
    id int identity(1,1) primary key clustered,
    ServiceURL varchar(200) not null
);

GO

-- Add the check constraint
ALTER TABLE dbo. MyTableOnlyLettersAndDigits 
    ADD CONSTRAINT chkValidURLs CHECK (dbo. IsValidURL(ServiceURL) = 1);

-- Some tests to show it works
insert into dbo. MyTableOnlyLettersAndDigits(ServiceURL)
values ('Missing prefix. Invalid');

insert into dbo. MyTableOnlyLettersAndDigits(ServiceURL)
values ('https://IamAValidURL123');

insert into dbo. MyTableOnlyLettersAndDigits(ServiceURL)
values ('https://Invalid#Char');

