Not able to cast in SQL Server - sql

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.

Related

sql server - error converting float to varchar

I have a float field, I need to convert it as varchar and I need to add a leading zero when it contains only one nummber(it can contains 1 or 2 numbers). This is my query:
SELECT FORMAT(((LTRIM(RTRIM(STR(floatField,2))))), '00')
but I get an error:data type for argument 1 is not valid. I know I can do this in a different way, but I want to understand what's wrong in this
format takes a float (among other things) and converts it to a varchar. Don't try to do its job for it:
SELECT FORMAT(myfloat, '00')
FROM mytable
DBFiddle Demo

SQL SERVER - Date conversion failed

I have a table in which AccessDate column is of type datetime.
And I am trying to do something like this:
SELECT 'AccessDate' UNION ALL SELECT AccessDate FROM table_name
I am trying to insert the header of the table "AccessDate" into the result of the query.
And this error shows up after executing:
Conversion failed when converting date and/or time from character string.
Can someone help me with this? Thank you.
Not surprising. You appear to want a date in a column where there is already a character. You need to convert the date to a string:
SELECT 'AccessDate' UNION ALL
SELECT CONVERT(VARCHAR(10), AccessDate, 121)
FROM table_name;
You can use whatever format you like. I prefer YYYY-MM-DD.

Add a negative symbol to a numeric value

Using SQL 2014 I need to append a negative sign to a list of numeric values. The data are dollar amounts with numerous places behind the decimal point. I did convert the data to numeric(15, 2)
Here is my select statement.
SELECT '-' + convert(15,2), MONEY from TABLE
I am getting the error: Arithmetic overflow error converting varchar to data type numeric.
I tried converting to varchar as well.
select '-' + CONVERT(varchar10), (convert(numeric(15, 2), MONEY)) from
TABLE
I get the same error as above. Any ideas how to accomplish this?
How about multiplying by -1 instead? Something like this:
SELECT -1 * convert(MONEY, 15.2) from TABLE
Your syntax isn't correct. I am guessing you want something like the above.
Or subtract from 0.
SELECT -convert(MONEY, 15.2) from TABLE
why not trying Cast Function
SELECT -1 * CAST(MONEY as Numeric(15.2)) from TABLE

Convert nvarchar scientific notation value into number in SQL Server 2014

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

Convert from varchar to float SqlServer

I have a column in varchar format and I would like to convert it to float. The values are like:
188234,62
188235
188235,43
188235,88
So, When I try to convert it I get an error:
Error converting data type varchar to float.
I thought that maybe some fields are not numeric and I checked with isnumeric() function, according to the query I concluded that all the values are numeric. Then I tried different queries to convert the field types:
Query1:
SELECT CarData.dbo.FinalData_OilLevel_new_send.KM
CASE WHEN ISNUMERIC(CarData.dbo.FinalData_OilLevel_new_send.KM) = 1
THEN CAST(CarData.dbo.FinalData_OilLevel_new_send.KM AS FLOAT)
ELSE NULL
END
FROM CarData.dbo.FinalData_OilLevel_new_send
Query2:
SELECT CASE ISNUMERIC(KM) WHEN 1 THEN CONVERT(float, KM)) ELSE null END from CarData.dbo.FinalData_OilLevel_new_send
However I always get the same error. Are there some other ways to convert varchar to float?
You have to use a dot as decimal separator:
CAST(REPLACE(CarData.dbo.FinalData_OilLevel_new_send.KM,',','.') AS FLOAT)
At first you need to Replace
The , present
try like this!
select cast(replace(val,',','') as float) from table
SEE DEMO