How do I convert 9E-5 Scientific notation to INT (0.00009) - tsql

How do I convert 9E-5 Scientific notation to INT (0.00009) in SQL, the majority of records are all INT values with this rogue record I'd like to handle it in the load stage.

SELECT convert(numeric(18,18),convert(float(53),'9E-5'))

Related

Snowflake - Convert varchar to numeric

I have a column(field1) defined as varchar in snowflake. It is storing both string and numbers(ex values: US15876, 1.106336965E9). How can I convert the numeric values to display something like 1106336965, without losing the columns that is storing string values or null values. I am trying try_to_numeric(field1), but this is eliminating the record with string values and showing them as null. Any help is appreciated.
So try_to_number is the way to have numbers, and nulls for non-number without errors. But if you want to keep the strings, you actually have to convert your newly create number, back to text (or variant), otherwise it cannot be in the same column, so nothing is gained:
select column1
,try_to_number(column1) as_num
,nvl(as_num::text, column1) as why_not_both
from values
('US15876'),
('1.106336965E9'),
('1.106336965'),
('1106336965');
COLUMN1
AS_NUM
WHY_NOT_BOTH
US15876
null
US15876
1106336965
1,106,336,965
1106336965
1.106336965
1
1
1106336965
1,106,336,965
1106336965

How to extract first digit from a Integer in transformer stage in IBM DataStage?

I have an integer field coming and I want to extract the first digit from the field, how can I do it. I cannot cast the field since the data is coming from a dataset, is there a way to extract first digit from the transformer stage in IBM datastage?
Example:
Input:
ABC = 1234
Output: 1
Can anyone please help me with the same?
Thanks!
Use a transformer, define a stage variable as varchar and use this formula to get the substring
ABC[1,1]
Alternatively you can also convert your numeric value by using the DecimalToString
You CAN convert to string within the context of your expression, and back again if the result needs to be an integer.
AsInteger(Left(ln_jn_ENCNTR_DTL.CCH,1)
This solution has used implicit conversion from integer to string. It assumes that the value of CCH is always an integer.
I would say- if ABC has type int, you can define a stage variable of type char having length 1.
then you need to convert Number to string first.And use Left function to extract the first char.
Left(DecimalToString(ABC),1).
If you are getting ABC as string, you can directly apply left function.
You can first define a stage variable (name say SV) of varchar type (to convert input integer column into varchar) :
Stage variable definition
Now assign the input integer column to stage variable SV and derive output integer column as AsInteger(SV[1,1]) : Column definition
i.e. input integer => (Type conversion to varchar) Stage variable => Substring[1,1] and Substring Conversion to Integer using AsInteger.
DecimalToString is an implicit conversion, so all you need is the Left() function. Left(MyString,1)

Conversion failed when converting the varchar value 'F01' to data type int

It appears that [t_l_unit] being a varchar is conflicting with the filters in my Where clause. Can I fix this with a Cast of some sort?
Going by the title of your post, your are trying to convert the value "F01" to an INT?
Obviously this is not possible because of the "F" in the string.
What you can do is use the TRY_CAST function which will convert the value to an INT if it can and will return NULL otherwise
SELECT val1, va2, etc
FROM myTable
WHERE TRY_CAST([t_l_unit] as INT) = 12
The query didn't come through, but you should be able to just do:
CAST([t_l_unit] AS INT)
to use it as a number in your where clause.

Explicit type conversion in postgreSQL

I am joining the two tables using the query below:
update campaign_items
set last_modified = evt.event_time
from (
select max(event_time) event_time
,result
from events
where request = '/campaignitem/add'
group by result
) evt
where evt.result = campaign_items.id
where the result column is of character varying type and the id is of integer type
But the data in the result column contains digits(i.e. 12345)
How would I run this query with converting the type of the result(character) into id
(integer)
Well you don't need to because postgresql will do implicit type conversion in this situation. For example, you can try
select ' 12 ' = 12
You will see that it returns true even though there is extra whitespace in the string version. Nevertheless, if you need explicit conversion.
where evt.result::int = campaign_items.id
According to your comment you have values like convRepeatDelay, these obviously cannot be converted to int. What you should then do is convert your int to char!!
where evt.result = campaign_items.id::char
There are several solutions. You can use the cast operator :: to cast a value from a given type into another type:
WHERE evt.result::int = campaign_items.id
You can also use the CAST function, which is more portable:
WHERE CAST(evt.result AS int) = campaign_items.id
Note that to improve performances, you can add an index on the casting operation (note the mandatory double parentheses), but then you have to use GROUP BY result::int instead of GROUP BY result to take advantage of the index:
CREATE INDEX i_events_result ON events_items ((result::int));
By the way the best option is maybe to change the result column type to int if you know that it will only contain integers ;-)

What is the numeric type equivalent in KnexJS (Postgres)?

I am creating a table using Knex.JS, and the table has a column for a currency value.
For example, here is the column amount:
knex.schema.createTable('payment', function(table) {
table.increments();
table.float('amount');
})
Currently, I am using the float type, but I'd like to use the numeric type. What is the equivalent of the numeric type in Knex.JS?
Thanks.
For currency decimal is best match, so your code may look like:
knex.schema.createTable('payment', function(table) {
table.increments();
table.decimal('amount',14,2); // e.g. 14 positions, 2 for the cents
});
see http://knexjs.org/#Schema-decimal