What is the equivalent of the function Digits in Snowflake? - function

What is the Equivalent of the function Digits in Snowflake?
Any help is appreciated.
Thank you

If this is digits() from DB2
The DIGITS function returns a character string representation of the absolute value of a number.
Then something like:
REPLACE(ABS(<yourcol>), '.', '')
Or:
ARRAY_TO_STRING(SPLIT(ABS(<yourcol>), '.'),'')
If you are also wanting the left-padded zeros there isn't a way to determine precision of a decimal in snowflake directly in your SQL, so you will have to be specific about how large your output string should be.
LPAD(REPLACE(ABS(<yourcol>), '.', ''), 10, '0')

Please try this:
SELECT TRY_TO_NUMBER('abcd');--NULL
SELECT TRY_TO_NUMBER('700000001020577');--700000001020577
SELECT TRY_TO_NUMBER('1234.56');--1234
SELECT TRY_TO_NUMBER('0.56');--0
SELECT TRY_TO_NUMBER('a12#$');--NULL
SELECT IFF(TRY_TO_NUMBER('1234') IS NULL,FALSE,TRUE) --TRUE
SELECT IFF(TRY_TO_NUMBER('abcd') IS NULL,FALSE,TRUE)--FALSE

Related

Get last two number in 2018

I want to trim JAN2018 and get last value like this =>JAN18. how I can ?
you can use Trim,concat and SUBSTR function
SELECT SUBSTR("Tutorial", 5, 3) AS ExtractString; it will return "ria"
For your case
SELECT concat( SUBSTR(Trim( ' JAN2018'),1,3), SUBSTR(Trim( ' JAN2018'),-2));
For more knowledge on Concat, Trim and SUBSTR
SELECT SUBSTRING("JAN2018", -2) AS endtwochar; //if you have one char then will return empty
SELECT right("JAN2018", 2) AS endtwochar; // if you have on char then you will one char
Both should give same result - decide based on output
if you need output JAN18 then try below
SELECT insert("JAN2018", 4,2, '') AS removetwochar;
Hope this query may help you:
SELECT RIGHT(columnname,2) FROM tablename;
Here is a demo
You can use SUBSTR()
SELECT SUBSTR('JAN2018',-2);
For your new requirement(an ugly way):
SELECT REPLACE('JAN2018','20','')
Give this a try, this is the query to find last two digit from date
select substr(to_char(sysdate),length(to_char(sysdate))-1,2) last_two from dual

MySQL - extract number from data field using SUBSTRING

I'd like to extract the number between NUMBER and ;. So far I can extract the data up to the number, but I don't want anything after the number. e.g.,
SELECT
SUBSTRING(field, LOCATE('NUMBER=', rrule) + 7)
FROM table
Data field:
DATA:PASS=X12;NUMBER=331;FIELD=1
DATA:PASS=X12;NUMBER=2;FOO=BAR;FIELD=1
Desired Output:
331
2
You can use a combination of SUBSTRING_INDEX functions:
SELECT
SUBSTRING_INDEX(
SUBSTRING_INDEX(field, 'NUMBER=', -1),
';',
1)
FROM
tablename
Please see an example fiddle here.
The inner SUBSTRING_INDEX will return everything after the NUMBER= string, while the second will return everything before the ; returned by the inner function.

How to remove commas of integer from MY SQL select query

I am using a mySQL statement that returns me average of values as comma separated integer.
Eg : 2,109. But I want my output to be plain integer like 2109. Please help me on this.
You can use something like this:
SELECT REPLACE(fieldname, ',', '')
FROM ...
Or if type of fieldname is integer use this query
SELECT REPLACE(CONCAT(fieldname), ',', '')
FROM ...

MySQL: Using sum on a varchar with comma?

I have a column, called net_amount, it contains values like 244,98. Its a varchar column. When I try to sum it using the sum function, it only sums the 244 and skipts the decimal places. I tried casting it to decimal like this:
select cast(net_amount as decimal) from mytable
This skips the decimal places as well ... any idea what might be wrong?
Thanks!
You could use REPLACE to replace comma to dot:
SELECT REPLACE('244,98', ',', '.') * 1
or you can use CAST like this:
cast(REPLACE(net_amount, ',', '.') as decimal(8,2))

How to select one text column, which string length >0?

Is there any function like "strlen" in mysql?
Mysql does have a length function to return the length of a string.
so you could do something like.
select * from some_table where length(some_string) > 0;
You can do it with using LENGTH keyword
SELECT * FROM table WHERE LENGTH (name) > 150;
How to select one text column, which string length >0 ?
You could compare to the empty string:
SELECT yourcolumn
FROM yourtable
WHERE yourcolumn <> ''
The following query would do the job for you.
select length(your_text_column) from sample_table where length(some_column) > 0
For anyone who comes across this answer in Google, keep this in mind:
Actually, CHAR_LENGTH() should be a better choice. For multi-byte charsets LENGTH() will give you the number of bytes the string occupies, while CHAR_LENGTH() will return the number of characters
MySQL - How to select data by string length