I have the following data set - there is more to it than whats below, I just made it easier to read and highlight my problem!
SELECT LEFT(actv.ProjID, 4) AS proj, actv.Activity, actv.TotalExpensesLB, actv.PADM
FROM dbo.xtbl_MERActv actv
WHERE LEFT(actv.projID,4) = @.project OR actv.PADM = @.PADM
What I want to do is have the user enter a 4 digit number (@.project) which will correspond to LEFT(actv.ProjID, 4). The way it is now, if the user enters a 4 digit number, no records are returned. If the user enters a 6 digit number ( the real length of the projID), then it runs correctly and I get the records I want.
I have tried to use the alias 'proj' in the where statement, but I get an error message that it is an invalid column name.
Where am I going wrong?
Thanks in advance!
You cannot use a column alias in the where clause, you just have to duplicate the calculation.
I cannot see anything wrong with what you are trying to do. My only guess is that perhaps there is a datatype problem. Are both @.project and actv.projID the same datatype?
Also, what version of SQL server is this?
|||Its SQL 2000, and I haven't actually set the @.project as any data type - could that be the problem? The actv.projID is char.
Actually, I may not have been clear in the problem. When running the data set, it will work fine, but when I try to preview the report, and enter a 4 digit @.project, I get a blank report with no error.
Thanks for the info on not using column alias!
|||Ah, that's a different problem.
I'm not really sure what could be causing a blank report when your dataset returns data.
What is the datatype of the parameter that you are using to hold the 4-digit number?
Also, when you said that entering a six digit number worked, was that without any modification to the dataset?
|||Sorry for that confusion about the problem.
The parameter is string, as is the projId.
It seems that using either 4 or 6 digit for @.project will give me results in the viewer, but still blank preview
Using the following statement
WHERE (LEFT(actv.ProjID, 4) = @.project)
|||I cannot think of a case where the left 4 characters of a string* would be equal to a six character string...
Actually, I take that back. If you have ANSI_NULLS set to OFF (not recommended) and both of the sides were NULL, then it could, but then you should be getting the same results for both entries, which is to say all rows. I guess I'm stumped.
*string meaning varchar, char, nvarchar, or nchar
|||It doesn't make sense to me at all - I figured there was something really obvious that I had forgotten...I'll take a new look at it on Monday, with fresh eyes, and maybe see my error.
Thanks for your help!