Think you have incorrect single quotes (') which are causing the issue. See the code snippet for correction.
Main Topics
Browse All TopicsDeclare @sqlquery1 float, @sqlquery2 float
set @sqlquery1='
select top 1 logspaceused from dbo.DBLogspaceHist
where server_id = 3 and database_name = ''bistage''
order by LogDT desc'
set @sqlquery2='
select top 1 logspaceused from
(select top 2 logspaceused,logdt from dbo.DBLogspaceHist
where server_id = 3 and database_name = ''bistage''
order by LogDT desc )as test order by LogDT asc'
select (round(convert(float,@sqlq
round(convert(float,@sqlqu
----
Declare @sqlquery1 real, @sqlquery2 real
set @sqlquery1='
select top 1 round(convert(float,logspa
from dbo.EnterpriseDBLogspaceHi
where server_id = 3 and database_name = ''bistage''
order by LogDT desc'
set @sqlquery2='
select top 1 round(convert(float,logspa
(select top 2 round(convert(float,logspa
from dbo.EnterpriseDBLogspaceHi
where server_id = 3 and database_name = ''bistage''
order by LogDT desc )as test order by LogDT asc'
select @SQLquery1-@sqlquery2
i am actually trying to get compare last 2 value to get % of growth in log file
(top1 - top2)
getting below error
Error converting data type varchar to float.
i tried as above 2 ways......
please let me know if u have any idea..
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
http://sequelserver.blogsp
Declare @sqlquery1 float, @sqlquery2 float
declare @Sql1 nvarchar(1000), @Sql2 nvarchar(1000)
set @sql='
select top 1 @sqlquery1 = logspaceused from dbo.DBLogspaceHist
where server_id = 3 and database_name = ''bistage''
order by LogDT desc'
set @sqlquery2='
select top 1 @sqlquery1= logspaceused from
(select top 2 logspaceused,logdt from dbo.DBLogspaceHist
where server_id = 3 and database_name = ''bistage''
order by LogDT desc )as test order by LogDT asc'
exec sp_ExecuteSQL @Sql1, N'@sqlquery1 float output', @sqlquery1 output
exec sp_ExecuteSQL @Sql2, N'@sqlquery2 float output', @sqlquery2 output
select (round(convert(float,@sqlq
round(convert(float,@sqlqu
thanks guys for the reply
ralmada: ur write no need of @sqlvariables the query worked fine
how does the query look if i try using @sqlvariables??
i have one more question here .. i have a list of logfilesize detalis from last few days in a table
i am actually trying to come with old data and get the percent growth of logfile ??
how can i do this?
did u get me??
thanks..
Thaks for the reply..
i tried as mwvisa1's reply
Declare @sqlquery1 float, @sqlquery2 float
set @sqlquery1= (select top 1 round(convert(float,logspa
from dbo.EnterpriseDBLogspaceHi
where server_id = 3 and database_name = 'bistage'
order by LogDT desc)
set @sqlquery2=(select top 1 round(convert(float,logspa
(select top 2 round(convert(float,logspa
from dbo.EnterpriseDBLogspaceHi
where server_id = 3 and database_name = 'bistage'
order by LogDT desc )as test order by LogDT asc)
select @SQLquery1-@sqlquery2
getting below error
Invalid column name 'logspaceused'.
for the line <<set @sqlquery2=(select top 1 round(convert(float,logspa
can u pls help in this too
i have one more question here .. i have a list of logfilesize detalis from last few days in a table
i am actually trying to come with old data and get the percent growth of logfile ??
how can i do this?
<<i am actually trying to get compare last 2 value to get % of growth in log file>>
<< have a list of logfilesize detalis from last few days in a table
i am actually trying to come with old data and get the percent growth of logfile>>
As ralmada originally suggested, you don't need to use variables, you can accomplish what you want in one query. For example, you could do something like the below.
Maybe you missed my post here -- http:#25072052. Please run this code and see as it should have what you need to get the prev_logspacedused. If the value is in a different table, then will need more information, but showed you an example with same table name in post I showed above. Please review and adjust table and column names according to your actual environment.
thanks for the reply
my table structure is as below
LogDT Serverid Databasename Logsize LogSizeUsed
09-08-10 3 bistage 8986.31 1.6613
09-08-10 3 bistage 8986.31 1.6629
09-08-10 3 bistage 8986.31 1.67
09-08-11 3 bistage 8986.31 1.81
09-08-11 3 bistage 8986.31 1.98
i want to compare last 5 or 6 values and get the %used
Here there will be no column like 'prev_logspacedused'prev_l
it same logspacedused column
how can i get 'prev_logspacedused value do i need to use some self join and get .......pls let me know
getting the error because there is no column 'prev_logspacedused'
thanks a lot....... it looks fine now
yep i am using 2005
and logDt has timestamp
if u dont mind can u briefly explain me the query
mainly
select *,
row_number() over (partition by server_id, database_name order by LogDT,
LogSpaceUsed) rn
from dbo.EnterpriseDBLogspaceHi
select a.server_id, a.database_name, a.LogDT,a.logsize,
a.LogSpaceUsed AS LogspaceUsed
, cast(case
-- avoid any divide by zero errors
when coalesce(b.LogSpaceUsed, 0) <> 0 ----------whats coalesce do here??
then (a.LogSpaceUsed - b.LogSpaceUsed) * 100.0 / b.LogSpaceUsed ---- b ? how does it take secondline here
end as decimal(10,4)) as PercentgeIncrrease
from cte a
left join cte b on a.rn = b.rn+1 -- cte b??
and a.server_id = b.server_id
and a.database_name = b.database_name
thanks .............
Ok, since LogDT has timestamps then you can get rid of the second sort in the OVER statement but will explain the rest.
;with cte as (
select *,
-- replace LogSizeUsed with column that correctly holds order per day
-- not needed at all if LogDT has timestamps
row_number() over (partition by serverid, databasename order by LogDT) rn
from DBLogspaceHist
)
The above portion of the query is a common table expression (CTE) (i.e., WITH ...) that allows you to create a virtual table of your data. Would be same as if you took the query inside the parenthesis and made it a view or a temp table. This gives added benefit over derived table as you can use it more than once in the outer query without a lot of ugly coding.
The row_number() function inside this CTE is used in conjunction with the analytical over() clause to sequence your rows and give you a nice numerical field you can depend on. In other words, I now that the row after row one has rn = 2, then rn = 3, etc.
The partition within the over() clause is the grouping. Without this, you would get the rows sequenced as they appeared in the table by the 'order by LogDT'. This is fine if you only have data from same serverid and database; however, the partition will treat each serverid and database combination as a new sequence, so you can have row 1 for serverid = 3 and database = 'bistage' and row 1 for serverid = 3 and database = 'test'.
For more information see this ref.: http://msdn.microsoft.com/
select a.LogSizeUsed AS LogspaceUsed
, cast(case
-- avoid any divide by zero errors
when coalesce(b.LogSizeUsed, 0) <> 0
Coalesce is like isnull() function. It will take the first non-null value between the columns result and literal 0. Table aliased with 'b' identifier is representing the data in the row previous to the one we are on. This is achieved in the join by 'a.rn = b.rn+1' which is same as 'b.rn = a.rn-1' so on row 1 the matching b would have to have a rn = 0. Since there is no rn = 0, the previous row is NULL. This is appropriate since your first record isn't increasing from anything so it should be NULL.
Consequently, what if there is a previous row, but the LogSizeUsed is 0. You will get a divide by 0 error. Therefore, I use coalesce(previous_value, 0) <> 0 to kill two birds with one stone as both NULL and 0 will be filtered out.
then (a.LogSizeUsed - b.LogSizeUsed) * 100.0 / b.LogSizeUsed
b.LogSizeUsed is from the previous record, see above comment on how.
end as decimal(10,4)) as PercentgeIncrrease
from cte a
left join cte b on a.rn = b.rn+1
This is the advantage of using a CTE that I declared above. I am self joining the CTE that we aliased as cte (could be called c, t, or whatever you want -- just happens to be cte here so don't let that confuse you) to itself so to keep things from being ambiguous, one is called a and the other b.
and a.serverid = b.serverid
and a.database_name = b.database_name
where a.server_id = 3 and a.database_name = 'bistage'
thanks a lot for the explanation .....i will have a look into it
can u pls look into my other SQLQuery issue i have in below link..
http://www.experts-exchang
Thanks....
Business Accounts
Answer for Membership
by: ralmadaPosted on 2009-08-11 at 10:57:03ID: 25071593
you are trying to use a float variable to store a string, hence the convertion error. Please give this a try:
(Please note that there's no need for the @sqlQuery variables)
Select allOpen in new window