Hyperlinks in SQL Server – Insert, Generate, Images & More

In this tutorial, I will show you how to use hyperlinks in SQL Server tables. You will learn how to create hyperlinks for text and images, generate, and also manipulate hyperlinks in SQL Server.

Before we get started, let’s learn about hyperlinks.

A Hyperlink is a clickable digital reference to data. It takes a user from one digital location to another. You can read more about hyperlinks on Wikipedia.

The syntax of a hyperlink in HTML looks like this:

<a href="URL" title="link title" target="link target" class="link class">link label</a>

For the purpose of this tutorial, let’s also see the HTML syntax for images.

<img src="URL" alt="Image Title" width="X" height="Y">

Now that we understand hyperlink syntax IN HTML, how do we create them in SQL? Let’s get started.

How to add hyperlinks in SQL Server

Let’s start by adding a hyperlink column to a table.

ALTER TABLE table_name
ADD hyperlink_column VARCHAR(255);

This code will add a new column “hyperlink_column” column to “table_name” table.

How to insert hyperlink in SQL query

To insert a hyperlink into the column we just created, the SQL query is as follows:

INSERT INTO table_name (hyperlink_column)
VALUES ('<a href="URL" title="link title" target="link target" class="link class">link label</a>')

Here’s my resultset.

How to replace a text column as hyperlink

You can also update existing text columns as hyperlinks using this code.

UPDATE table_name
SET hyperlink_column = CONCAT('<a href="'+[url_column]+'"'
,' title="' + [title_column] + '"'
,' target="' + [target_column] + '"'
,' class="' + [class_column] + '"'
, '>'
, [text_column]
, '</a>') 
where id = 1

How to generate hyperlinks in SQL query

Now let’s consider a situation where you need to combine columns to generate a hyperlink in SQL Server.

SELECT
'<a href="'+[url_column]+'"'
+' title="' + [title_column] + '"'
+' target="' + [target_column] + '"'
+' class="' + [class_column] + '"'
+ '>'
+ [text_column]
+ '</a>' AS hyperlink_column
FROM table_name

Alternatively, you can use the CONCAT function.

SELECT
CONCAT('<a href="'+[url_column]+'"'
,' title="' + [title_column] + '"'
,' target="' + [target_column] + '"'
,' class="' + [class_column] + '"'
, '>'
, [text_column]
, '</a>') AS hyperlink_column
FROM table_name

How to generate hyperlinks in SQL query that opens in a new page

There was a time I was generating my URLs in SQL for SSRS reporting. When clicking the links on the web page, the URL opens on the same page. However, users needed the reports to open on another tab.

This is a simple fix. To do this, add or change the target value to “_blank”.

SELECT
CONCAT('<a href="'+[url_column]+'"'
,' title="' + [title_column] + '"'
,' target="_blank"'
,' class="' + [class_column] + '"'
, '>'
, [text_column]
, '</a>') AS hyperlink_column
FROM table_name

This attribute and value (target=”_blank”) make your hyperlink open in a new tab on your web browser.

How to generate image URL in SQL server

This is similar to generating hyperlinks. The difference is the syntax for images is different. Using the CONCAT SQL Server function, you can generate an image URL as follows:

SELECT
CONCAT('<img src="'+[src_column]+'"'
,' alt="' + [alt_column] + '"'
,' width="' + [width_column] + '"'
,' height="' + [height_column] + '"'
, '>') AS image_column
FROM table_name

I hope you found this article helpful. To learn more, check out my SQL section.