I have a column loaded into SQL Server with these nvarchar values:
ColumnName
==========
6.19e+014
.....
6.19e+014
Now, what would be the easiest way to convert this value into numbers again.
Kindly suggest. Thanks!
declare #String varchar(25)='6.19e+014'
Select cast(#String as float)
Returns 619000000000000
This will cast to FLOAT without issue:
SELECT CAST('6.19e+014' AS FLOAT)
If what you really want is the specific value that got converted to scientific notation in the first place, you'll have to go back in the process to before it got converted and fix that.
Also, try this
Select convert(numeric(15,0),ltrim(rtrim(str(column_name,15))))
from table_name
Related
I have my date which looks something like this.
I want to get sum of ITEM_QTY which has varchar datatype currently and tried following ways after typecasting but none of them worked.
SELECT SUM(TRY_CAST(ITEM_QTY as bigint))
FROM sales219
Result:
SELECT SUM(CAST(ITEM_QTY as bigint))
FROM sales219
Output:
Error converting data type varchar to bigint.
SELECT SUM(convert(bigint,ITEM_QTY))
FROM sales219
Output:
Error converting data type varchar to bigint.
SELECT SUM(try_convert(bigint,ITEM_QTY))
FROM sales219
Output:
Any suggestion is appreciated.
Thanks!
You will want to remove the double quotes from your data. In the mean time try this:
Select sum(try_convert(bigint,replace([ITEM_QTY],'"',''))) from YourTable
Or rather, fix your importing process to stop putting the quotes there to begin with.
I am not so familiar with using convert in SQL so thats why i am stuck with the following situation:
The column has a nvarchar(max) and i would like to convert it into decimal (18,2). But there are some rows that consist a "full" amount (see red box). For all values with the full amount i would like to have it as 1222,00
When creating a SQL view I got this error:
Error converting data type nvarchar to numeric.
How can i still convert this column into decimal?
Many thanks
From your pictures it seems to me like you are using SQL-Server. If so your problem is having , as decimal point instead of . It has nothing to do with having whole and decimal numbers in your data.
So you should replace it before converting data.
SELECT CONVERT(decimal(18,2), REPLACE(ColumnName,',','.')) FROM TableName
DB<>fiddle
I can't seem to find an answer to this anywhere --- I want to convert a datetime in SQL to the excel serial number.
I'm essentially looking for the DATEVALUE function from excel but for use in SQL
Any ideas on how to do this? thanks
Assuming the desired date is 2016-05-25
Select DateDiff(DD,'1899-12-30','2016-05-25')
Returns
42515
If you want the time portion as well
Declare #Date datetime = '2016-05-25 20:00'
Select DateDiff(DD,'1899-12-30',#Date)+(DateDiff(SS,cast(#Date as Date),#Date)/86400.0)
Returns
42515.8333333
I ran into this issue and found the most elegant solution to be the following (as others have mentioned, adding the +2 is essential due to the differences between SQL and Excel dates):
Assuming the "Column" is a DateTimeOffset:
SELECT CAST(CAST(COLUMN_TO_BE_CONVERTED as datetime)+2 as float) as EXCEL_DATE_FLOAT
If the column is not already a DateTimeOffset and is just a DateTime, you would not need the double cast; you'd just need something like:
SELECT CAST(COLUMN_TO_BE_CONVERTED+2 as float) as EXCEL_DATE_FLOAT
The resultant float value in either case is what excel recognizes as the date and time and you can easily extract what you need from there. Upon manually verifying the results in excel, I confirmed that the serial numbers matched the date and time exactly as I expected.
You just need to convert your datetime value to int and add 1:
SELECT CONVERT(INT,YourDate) + 1
FROM dbo.SomeTable;
With collation: SQL_Latin1_General_CP1_CI_AS
you should use
Select DateDiff(DD,'18991230','20160525')
Returns
42515
You could replace '20160525' for getdate() or you date field
I am using Sybase and I am doing a select which returns me a column called "iftype", but its type is int and I need to convert into varchar. When I try to do the select without the convert function I get this error:
Error code 257, SQL state 37000: Implicit conversion from datatype 'VARCHAR' to 'INT' is not allowed. Use the CONVERT function to run this query.
I dont know how to implement the function CONVERT. Can anyone help me, please ?
Use the convert function.
SELECT CONVERT(varchar(10), field_name) FROM table_name
Use the STR function:
SELECT STR(field_name) FROM table_name
Arguments
float_expression
Is an expression of approximate numeric (float) data type with a decimal point.
length
Is the total length. This includes decimal point, sign, digits, and spaces. The default is 10.
decimal
Is the number of places to the right of the decimal point. decimal must be less than or equal to 16. If decimal is more than 16 then the result is truncated to sixteen places to the right of the decimal point.
source: https://msdn.microsoft.com/en-us/library/ms189527.aspx
You can use CAST function:
SELECT CAST(your_column_name AS varchar(10)) FROM your_table_name
Actually you don't need to use STR Or Convert. Just select 'xxx'+LTRIM(ColumnName) does the job.
Possibly, LTRIM uses Convert or STR under the hood.
LTRIM also removes need for providing length. It seems to be working for integer or float without worry of truncation.
SELECT LTRIM(ColumnName) FROM TableName
also, LTRIM is better than STR as
SELECT STR(1234567890.123)
gives 1234567890
whereas
SELECT LTRIM(1234567890.123)
gives 1234567890.123
SELECT Cast(Cast([field_name] AS BIGINT) AS NVARCHAR(255))
FROM table_name
CONVERT(DATA_TYPE , Your_Column) is the syntax for CONVERT method in SQL. From this convert function we can convert the data of the Column which is on the right side of the comma (,) to the data type in the left side of the comma (,) Please see below example.
SELECT CONVERT (VARCHAR(10), ColumnName) FROM TableName
Try the following on MSSQL 2005:
select convert(char(2), 123)
The key here is that char(2) is too small to accept the value '123'. I would expect to see a truncation error here, but instead the value "*" is returned.
Update:
A few of the answers showed how to cast in a way that will cause an error. That's not really what I need. We have lots of code that uses a specific field that used to be declared char(2) but has since been changed to int. My goal here is to make sure that code that hasn't been converted will fall over if it encounters data that it can't handle. So we can go fix it.
Interestingly, dsolimano notes that changing the above type to nchar causes the expected error, and Justin Niessner notes that both of these are by design. Strange inconsistency given that nchar is for Unicode support, hey?
From the answers I have here, it appears that I sadly can't make the server throw the error for existing code.
Microsoft's convert/cast page seems to indicate that you can cast to nchar(2) if you want to get an error instead of * or truncation, and indeed that is what I get:
SELECT CAST(123 AS NCHAR(2))
Msg 8115, Level 16, State 2, Line 3
Arithmetic overflow error converting expression to data type nvarchar.
Scroll down the following MSDN Page:
CAST and CONVERT (Transact-SQL)
Until you get to the heading Truncating and Rounding Results
You'll see that the behavior you're seeing is the defined behavior.
If you want the truncation to occur so that you get only two digits of the number, then you can try:
select cast(convert(varchar(10), 123) as char(2))
Just cast it to a variable length string before you cast it to a string that is too small.
Select Cast( Cast( 123 as varchar(10) ) As char(2) )
SQL Server will truncate strings without a problem. So you could convert the value to a varchar first, and then cast it to char(2):
select convert(char(2), cast(123 as varchar(128))
Depends what you mean by "prevent" this from happening? What would you like to happen instead? Nothing?
DECLARE #number INT
SET #number = 123
IF(#number < 100) SELECT CONVERT(char(2), #number)