I have a column with datatype of varchar. I would like to replace all the values that are not numeric with NULL.
So for example my column can contain a value of MIGB_MGW but also 1352. The current expression I am using with Derived Column Transformation Editor is:
(DT_I4)kbup == (DT_I4)kbup ? 1 : 0
But of course this replaces all the values I want to keep with 1. What expression would I use to keep the numeric values? (1352 in this example)
If you want a null of varchar type, you can use NULL(DT_STR). For a DT_I4 you can use NULL(DT_I4) etc.
You can then use (DT_I4)kbup in place of your 1 to return the original varchar value that you want to keep, converted to a DT_I4:
(DT_I4)kbup == (DT_I4)kbup ? (DT_I4)kbup : NULL(DT_I4)
You could just convert them with a Derived Column and then use the ignore failure option in the Error output.
Use NOT LIKE
SELECT CASE
WHEN col NOT LIKE '%[^0-9]%' THEN col
ELSE NULL
END as Only_Numeric
FROM (VALUES ('MIGB_MGW'),
('1352')) tc(col)
Result :
Only_Numeric
------------
NULL
1352
Another option if 2012+ is Try_Convert()
SELECT Try_Convert(float,col)
FROM (VALUES ('MIGB_MGW'),
('2.6e7'),
('2.6BMW'),
('1352')) tc(col)
Returns
NULL
26000000
NULL
1352
Related
how to show empty value for a int datatype in SQL?
I have a case statement on an int datatype column to show empty for the values less than or equal to 0.
case when [TotalValue] <= 0 Then ''
when [TotalValue] > 0 Then [TotalValue]
End as [TotalValue]
Right now, case statement is returning 0 for any values less than or equal to 0. I expect to have them as Empty. Having 0 instead of negative value is not a correct result.
How to convert the record to show only empty?
The problem of your code is that Then '' is automatically converted to int value, which happens to be 0 for empty strings (try select CAST('' as int) to check).The data type for ambiguously defined column (like yours) is determined from the data type precedence rules.
Unambiguously defining the data type of the column would resolve the issue.
I recommend trying to return NULL from the database, like this:
case when [TotalValue] <= 0 Then NULL
when [TotalValue] > 0 Then [TotalValue]
End as [TotalValue]
Most likely, your report engine will convert NULL to something like an empty string. In addition, you may be getting some benefits of ability to manipulate numeric values, if your report engine supports those (e.g. calculate average over selection).
Alternatively, try casting the values to string in SQL:
case when [TotalValue] <= 0 Then ''
when [TotalValue] > 0 Then CAST([TotalValue] as varchar)
End as [TotalValue]
I think the simplest construct is:
(case when TotalValue > 0 Then TotalValue
end) as TotalValue
You can always CAST the number to a VARCHAR (string) and then set it to an empty string when NULL:
ISNULL(CAST(TotalValue as varchar(10)),'') as TotalValue
Empty string is a concept that doesn't make sense for integer datatype.
Generally you should return the results to the application as integer datatype and use NULL for this and have your application display null as an empty string if desired.
If you do need to do this in SQL you are now dealing with strings rather than integers.
One way of converting to string and performing your desired formatting is with the FORMAT function.
SELECT FORMAT(TotalValue, '0;"";""')
FROM
(VALUES (1),
(0),
(-123),
(123456))T(TotalValue)
Returns
1
123456
I have the following tables
productinfo:
ID|productname|productarea|productcost|productid
sales:
ID|salesid|productid|
salesdata:
ID|productid|productname|salestotal
Where I am having trouble is: salesdata.salestotal is a varchar column and may have nulls
Comparing the salesdata.saletotal to the productinfo.productcost columns.
I can do a
cast(salesdata.saletotal as float(7)) > X
and it works. I would like to do is
cast(salesdata.saletotal as float(7)) > productinfo.productcost
where
sales.productid = salesdata.productid and
productinfo.productid = salesdata.productid
However when I do that I get an error:
Error when converting type varchar to float
I found this post which was similar but am unable to get any other columns with it. I can not change the current db structure.
You can add a IsNumeric() to your where clause to check that salesdata.saletotal can be parsed to float
SELECT
cast(salesdata.saletotal as float(7)) > productinfo.productcost
FROM Sales,Salesdata
WHERE
sales.productid = salesdata.productid and
productinfo.productid = salesdata.productid and
IsNumeric(salesdata.saletotal) =1
Or you can use Not Like (best than IsNumeric)
salesdata.saletotal NOT LIKE '%[^0-9]%'
if you are using sql server version 2012 and above, you can use try_cast() or try_parse()
try_cast(salesdata.saletotal as float)>productinfo.productcost
or
try_parse(salesdata.saletotal as float)>productinfo.productcost
Prior to 2012, I would use patindex('%[^0-9.-]%',salesdata.saletotal)=0 to determine if something is numeric, because isnumeric() is somewhat broken.
e.g. rextester: http://rextester.com/UZE48454
case when patindex('%[^0-9.-]%',salesdata.saletotal)>0
then null
when isnull(cast(salesdata.saletotal as float(7)),0.0)
> isnull(cast(productinfo.productcost as float(7)),0)
then 1
else 0
end
I am trying to generate a hive query which will take multiple numeric column names and check whether it is has numeric values. If the column has numeric values then the output should be (column name,true) else if the field has NULL or some string value the output should be (column name,false)
SELECT distinct (test_nr1,test_nr2) FROM test.abc WHERE (test_nr1,test_nr2) not like '%[^0-9]%';
SELECT distinct test_nr1,test_nr2 from test.abc limit 2;
test_nr1 test_nr2
NULL 81432269
NULL 88868060
the desired output should be :
test_nr1 false
test_nr2 true
Since test_nr1 is a decimal field and it has NULL values, it should output false.
Appreciate valuable suggestions.
You can use cast function. It returns NULL when the value can not not be cast to numeric.
For example:
select case when cast('23ccc' as double) is null then false else true end as IsNumber;
You're trying to use character class pattern matching syntax here, and it doesn't work in every SQL implementation IIRC, however, regexp matching works in most, if not all, SQL implementations.
Considering you're using hive, this should do it:
SELECT ('test_nr1', test_nr1 RLIKE '\d'), ('test_nr2', test_nr2 RLIKE '\d') FROM test.abc;
You should remember that regexp matching is very slow in SQL though.
I have a table with a field named MINIMUM_AGE. The values stored in this field are of type nvarchar:
17 years
54 years
N/A
65 years
I would like to apply a WHERE clause on the column to check for a certain age range. To do that I need to parse out the age from the field values.
So, I think I need to select the first two characters, then cast them into an integer. Also, some fields may not contain numbers for the first two characters. Some may simply be N/A. So, I will need to check for that before casting.
Can someone explain how to accomplish this?
Here is the SQL Fiddle that demonstrates the below query:
SELECT CASE
WHEN MINIMUM_AGE <> 'N/A'
THEN CAST(LEFT(MINIMUM_AGE, 2) AS int)
ELSE 0
END
FROM MyTable
Note: the CASE expression can only return one data type. So, in the example above if the MINIMUM_AGE is N/A then it returns 0.
If you would rather have it return null, then use the following:
SELECT CASE
WHEN MINIMUM_AGE <> 'N/A'
THEN CAST(LEFT(MINIMUM_AGE, 2) AS int)
END
FROM MyTable
I have the following query returns resulting me Null or Zero:
SELECT TOP 1 ISNULL([jul-12],0) FROM Table_tmp
WHERE ID = 123250838
but when I add a condition asking if the column value is zero causes overflow error:
SELECT TOP 1 ISNULL([jul-12],0) FROM Table_tmp
WHERE ID = 123250838
AND [jul-12] <> 0
The data type of the column is FLOAT
exec sp_help 'Table_tmp'
jun-12 float no 8 53 NULL yes (n/a) (n/a) NULL
I tried with the functions CONVERT () and CAST () but with the same result.
But when the value of the column [Jul-12] is nonzero, it works without errors. Why does this happen?
Try like below... may be it will help you...
SELECT TOP 1 ISNULL([jul-12],0) FROM Table_tmp
WHERE Str([jul-12], 25, 5) <> '0'
This is because of CAST () specification. The result data type will be as in the second parameter. To avoid this you can use COALESCE function which determine result data type by first parameter, also benefit of this function is that it's ANSI standard (instead of ISNULL).
you can look for this function here:
COALESCE MSDN
usage in your case is: COALESCE(jul-12],0) - this will return first not null value.
I just ran across this problem and solved it by throwing an "IS NOT NULL" clause into "WHERE" on the column that I was using cast()