SQL converting backup size columns - sql-server

So I have this part of SQL script which I need to convert the two columns into 2 decimal point.
convert (numeric(10,2),
ROUND(SUM(msdb.dbo.backupset.compressed_backup_size)*8/1024, 0))
as 'Compressed_Backup_Size in MB' ,
SUM(msdb.dbo.backupset.backup_size/1024)/1024
as 'Backup_Size in GB'
My question is how do I get the two backup size columns to show the right value, I tried the conversion but the result it's still 894512.00 instead of 89.45 MB.. I have looked everywhere and they telling me to convert the numeric but still doesn't work for me. Help please

You can try this query for getting size in gb
select convert(decimal(18,3),(sum(backup_size))/1024/1024/1024) as SizeinGB
from msdb.dbo.backupset

Related

Pandas read_sql changing large number IDs when reading

I transferred an Oracle database to SQL Server and all seems to have went well. The various ID columns are large numbers so I had to use Decimal as they were too large for BigInt.
I am now trying to read the data using pandas.read_sql using pyodbc connection with ODBC Driver 17 for SQL Server. df = pandas.read_sql("SELECT * FROM table1"),con)
The numbers are coming out as float64 and when I try to print them our use them in SQL statements they come out in scientific notation and when I try to use '{:.0f}'.format(df.loc[i,'Id']) It turns several numbers into the same number such as 90300111000003078520832. It is like precision is lost when it goes to scientific notation.
I also tried pd.options.display.float_format = '{:.0f}'.format before the read_sql but this did not help.
Clearly I must be doing something wrong as the Ids in the database are correct.
Any help is appreciated Thanks
pandas' read_sql method has an option named coerce_float which defaults to True and it …
Attempts to convert values of non-string, non-numeric objects (like decimal.Decimal) to floating point, useful for SQL result sets.
However, in your case it is not useful, so simply specify coerce_float=False.
I've had this problem too, especially working with long ids: read_sql works fine for the primary key, but not for other columns (like the retweeted_status_id from Twitter API calls). Setting coerce_float to false does nothing for me, so instead I cast retweeted_status_id to a character format in my sql query.
Using psql, I do:
df = pandas.read_sql("SELECT *, Id::text FROM table1"),con)
But in SQL server it'd be something like
df = pandas.read_sql("SELECT *, CONVERT(text, Id) FROM table1"),con)
or
df = pandas.read_sql("SELECT *, CAST(Id AS varchar) FROM table1"),con)
Obviously there's a cost here if you're asking to cast many rows, and a more efficient option might be to pull from SQL server without using pandas (as a nested list or JSON or something else) which will also preserve your long integer formats.

Convert varchar dd-mm-yyyy to yyyy-mm-dd date field in SQL Server 2008

hopefully the title describes what I'm trying to do.
I have a varchar field in a SQL Server 2008 table that contains text dates in the format dd-mm-yyyy (e.g., 31-12-2009). I am trying to use CONVERT to convert it to a DATE field. I was successful in converting a similar varchar field in the same table using the following:
SELECT DISTINCT(CONVERT(DATE, MYDATEFIELD1, 103)) AS [CONV_MYDATEFIELD1] FROM MYTABLE;
But when I apply the same to MYDATEFIELD2, which appears to have the same type of data values as MYDATEFIELD1, it fails with the following error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
I've tried sorting and using LIKE to try to find any characters that might prevent the conversion but I haven't been able to pinpoint anything.
Any help will be greatly appreciated. Thanks!
You may have some invalid dates (e.g. 30-02-2009), try to find them splitting the characters and validating the day and the months, assuring that the days correspond to the month and the month is in the range 01 - 12.
If you can't find which value is causing the conversion error then use a cursor to go through all the records individually and use TRY CATCH to find which record(s) cause the conversion error. You could use a PRINT statement in the CATCH block to identify the records that are erroring.
Find your bad dates with the following:
SET DATEFORMAT dmy;
select MYDATEFIELD1, isdate(MYDATEFIELD1)
from MYDATEFIELD1
I figured out the issue that was causing the CONVERT to fail but I'm not sure of the best way to select an answer (veritable stack noob) so, any help on that would be appreciated. Here are the major steps I took to find the issue:
I used MIN and MAX SUBSTRING to identify that the component parts of the
varchar field were correct (i.e., the 1st two digits min=01 max=31,
middle two min=01 max=12)
I used DISTINCT SUBSTRING to identify that all of the date separators were consistent (i.e., all dashes).
I used MAX(LEN) to determine that my varchar "date" field was 12 characters (vs. the 10 characters I was expecting).
I used CONVERT(VARBINARY, MYDATEFIELD2) to determine what was actually stored in the string.
The last step revealed that the field contained line feeds (00A). I opened the source text file in notepad++, clicked View -> Show Symbol -> Show All Characters and I could see the LF at the end of each line.
So now I'm modifying the DTSX package (fixed width text) to include an extra field for the linefeed that I can drop afterwards. Now that I know what the intended format of the date fields is, I'll try to import them as DT_DATE vs DT_STR. I'm not exactly sure how to specify the correct date style 105 at import (thanks #Panagiotis Kanavos) but I'll figure it out.
Whew! What a learning experience! :D
Thanks to everyone who helped - and if you can give advice on the best way to select the best answer it will be greatly appreciated.

Issue with datatype Money in SQL SERVER vs string

I have a spreadsheet that gets all values loaded into SQL Server. One of the fields in the spreadsheet happens to be money. Now in order for everything to be displayed correcctly - i added a field in my tbl with Money as DataType.
When i read the value from spreadsheet I pretty much store it as a String, such as this... "94259.4". When it get's inserted in sql server it looks like this "94259.4000". Is there a way for me to basically get rid of the 0's in the sql server value when I grab it from DB - because the issue I'm running across is that - even though these two values are the same - because they are both compared as Strings - it thinks that there not the same values.
I'm foreseeing another issue when the value might look like this...94,259.40 I think what might work is limiting the numbers to 2 after the period. So as long as I select the value from Server with this format 94,259.40 - I thin I should be okay.
EDIT:
For Column = 1 To 34
Select Case Column
Case 1 'Field 1
If Not ([String].IsNullOrEmpty(CStr(excel.Cells(Row, Column).Value)) Or CStr(excel.Cells(Row, Column).Value) = "") Then
strField1 = CStr(excel.Cells(Row, Column).Value)
End If
Case 2 'Field 2
' and so on
I go through each field and store the value as a string. Then I compare it against the DB and see if there is a record that has the same values. The only field in my way is the Money field.
You can use the Format() to compare strings, or even Float For example:
Declare #YourTable table (value money)
Insert Into #YourTable values
(94259.4000),
(94259.4500),
(94259.0000)
Select Original = value
,AsFloat = cast(value as float)
,Formatted = format(value,'0.####')
From #YourTable
Returns
Original AsFloat Formatted
94259.40 94259.4 94259.4
94259.45 94259.45 94259.45
94259.00 94259 94259
I should note that Format() has some great functionality, but it is NOT known for its performance
The core issue is that string data is being used to represent numeric information, hence the problems comparing "123.400" to "123.4" and getting mismatches. They should mismatch. They're strings.
The solution is to store the data in the spreadsheet in its proper form - numeric, and then select a proper format for the database - which is NOT the "Money" datatype (insert shudders and visions of vultures circling overhead). Otherwise, you are going to have an expanding kluge of conversions between types as you go back and forth between two improperly designed solutions, and finding more and more edge cases that "don't quite work," and require more special cases...and so on.

Error converting Varchar to Decimal SQL Server

I have a staging table loaded with data from a SAS dataset containing 5M records. All the columns are varchar. I am trying to convert a couple of columns to decimal(32,10). But it generates an error. I tried cast, I tried convert and even splitting the data up before and after decimal - same result.
I looked at the IsNumeric flag of the column and there are 0 records <> 1 meaning the data is numeric.
case
when wtd_count = '.' THEN NULL
when wtd_count = '' THEN NULL
else convert(decimal(32, 10), wtd_count)
end
Error:
Msg 8114, Level 16, State 5, Line 99
Error converting data type varchar to numeric.
So I'm wondering what else I can do to convert the data to decimal? Any idea?
Any help will greatly be appreciated.
If you are in SQL Server 2012 and above try to use try_parse or try_convert
ISNUMERIC is not reliable for what you're doing. It will flag values with things like monetary symbols and commas in them as valid.
It seems quite likely that there is some non-numeric data present. TRY_CONVERT or TRY_PARSE are your friend here. As an FYI, SQL Server version 11.0.x is SQL Server 2012, so you should be able to use these.
I also find it hard to believe that converting to numeric works, but not decimal. I can find no information that suggests the actual implementation of these two data types is different, and as such neither should work.
I would do some more in depth analysis of your data to make sure it looks like you're expecting it to.
After read your case statement i suppose that you have coma separated values. I'm pretty sure that you should use: CONVERT(DECIMAL(32,10),REPLACE(wtd_count,',','.'))

sum(DATALENGTH) returning an "Arithmetic overflow" error

I am new to SQL Server so please accept my apologies if my question seems too easy. I tried finding a solution, but so far I couldn't see anything that I could use in my query.
I am trying to find length of the biggest columns in a table and I'm using the following query.
SELECT
SUM(DATALENGTH([INSDetails])) as [INSDetails]
FROM
dbo.Contractors
The table Contractors is slightly over 8GB and has over 30mln rows. INSDetails column is varchar(2048)
The query above works perfectly well for all the other columns of all the other tables in my database, but when I run it on Contractors table it returns an error
Msg 8115, Level 16, State 2, Line 26
Arithmetic overflow error converting expression to data type int.
I understand that this error message appears when you try to convert a value of a certain datatype to another datatype, but that value is too large for the second datatype.
Could you please help me to rewrite the query or suggest alternative approach to get the output?
I read that someone suggested using CAST AS big int to solve this issue, but I'm not sure how it can be incorporated in my query.
Any suggestions will be appreciated. Thank you in advance.
select sum(cast(datalength([INSDetails]) as bigint))

Resources