0

Does anyone know how to print the output of an SQL Query once it has been executed in SQL Server?

select count(eo.equal_opps_id) as 'Age 40 - 49'
from dbo.app_equal_opps eo, dbo.app_status s
where eo.date_of_birth > DateAdd(yy,-49,getDate())
and eo.date_of_birth < DateAdd(yy,-39,getDate())
and eo.application_id = s.status_id
and s.job_reference_number = '33211016'
and s.submitted = 1    

Above shows an SQL query, however, once it is executed I would like to see the actual date values that are created at the following sections

DateAdd(yy,-49,getDate())
DateAdd(yy,-39,getDate())

In other words, once the query is executed, can I print the SQL Query output so that it shows something like this

select count(eo.equal_opps_id) as 'Age 40 - 49'
from dbo.app_equal_opps eo, dbo.app_status s
where eo.date_of_birth > '1962-05-04 13:00:00.000'
and eo.date_of_birth < '1972-05-04 13:00:00.000'
and eo.application_id = s.status_id
and s.job_reference_number = '33211016'
and s.submitted = 1

As always, any feedback would be greatly appreciated.

Thanks Everyone.

1
  • It doesn't expand out those function calls in a textual form - the "generated" form you've shown will never exist. Commented May 4, 2011 at 14:49

2 Answers 2

2

Add the two dates to your select list:

select 
   count(eo.equal_opps_id) as 'Age 40 - 49' 
  ,DateAdd(yy,-49,getDate()) as LesserDate
  ,DateAdd(yy,-39,getDate()) as GreaterDate
from 
  dbo.app_equal_opps eo, dbo.app_status s 
where 
  eo.date_of_birth > DateAdd(yy,-49,getDate()) 
  and eo.date_of_birth < DateAdd(yy,-39,getDate()) 
  and eo.application_id = s.status_id 
  and s.job_reference_number = '33211016' 
  and s.submitted = 1 
Sign up to request clarification or add additional context in comments.

Comments

1

The only way i can think is messy. You would build the sql up as a string then execute it using the exec in built function.

declare @sql as varchar(max)

select @sql = '
select count(eo.equal_opps_id) as ''Age 40 - 49''
from dbo.app_equal_opps eo, dbo.app_status s
where eo.date_of_birth > ' + ( select cast(DateAdd(yy,-49,getDate()) as varchar(100)) ) + '
and eo.date_of_birth < ' + ( select cast(DateAdd(yy,-39,getDate()) as varchar(100)) ) + '
and eo.application_id = s.status_id
and s.job_reference_number = ''33211016''
and s.submitted = 1'

print @sql
exec(@sql)   

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.