How to get database size in SQL Server Enterprise edition - sql

I'm getting an error with regard of this query
SELECT
DB.name,
SUM(CASE WHEN type = 0 THEN MF.size * 8 / 1024.0 ELSE 0 END) AS DataFileSizeMB,
SUM(CASE WHEN type = 1 THEN MF.size * 8 / 1024.0 ELSE 0 END) AS LogFileSizeMB,
SUM(mf.size* 8 / 1024.0) AS TotalSizeMB,
SYSDATETIME() AS Datelogged
FROM
sys.master_files MF
JOIN
sys.databases DB ON DB.database_id = MF.database_id
WHERE
DB.source_database_id IS NULL
GROUP BY
DB.name
ORDER BY
Datelogged DESC
I get an error:
Arithmetic overflow error converting expression to data type int
I'm running this on SQL Server 2016 Enterprise edition. I've tried to run this on Standard and other lower editions and it'll work. Can someone tell what's wrong with this code and what should be the correct one. I do not own this code, I've just copied it from another site.
Action taken : I've try to convert this into bigint data type but still no avail.
Thank you very much

instead of MF.size * 8 use MF.size * 8.0

Related

Case in Select Query has syntax error

I want to return a boolean according to a condition on one of the column of my table. I tested it in SQL Server 2014 and it works, but I have no experience in Access.
This is the query I have at the moment, using Access 2007.
SELECT (CASE WHEN Type = 'C' THEN 1 ELSE 0 END) AS EstContrat
FROM Historique_EnTete
Type has the Text type.
I have translated the error message to :
"Syntax error (missing operator) in the expression << (CASE WHEN Type = 'C' THEN 1 ELSE 0 END) >>"
What am I missing?
In access you have IIF
SELECT IIF(Type = 'C', 1, 0 ) AS EstContrat
FROM Historique_EnTete
As Lamak say you also have SWITCH

How to use case when in SQL Server 2008

I want to use case after where clause in SQL Server 2008; how can I use it like
select *
from MPR
where
case when #min >= 0
then MainComponent_Id = #mainc and SubComponent_Id = #subcomp
else MainComponent_Id = #mainc and SubComponent_Id = #subcomp and MinorComponent_Id = #minorc
end
end
I am using a stored procedure, and in my stored procedure if minorcomponent_id value is 0 then it will add 1 one else 2 one will work.
I had tried hard but its giving error in near case.
How to resolve it?
Thanks
You don't need to use a CASE statement. You can simplify logic as follows:
select
*
from
MPR
where
MainComponent_Id = #mainc AND SubComponent_Id = #subcomp
AND (#min >= 0 OR MinorComponent_Id = #minorc)

Msg 8115, Level 16, State 2, Line 1: Arithmetic overflow error converting %ls to data type %ls

I'm running this query on SQL Server 2005 and getting error. Could not found the root cause of this issue.
Please help to solve this issue. Is this issue Version specific and coming only with SQL Server 2005 not with 2008?
SELECT "AAA1_before_Final13"."AAA1" AS "level0key",
"AAA1_before_Final13"."Reporting_Month_Year" AS "levelkey",
"AAA1_before_Final13"."Reporting_Period" AS "Reporting_Period",
"Charge_Type14"."U_INV_CHG_TYPE" AS "level1key",
MIN(1) AS "rowCount",
SUM("AAA1_before_Final13"."Lag_Days") AS "Lag_Days",
SUM("AAA1_before_Final13"."Invoice_Count") AS "Invoice_Count"
FROM
(SELECT "AAA111"."Division_Name" AS "Division_Name",
"AAA111"."Original_FSC" AS "Original_FSC",
"AAA111"."FSC_Class" AS "FSC_Class",
"AAA111"."FSC_Name" AS "FSC_Name",
"AAA111"."Billing_Area_Category" AS "Billing_Area_Category",
"AAA111"."Billing_Area" AS "Billing_Area",
"dateDim12"."Reporting_Period" AS "Reporting_Period",
"dateDim12"."Reporting_Month_Year" AS "Reporting_Month_Year",
"AAA111"."AAA1" AS "AAA1",
"AAA111"."Lower_Range" AS "Lower_Range",
"AAA111"."Upper_Range" AS "Upper_Range",
SUM("AAA111"."Lag_Days") AS "Lag_Days",
SUM("AAA111"."Inv_Count") AS "Invoice_Count",
"AAA111"."GRP" AS "GRP"
FROM
(SELECT "idxAnalyzer"."dbo"."Date_Dim"."Reporting_Period" AS "Reporting_Period",
"idxAnalyzer"."dbo"."Date_Dim"."Reporting_Month_Year" AS "Reporting_Month_Year",
"idxAnalyzer"."dbo"."Date_Dim"."Reporting_Year" AS "Reporting_Year",
"idxAnalyzer"."dbo"."Date_Dim"."Deposite_Days" AS "Deposite_Day",
"idxAnalyzer"."dbo"."Date_Dim"."Deposite_Days" AS "Deposite_Day1",
"idxAnalyzer"."dbo"."Date_Dim"."Deposite_Days" AS "Deposite_Day_Grp"
FROM "idxAnalyzer"."dbo"."Date_Dim"
WHERE "idxAnalyzer"."dbo"."Date_Dim"."Reporting_Year" = 2013
AND "Date_Dim"."Day_of_Month" = 1
) "dateDim12"
LEFT OUTER JOIN
(SELECT "AAA1_Lag_Days"."Reporting_Period" AS "Reporting_Period",
"AAA1_Lag_Days"."Division_Name" AS "Division_Name",
"AAA1_Lag_Days"."FSC_Class" AS "FSC_Class",
"AAA1_Lag_Days"."Original_FSC" AS "Original_FSC",
"AAA1_Lag_Days"."FSC_Name" AS "FSC_Name",
"AAA1_Lag_Days"."Billing_Area_Category" AS "Billing_Area_Category",
"AAA1_Lag_Days"."Billing_Area" AS "Billing_Area",
SUM("AAA1_Lag_Days"."Lag_Days") AS "Lag_Days",
SUM("AAA1_Lag_Days"."Inv_Count") AS "Inv_Count",
"AAA1_Lag_Days"."Lower_Range" AS "Lower_Range",
"AAA1_Lag_Days"."Upper_Range" AS "Upper_Range",
'AAA1' AS "AAA1",
"AAA1_Lag_Days"."GRP" AS "GRP"
FROM
(SELECT "AAA1_Lag_Days"."Reporting_Period" AS "Reporting_Period",
"AAA1_Lag_Days"."Division_Name" AS "Division_Name",
"AAA1_Lag_Days"."Original_FSC" AS "Original_FSC",
"AAA1_Lag_Days"."FSC_Class" AS "FSC_Class",
"AAA1_Lag_Days"."FSC_Name" AS "FSC_Name",
"AAA1_Lag_Days"."Billing_Area" AS "Billing_Area",
"AAA1_Lag_Days"."Billing_Area_Category" AS "Billing_Area_Category",
"AAA1_Lag_Days"."Reporting_Period_Year" AS "Reporting_Period_Year",
"AAA1_Lag_Days"."Lag_Days" AS "Lag_Days",
"AAA1_Lag_Days"."Inv_Count" AS "Inv_Count",
"AAA1_Lag_Days"."Lower_Range" AS "Lower_Range",
"AAA1_Lag_Days"."Upper_Range" AS "Upper_Range",
"AAA1_Lag_Days"."Group_Num" AS "GRP"
FROM "idxAnalyzer"."dbo"."AAA1_Lag_Days_View" "AAA1_Lag_Days"
) "AAA1_Lag_Days"
WHERE (4 = 0
OR 4 = "AAA1_Lag_Days"."GRP")
AND "AAA1_Lag_Days"."Reporting_Period_Year" = 2013
AND "AAA1_Lag_Days"."Lower_Range" = 16.49
GROUP BY "AAA1_Lag_Days"."Reporting_Period",
"AAA1_Lag_Days"."Division_Name",
"AAA1_Lag_Days"."FSC_Class",
"AAA1_Lag_Days"."Original_FSC",
"AAA1_Lag_Days"."FSC_Name",
"AAA1_Lag_Days"."Billing_Area_Category",
"AAA1_Lag_Days"."Billing_Area",
"AAA1_Lag_Days"."Lower_Range",
"AAA1_Lag_Days"."Upper_Range",
"AAA1_Lag_Days"."GRP"
) "AAA111"
ON "dateDim12"."Reporting_Period" = "AAA111"."Reporting_Period"
GROUP BY "AAA111"."Division_Name",
"AAA111"."Original_FSC",
"AAA111"."FSC_Class",
"AAA111"."FSC_Name",
"AAA111"."Billing_Area_Category",
"AAA111"."Billing_Area",
"dateDim12"."Reporting_Period",
"dateDim12"."Reporting_Month_Year",
"AAA111"."AAA1",
"AAA111"."Lower_Range",
"AAA111"."Upper_Range",
"AAA111"."GRP"
) "AAA1_before_Final13",
"idxAnalyzer"."dbo"."U_INVOICE_HEADER" "Charge_Type14"
WHERE CAST ("AAA1_before_Final13"."GRP" AS BIGINT) = CAST ("Charge_Type14"."GRP" AS BIGINT)
GROUP BY "AAA1_before_Final13"."AAA1",
"AAA1_before_Final13"."Reporting_Month_Year",
"AAA1_before_Final13"."Reporting_Period",
"Charge_Type14"."U_INV_CHG_TYPE"

Latitude and Longitude conversion error

I'm trying to execute a specific query in my database, but occurs the following error
Table San_Filial
Filial_Id Name lat lon
2 A -19.926131 -43.924373
3 B -19.952192 -43.938789
4 C -19.939626 -43.924541
5 D -19.95529 -43.92953
6 E -19.9099 -43.93124
7 F -19.926191 -43.946067
9 G -19.97125 -43.96622
14 H -19.89038 -43.921734
17 I -19.88838 -43.93059
19 J -19.94305 -43.94093
Query
SELECT *
FROM San_Filial
WHERE San_Filial.Credenciada_Id IN (2,3,4,5,6,7,9,14,17)
AND ACOS(COS(RADIANS(ltrim(San_Filial.lat)))
* COS(RADIANS(convert(float, -19.926131)))
* COS(RADIANS(ltrim(San_Filial.lon))
- RADIANS(convert(float, -43.924373)))
+ SIN(RADIANS(ltrim(San_Filial.lat)))
* SIN(RADIANS(convert(float, -19.926131)))) * 6380 < 5.0
Error
Mesage 8114, Level 16, State 5, Line 1
Error converting data type varchar to float.
Someone can help me ?
Start by seeing if you have any data that is not in a correct format:
select *
from san_filia1
wHERE San_Filial.Credenciada_Id IN (2,3,4,5,6,7,9,14,17) and
(isnumeric(lat) = 0 or isnumeric(long) = 0)
From this, you'll see what is causing the problem and then you can fix it.
The correct fix is probably along these lines:
select *
from (select sf.*,
(case when isnumeric(lat) then cast(lat as float) end) as latf,
(case when isnumeric(long) then cast(long as float) end) as longf
from san_filial sf
) sf
where . . . -- use Latf and Longf instead of lat and long
You need to do the conversion inside a case statement to guarantee that it works as expected. SQL does not guarantee the ordering of statements and a filter might be applied after the calculation.

Weird SQL Behavior, why is this query returning nothing?

Assume there is a table named "myTable" with three columns:
{**ID**(PK, int, not null),
**X**(PK, int, not null),
**Name**(nvarchar(256), not null)}.
Let {4, 1, аккаунт} be a record on the table.
select * from myTable as t
where t.ID=4
AND t.X = 1
AND ( t.Name = N'аккаунт' )
select * from myTable as t
where t.ID=4
AND t.X = 1
AND ( t.Name LIKE N'%аккаунт%' )
The first query return the record, however, the second does not? Why?
Systems where this issues are experienced:
*Windows XP - Professional - Version 2002 - SP3
Server Collation: Latin1_General_CI_AS
Version: 9.00.3073.00
Level: SP2
Edition: Developer Edition
Sever Collation: SQL_Latin1_General_CP1_CI_AS
Version: 9.00.3054.00
Level: SP2
Edition: Enterprise Edition
Results:
SELECT SERVERPROPERTY('SQLCharSetName')
iso_1
Using OSQL.exe
0x30043A043A04300443043D04420400000000000000000000000000000000
0x3F3F3F3F3F3F3F0000000000000000000000000000000000000000000000
0x253F3F3F3F3F3F3F25000000000000000000000000000000000000000000
SELECT CAST(name AS BINARY),
CAST(N'аккаунт' AS BINARY),
CAST(N'%аккаунт%' AS BINARY)
FROM myTable t
WHERE t.ID = 4
AND t.X = 1
CAST(name AS BINARY)
0x30043A043A04300443043D04420400000000000000000000000000000000
CAST(N'аккаунт' AS BINARY)
0x3F3F3F3F3F3F3F0000000000000000000000000000000000000000000000
CAST(N'%аккаунт%' AS BINARY)
0x253F3F3F3F3F3F3F25000000000000000000000000000000000000000000
Could you please post the result of the following query:
SELECT CAST(name AS BINARY),
CAST(N'аккаунт' AS BINARY),
CAST(N'%аккаунт%' AS BINARY)
FROM myTable t
WHERE t.ID = 4
AND t.X = 1
This will help to narrow the problem down.
UPDATE:
As I can see from the results of your query, you have a problem with encoding.
The Cyrillic literals from your string constants are being converted to the question marks (0x3F).
Unfortunately, I cannot reproduce this behavior with Management Studio on my test server.
I reckon there is some problem with OS settings, as Cyrillic characters most probably don't even reach SQL Server.
Could you please answer three more questions:
What OS are you using (version, language, MUI if any)
What does this query return:
SELECT SERVERPROPERTY('SQLCharSetName')
Connect to your server using osql.exe and issue this query:
SELECT CAST(name AS BINARY),
CAST(N'аккаунт' AS BINARY),
CAST(N'%аккаунт%' AS BINARY)
FROM myTable t
WHERE t.ID = 4
AND t.X = 1
GO
What does it return being run in osql.exe?
Both queries return the same result for me.
select * from myTable as t
where t.ID=4
AND t.X = 1
AND (t.Name = N'аккаунт')
Returns:
ID X Name
----------- ----------- ------------
4 1 аккаунт
And
select * from myTable as t
where t.ID=4
AND t.X = 1
AND (t.Name LIKE N'%аккаунт%')
Returns:
ID X Name
----------- ----------- ------------
4 1 аккаунт
(1 row(s) affected)
My version of SQL Server is:
Microsoft SQL Server 2005 - 9.00.3077.00 (Intel X86)
Dec 17 2008 15:19:45
Copyright (c) 1988-2005 Microsoft Corporation
Express Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
My collation is set to: SQL_Latin1_General_CP1_CI_AS
My results for Quassnoi:
0x30043A043A04300443043D04420400000000000000000000000000000000
0x30043A043A04300443043D04420400000000000000000000000000000000
0x250030043A043A04300443043D0442042500000000000000000000000000
(1 row(s) affected)
Alright, after a great deal of research, I found it is indeed a problem found on the following versions of SQL Server 2005:
Windows XP - Professional - Version 2002 - SP3
Version: 9.00.3073.00
Level: SP2
Edition: Developer Edition
Version: 9.00.3054.00
Level: SP2
Edition: Enterprise Edition
..may be other versions as well.
FIX: Upgrade to SP3.