Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

t-sql query

Posted on 2014-01-23
1
Medium Priority
?
270 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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

721 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