Solved

SQL Server 2005 SQL Error

Posted on 2009-04-01
3
181 Views
Last Modified: 2012-05-06
The below SQL is producing the following error:

Msg 207, Level 16, State 1, Line 21
Invalid column name 'StartDate'.
Msg 207, Level 16, State 1, Line 22
Invalid column name 'ClosingDateFinal'.
Msg 207, Level 16, State 1, Line 22
Invalid column name 'ClosingDateF2'.

Any ideas?
CASE WHEN [dtStart] IS NULL THEN NULL 
      ELSE CAST(MONTH([dtStart]) AS varchar(2)) + '/1/' + 
               CAST(YEAR([dtStart]) AS varchar(4)) + ' 5:00 PM' END AS StartDate,
CAST([StartDate] AS datetime) AS StartDate2,
CASE WHEN StartDate Is Null THEN Null ELSE [StartDate2] END AS Dt3

Open in new window

0
Comment
Question by:CipherIS
[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
3 Comments
 
LVL 27

Expert Comment

by:Chris Luttrell
ID: 24043598
I am assuming this is the select column list in a query.  startdate looks to be a column alias and you can not use it on another line.  You will have to reuse the entire logic for startdate or rework the query some, but there is not enough detail here to help with that.
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 24043618
StartDate and StartDate2 are not columns in your table, they are aliases in your select statement.  They cannot be referenced in the statement itself unless you place the select into a derived table.  From there, the statements can reference by alias.

example:
select CASE WHEN StartDate Is Null THEN Null ELSE [StartDate2] END AS Dt3
from
(select CAST([StartDate] AS datetime) AS StartDate2
from 
(select CASE WHEN [dtStart] IS NULL THEN NULL 
      ELSE CAST(MONTH([dtStart]) AS varchar(2)) + '/1/' + 
               CAST(YEAR([dtStart]) AS varchar(4)) + ' 5:00 PM' END AS StartDate
from TableA) a)b)c

Open in new window

0
 
LVL 8

Expert Comment

by:vinurajr
ID: 24043780
IS THE CODE U SEND IS CORRECT.. WHAT IS UR REQUIREMENT...?
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

626 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