I'm trying an 0, before every column in my sql
select TemporaryStock * '0,'+ cast( VAT as varchar(50)) from Market
i get error Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to float.
You don't need to cast... you just need to move the decimal.
select TemporaryStock * (VAT * 0.01) from Market
If you want every value to be like 0.###### then you can use:
select TemporaryStock * convert(decimal(38,37),'0.' + convert(varchar,VAT))
If you want to add '0' before the VAT then use CONCAT(args1, args2) method:
select CONCAT('0',VAT) from ExpressMarket;
Note: Both the arguments should be of string type.
Or else you can refer the following to know more about how to concatenate and aliasing it.
MySQL select with CONCAT condition
Related
I have been given a table that is in NVARCHAR format. I cannot change this table. However, I need to get a SUM value. Neither the CAST, nor CONVERT functions work. Can anyone please provide some suggestions?
SELECT SUM(CONVERT(INT, [Product]))
FROM mytable
SELECT SUM(CAST([Product] AS INT))
FROM mytable
Both result in:
Msg 245, Level 16, State 1, Line 300
Conversion failed when converting the nvarchar value 'WP 4.3-CU AB CDE WINDOW ABA5000RSW' to data type int.
'WP 4.3-CU AB CDE WINDOW ABA5000RSW' will NEVER convert to an int.
If 2012+, you can use try_convert(int,[Product])
This will return NULL for the strings which fail the conversion.
Try checking for numeric products?
SELECT
SUM(CONVERT(INT, [Product]))
FROM
mytable
WHERE
ISNUMERIC([Product]) = 1;
I have a calculated column Total_Count which is derived from couple of float data type columns with results like 0, 101, NULL (in Total_Count column).
Instead of showing NULL I want to show - (hyphen character) in the Total_Count column.
If I use COALESCE(Total_Count, '-') then I am getting the following error:
Msg 8114, Level 16, State 5, Line 62
Error converting data type varchar to float.
Please help!
try this in select
SELECT COALESCE(CAST(Total_Count AS VARCHAR), '-')
How do you write a SQL query to find only the rows that has a float currency value like $15.34 and NOT round currency value like 15 in a nvarchar field.
Assuming you have a mix of numeric and non-numeric, this should work to return all decimal values that are not whole dollar amounts:
Select * from tablename
where colname like '%.%' --Has a decimal (as in original query)
and colname not like '%.00' --Does not end with 00
It is as simple as
Select * from tablename where columnname = '15.34'
I would strip the $ out, and check if it evaluates to a numeric or not, and use a modulo to be sure a remainder remains when divided by 1.
DECLARE #TEST TABLE (columnname NVARCHAR(15))
INSERT INTO #TEST
SELECT '$15.34' UNION
SELECT 'ZERO' UNION
SELECT '$123.00'
SELECT *, CONVERT(MONEY,REPLACE(columnname,'$',''))
FROM #TEST
WHERE ISNUMERIC(REPLACE(columnname,'$',''))=1
AND CONVERT(MONEY,REPLACE(columnname,'$','')) % 1 != 0
You can use like
Select * from Yourtablename where Yourcolumnname like '$15.%'
Two things:
First, you want to find the rows having the $ in them.
WHERE LOCATE('$',columname) <> 0
Second, you want to find the rows where the rest of the value in the column is a floating point number.
AND CONVERT(REPLACE(columnname,'$',''),DECIMAL(10,2)) <> 0
That CONVERT() <>0 pattern works because MySQL silently returns zero when you try to convert a nonnumeric value to a number.
I have a table that looks like this:
id datatype name value
0 nvarchar(255) myName 'Paul'
1 int age '30'
2 float(53) Custom1 0.5
3 float(53) Custom2 1.3
4 float(53) Custom3 2.7
I am wondering if it is possible to do something like the following where I cast the order by as a float - I know this is incorrect syntax but wondering if this can be done.
SELECT datatype, name, value FROM myTable
ORDER BY (float)name
Thanks in advance.
To convert datatypes in SQL you can use CAST or CONVERT:
SELECT CAST('123' AS FLOAT), CONVERT(FLOAT, '123')
But if your name field contains a value that cannot be converted to a valid float, you'll get an error.
e.g.
SELECT CAST('NotAValidFloat' AS FLOAT)
will give you:
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to float.
I'm looking for an elegant way to convert a field of type varchar, with variable data in it, to a data type which can be used for mathematical operations sample data from the field
(excluding quotes)
''
'abc'
'23'
'23.2'
The method should work for all, and for the first & second values should return 0, and not throw an SQL Server error..
Try this:
SELECT CASE WHEN IsNumeric(YourColumn) = 0 THEN
0
ELSE
CAST(YourColumn AS decimal(18, 2))
END
You have to adjust the destination data type, I have chosen decimal(18, 2) for demonstration.
I know this is a long-dead thread, but I recently stumbled upon it from a Google search and had a thought. It is less elegant than a CASE statement, but it is an alternative.
SELECT
COALESCE(CAST(NULLIF(ISNUMERIC(LEFT(MyColumn, PATINDEX('% %', MyColumn + ' ') - 1)), 1) AS MONEY), LEFT(MyColumn, PATINDEX('% %', MyColumn + ' ') - 1))
FROM
myTable
or you could do:
Select COALESCE(CAST(NULLIF(ISNUMERIC(MyColumn), 1) AS MONEY), MyColumn)
FROM
myTable
The top version would see "2 5" as just 2, the bottom one would see it as a text field.
SELECT CASE IsNumeric(mycol) WHEN 1 THEN CAST(mycol AS FLOAT) ELSE 0 END
FROM mytable
If you'd like to convert it, you should use UPDATE instead of SELECT
UPDATE Table
SET Col1 = CAST(Col1 As Decimal(18,2))
COALESCE is a great option for this: Find more information here. It evaluates the arguments in order and returns the current value of the first expression that initially does not evaluate to NULL.
ISNUMERIC returns 0 or 1 depending on if the value being evaluated could be considered one of the SQL 'number' or 'numeric' types. e.g. int, bigint, money..
NULLIF essentially finds the value you specify and if it matches it replaces it with a NULL value.
CAST Simply changes a data type to another in this example to MONEY
As you can see, if you break the below down using this information its quite an elegant solution I think?
COALESCE(CAST(NULLIF(ISNUMERIC(COL1), 1) AS MONEY), COL1)