Solved

t-sql query

Posted on 2014-01-23
1
264 Views
Last Modified: 2014-01-23
Hi experts,

I'm using sql server 2008 R2.

I'm using the Employees table from the Northwind database

So I have 2 queries.

Query 1 is a working example.
Query 2 is the one that i'm having trouble with and need help with.



Query 1

This is the code for it:

-- Query 1
DECLARE @FromDate char(30), @ToDate char(30)
SET @FromDate = '02/01/1955'
SET @ToDate = '09/30/1955'
  
SELECT [EmployeeID]
      ,[LastName]
      ,[FirstName]
      ,[Title]
      ,[BirthDate]
FROM [Northwind].[dbo].[Employees] e
WHERE
e.[BirthDate] Between RTrim(@FromDate) And RTrim(@ToDate) 

Open in new window


When I run this query I get this:

query 1 result set
Query 2

In this query my parameter called @WhatDay holds the name of the Birthdate column of the Employees table.

This is the code I have:

-- Query 2 
DECLARE @FromDate char(30), @ToDate char(30), @WhatDay char(50)

SET @FromDate = '02/01/1955'
SET @ToDate = '09/30/1955'
SET @WhatDay = 'BirthDate'
  
SELECT [EmployeeID]
      ,[LastName]
      ,[FirstName]
      ,[Title]
      ,[BirthDate]
FROM [Northwind].[dbo].[Employees] e
WHERE
e.RTrim(@WhatDay) Between RTrim(@FromDate) And RTrim(@ToDate)     

Open in new window



In Sql Server management studio this is a screen shot of the error I'm getting:

my error message

How do I fix query 2?
0
Comment
Question by:maqskywalker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 39805184
You can't use a variable to contain a column name in SQL Server.

If you want the column name to vary, you have to build a string and then execute it dynamically, i.e., you must use dynamic SQL.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question