Can anyone help me to fix this issue please?
I'm getting an error while trying to convert this to BIGINT:
SELECT CONVERT(BIGINT, '10472553255347451137')
Thanks
Well, your number simply is to large for a bigint. A bigint's maximum is 9223372036854775807, see the documentation.
And as 9223372036854775807 < 10472553255347451137, there's no way to convert 10472553255347451137 to a bigint.
And BTW, do not post images of error messages. Paste the text into the post's text.
If you query for the length of the number, it's 20. Therefore, you may use Numeric(20, 0) to store the value.
Length
SELECT len('10472553255347451137')
20
Select converted value
SELECT CONVERT(numeric(20, 0), '10472553255347451137')
10472553255347451137
Related
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.
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
I am very new to programming. I am learning SQL and am using SS2008 as a starting point. Firstly,thank you for your help! It makes the process of learning for someone like myself a lot easier.
I have read other threads but their code seems a lot more complicated.
I am going to keep this as simple as possible: I am querying a database wanting to perform a sum function on a particular column.
At present, the meta data of the column is of type varchar. I understand, to be able to use the sum function, the column in question needs to be of int.
To convert the column accordingly, I thought I could use either the cast/convert functions but incur errors when doing so:
/**
CAST:Charge_Pct FROM VARCHAR -> INT
**/
SELECT CAST(Charge_Pct AS NUMERIC(7,7))
FROM [Main].[Share_Class_Charges]
WHERE CHARGE_PCT <> 'None'
-- Arithmetic overflow error converting varchar to data type numeric.
/**
CONVERT: Charge_Pct FROM VARCHAR -> INT
**/
SELECT CONVERT(NUMERIC(7,7),Charge_Pct)
FROM [Main].[Share_Class_Charges]
WHERE CHARGE_PCT <> 'None'
-- Error converting data type varchar to numeric.
I am confused by where I am going wrong and what the error messages are saying. Please could someone help me by explaining what the error messages means and what needs to be done to correct the code?
Many thanks,
Al.
The NUMERIC(7,7) type describes a number with 0 digits before the decimal and 7 after. This means that if you try to cast a VARCHAR as small as 10.12 you will get an overflow error.
Try this query instead:
SELECT CAST(Charge_Pct AS NUMERIC(5,2))
FROM [Main].[Share_Class_Charges]
WHERE CHARGE_PCT <> 'None'
This will attempt to convert the Charge_Pct column (which I assume holds percentages) into a NUMERIC type consisting of 5 total numbers with 2 coming after the decimal place.
I have a field with datatype VARCHAR and MAX length.
However, I just want to print only the first 2000 characters from that field.
I cannot use VARCHAR(2000) as the datatype for the field upon table creation since there are a records exceeding this length and as you might all know, this results in a 'String or binary data would be truncated' error.
What I'm doing is sending an email report which may get too lengthy because of that field so I just want to output the first 2000 characters.
Any help would be greatly appreciated! Thanks in advance!
you can use substring.
select SUBSTRING(yourcolumnname,0,2000) from yourtablename
I have Two SQL Query Both Return
select round(convert(float,'24367.723'),2)
Result:24367.72
Second:
select convert(varchar(20),round(convert(float,'24367.723'),2))
Result:24367.7
Why the Second Query Return exclude the last digit after converting to varchar
Thanks in Advance
By not specifying a style parameter to the convert function you get the default style (0).
i.e. it is equivalent to doing
select convert(varchar(20),round(convert(float,'24367.723'),2), 0)
The default style for converting from float to varchar displays a maximum of 6 digits.
When working with a float the STR() function usually gives better results according to MSDN as you've more control.
E.g.
select str(convert(float,'24367.723'),8, 2)
Dont use floats, use exact numberics. Something like this
convert(varchar(20), convert(numeric(20,2), '24367.72'))