1

Have can I execute below script in SQL

Select Year as ' years between ' +  @StartYear + '-' +  @EndYear + ' value ' 
from tblYears
where Year Between @StartYear and @EndYear
2
  • I believe Have is typo... It should be How. Isn't it?? Could you execute and see what error you are getting?? Commented Mar 8, 2012 at 15:21
  • Should either be [alias] = <expression> or <expression> AS [alias]. Not sure where you got the [alias] AS <expression> syntax you're using. Commented Mar 8, 2012 at 15:40

2 Answers 2

3

You have your as switched around. The name comes last

Select 'years between ' +  @StartYear + '-' +  @EndYear + ' value ' AS Year
from tblYears
where Year Between @StartYear and @EndYear

Now @StartYear and @EndYear are probably not char/varchar/nchar/nvarchar so you will need to convert them. I am going to assume here that your year is a 4 digit integer. You will need to tweak this if it is not.

Select 'years between ' +  convert(char(4), @StartYear) + '-' +  convert(char(4), @EndYear) + ' value ' AS Year
from tblYears
where Year Between @StartYear and @EndYear
Sign up to request clarification or add additional context in comments.

Comments

0

I believe what you want is something like this, where you are inserting the selected row's value into the final output?

SELECT CONVERT(CHAR(4), Year) + ' year is between ' 
    + CONVERT((CHAR(4),@StartYear) +'-'+ CONVERT((CHAR(4),@EndYear) + ' values.' 
FROM tblYears
WHERE Year BETWEEN @StartYear AND @EndYear

Otherwise, you do not even need the FROM or WHERE, as you will be printing the same thing over and over. Then, I would suggest:

SELECT ' years between ' + CONVERT((CHAR(4),@StartYear) + '-' 
    + CONVERT((CHAR(4),@EndYear) + ' value' 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.