Concat column of one table to each column of another table

0

I have dates in one table:

2021-10-01
2021-10-02

And codes in another table:

0101
0102

I want my results to look like so:

2021-10-01 - 0101
2021-10-02 - 0101
2021-10-01 - 0102
2021-10-01 - 0102

Which translates to adding all codes to each date into a final table where each date has a code.

Date table query:

Select DATE from Table1

Code table query:

Select CODE from Table2
sql sql-server tsql
2021-11-24 06:43:45
1

2

Use a cross join:

SELECT CONVERT(varchar(10), t1.date, 120) + ' - ' + t2.code
FROM Table1 t1
CROSS JOIN Table2 t2
ORDER BY t2.code, t1.date;

Demo

2021-11-24 06:47:17

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................