Run query against all databases on server containing specific string in name If I have a query such as:

SELECT convert(varchar, DateTime, 100) AS "Date and Time"
,Text2 AS Area
,Substring(PText7, 1,Charindex(':', PText7)-1) AS Username
FROM the_database.dbo.AlgViewENU AS data
WHERE PText6 LIKE '%BypIn%' AND DateTime = (SELECT MAX(DateTime) FROM the_database.dbo.AlgViewENU WHERE Text1 = data.Text1 AND PText6 LIKE 'Set%')


How would I run the same query against all the databases in the server containing "the" (for example) in the name? I haven't had any luck with sp_MSforeachdb
You may try to generate a dynamic statement. 

Basic example:

-- Declarations
DECLARE @stm nvarchar(max)
DECLARE @err int
SET @stm = N''

-- Dymanic statement
SELECT @stm = @stm + CONCAT(N'SELECT * FROM [', [name] , N'].sys.tables ')
FROM sys.databases
WHERE [name] LIKE '%the%'

-- Execution
EXEC @err = sp_executesql @stm


Example with your statement:

-- Declarations
DECLARE @stm nvarchar(max)
DECLARE @err int
SET @stm = N''

-- Dymanic statement
SELECT @stm = 
    @stm + 
    CONCAT(
    N'SELECT convert(varchar, DateTime, 100) AS "Date and Time", Text2 AS Area, Substring(PText7, 1,Charindex('':'', PText7)-1) AS Username FROM [', 
    [name] , 
    N'].dbo.AlgViewENU AS data
    WHERE PText6 LIKE ''%BypIn%'' AND [DateTime] = (SELECT MAX(DateTime) FROM [',
    [name] , 
    N'].dbo.AlgViewENU WHERE Text1 = data.Text1 AND PText6 LIKE ''Set%''); '
    )
FROM sys.databases
WHERE [name] LIKE '%the%'

-- Execution
PRINT @stm
EXEC @err = sp_executesql @stm