maqskywalker
asked on
sql server 2008 query
Hi experts,
I'm using Sql Server 2008 with SSMS.
I have a table that looks like this:
This is the script to create this table:
Now I want to write a query that creates a column to my table. The column I'm adding is called Test Column. This column concatenates DepartmentID a dash and then DepartmentName.
I'm also creating a DepartmentID value of 0 and adding it to the DepartmentID column and this value should contain the word All as the value in the TestColum.
So my desired result should look like this drawing which I drew in Excel.
The red items in the drawing are what I want to add.
Query 1
So I have this query:
SELECT 0 AS DepartmentID
UNION
SELECT m.DepartmentID
FROM [TestDatabase].[dbo].[MyTe stTable1] m
When I run it I get this:
Query 2
So now I just want to add the Concatenated column called TestColumn to Query 1.
So i have this query:
SELECT 0 AS DepartmentID
UNION
SELECT m.DepartmentID
,Cast(m.DepartmentID As varchar) + ' - ' + m.DepartmentName As TestColumn
FROM [TestDatabase].[dbo].[MyTe stTable1] m
But this query gives me this error:
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
Anyone know how I can fix Query 2 so it gives my desired result set as shown in the image above?
I'm using Sql Server 2008 with SSMS.
I have a table that looks like this:
This is the script to create this table:
CREATE TABLE [dbo].[MyTestTable1](
[DepartmentID] [int] NULL,
[DepartmentName] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[MyTestTable1] ([DepartmentID], [DepartmentName]) VALUES (1, N'Accounting')
INSERT [dbo].[MyTestTable1] ([DepartmentID], [DepartmentName]) VALUES (2, N'Human Resources')
INSERT [dbo].[MyTestTable1] ([DepartmentID], [DepartmentName]) VALUES (3, N'Finance')
INSERT [dbo].[MyTestTable1] ([DepartmentID], [DepartmentName]) VALUES (4, N'Marketing')
Now I want to write a query that creates a column to my table. The column I'm adding is called Test Column. This column concatenates DepartmentID a dash and then DepartmentName.
I'm also creating a DepartmentID value of 0 and adding it to the DepartmentID column and this value should contain the word All as the value in the TestColum.
So my desired result should look like this drawing which I drew in Excel.
The red items in the drawing are what I want to add.
Query 1
So I have this query:
SELECT 0 AS DepartmentID
UNION
SELECT m.DepartmentID
FROM [TestDatabase].[dbo].[MyTe
When I run it I get this:
Query 2
So now I just want to add the Concatenated column called TestColumn to Query 1.
So i have this query:
SELECT 0 AS DepartmentID
UNION
SELECT m.DepartmentID
,Cast(m.DepartmentID As varchar) + ' - ' + m.DepartmentName As TestColumn
FROM [TestDatabase].[dbo].[MyTe
But this query gives me this error:
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
Anyone know how I can fix Query 2 so it gives my desired result set as shown in the image above?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SELECT 0 AS DepartmentID, NULL
UNION
SELECT m.DepartmentID
,Cast(m.DepartmentID As varchar) + ' - ' + m.DepartmentName As TestColumn
FROM [TestDatabase].[dbo].[MyTe