Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
551 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Written by Andrew Gould
In this tutorial
The key to dynamic SQL is concatenating strings to form valid SQL statements. This part of the series is designed to give you a brief primer on concatenation so feel free to skip to the next part if you already know how to do this!
To concatenate two strings of text in SQL you use the + symbol. The example below prints the result of concatenating two literal strings:
PRINT 'Today is ' + 'Tuesday'
The fairly unremarkable result of executing the above code is shown below:
The two strings are joined together into a single phrase.
The technique becomes more useful when one or more of the individual strings is provided by a variable component, such as a function:
PRINT 'Today is ' + DATENAME(DW,GETDATE())
The above code calculates the name of the current day of the week and concatenates it with a literal string. The result of executing the code depends on which day it is run.
When concatenating long strings it can be useful to use variables to store the various components of the phrase before joining them together.
DECLARE @DayName AS VARCHAR(9)
SET @DayName = DATENAME(DW,GETDATE())
PRINT 'Today is ' + @DayName
When you're concatenating a phrase you must make sure that each bit of the sentence is a piece of text. The example below attempts to concatenate an integer with a literal string:
DECLARE @TheNumber AS INT
SET @TheNumber = 10
PRINT 'The number is ' + @TheNumber
Attempting to execute this code results in the following error message:
SQL has attempted to convert our literal string into a number - what we actually want to do is convert the number into a string.
You can use the CAST or CONVERT function to change one data type into another. The example below uses CAST:
DECLARE @TheNumber AS INT
SET @TheNumber = 10
PRINT 'The number is ' + CAST(@TheNumber AS VARCHAR(2))
If you prefer you can use the CONVERT function instead:
DECLARE @TheNumber AS INT
SET @TheNumber = 10
PRINT 'The number is ' + CONVERT(VARCHAR(2),@TheNumber)
You're most likely to build dynamic SQL statements using a stored procedure into which you pass parameters. The code below creates a simple stored procedure which accepts a single parameter whose value is concatenated into a string:
CREATE PROC spTodayMessage
(
@Day AS VARCHAR(9)
)
AS
BEGIN
PRINT 'Today is ' + @Day
END
After the code is executed to create the stored procedure it can be used to generate a message as shown below:
EXEC spTodayMessage 'Wednesday'
Now that you know the basics of concatenation in SQL the next part of this series will show you how to create useful stored procedures which will construct and execute dynamic SQL statements.
You can learn more about this topic on the following Wise Owl courses:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.