Solved

t-sql query

Posted on 2014-01-23
1
253 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
1 Comment
 
LVL 69

Accepted Solution

by:
ScottPletcher 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now