maycil
maycil

Reputation: 764

How can I show a parameter in sql string value

Have can I execute below script in SQL

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

Upvotes: 1

Views: 88

Answers (2)

Justin Pihony
Justin Pihony

Reputation: 67075

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' 

Upvotes: 0

cadrell0
cadrell0

Reputation: 17307

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

Upvotes: 3

Related Questions