How to convert varchar to number [duplicate] - sql

This question already has answers here:
Want to convert from character format to number format with decimal
(2 answers)
Closed 6 years ago.
I have character string as '00000625710' I want it as number 6257.10 how can I do that . I tried as follow
select to_number('00000002511','999999999.99')from dual;
But didn't work . can any one help me in this . Thank you.

If you know that the least 2 characters are always decimal (more generally, that the structure of your input strings is fixed), you can simply divide:
select to_number('00000002511') / 100 from dual;

Related

Oracle SQL - TO_NUMBER RN Format_model [duplicate]

This question already has answers here:
How to convert Roman numerals into decimal numbers?
(4 answers)
Closed 4 months ago.
I want to convert some Roman numerals into numbers. There are some quite complex methods, but I wonder whether it's possible to use TO_NUMBER fuction.
There's a 'RN' format, it works well other way round, say:
SELECT
TO_CHAR('1998', 'RN')
FROM DUAL;
However this won't work:
SELECT
TO_NUMBER('MCMXCVIII', 'RN')
FROM DUAL;
Any ideas?
thanks,
You can't use the function "TO_NUMBER" o "TO_CHAR" for convert a roman number to decimal, but today there is a algorithm for do it.
This algorithm use CTE / recursive subquery.
For each character, starting from the RIGHT (lowest value Roman numeral):
1.- Convert the character into the value it represents
2.- If the character’s value is greater than or equal to the previous one, add the value to the running total
3.- If character’s value is less than the previous one, subtract the value from the running total
Below, I leave you the url the documentation and the script.
References:
http://rdbms-insight.com/wp/2015/roman-numerals-to-decimal-in-sql/
https://livesql.oracle.com/apex/livesql/file/content_CESOH7H2D4O88XLW60330Q3L9.html
I hope to help you :)

Conversion failed when converting the varchar/nvarchar value to data type int [duplicate]

This question already has answers here:
Can't convert varchar values to data type int
(2 answers)
Closed 9 months ago.
SELECT CONVERT([int],11.25)-CONVERT([int],'10.25')
How to avoid error and return 1 as result.
This is not a duplicate question. The similar question you referred is complex when comparing to my simple query question.
Rather than using the implicit behaviour of the CONVERT function, why not use the correct function, e.g. ROUND
SELECT ROUND(11.25, 0)-ROUND(CONVERT(DECIMAL(9,2),'10.25'), 0)

How to covert a string to date format ddmonyyyy [duplicate]

This question already has answers here:
Convert pyspark string to date format
(6 answers)
Closed 2 years ago.
I have a string containing date in it.
It looks like:
01JAN2020
02FEB2020
I use the following code to parse it to date format but didn't succeed:
a= a.withColumn('dt_one', to_date(unix_timestamp(col('dt_one'), 'dLy').cast("timestamp")))
Do you know the right syntax to do this?
Thanks!
I think this should work:
a= a.withColumn('dt_one', to_date(unix_timestamp(col('dt_one'), 'dMMMy').cast("timestamp")))

number rounded down in select statement with equation SQL Server 2012 [duplicate]

This question already has answers here:
Integer division in sql server
(8 answers)
Closed 7 years ago.
If I do
select 18000*13/100000
it returns 2
when it should return 2.34
How do I make it return the correct number?
thanks
select 18000*13/100000.0
Use a float either in numerator or denominator to get a float.
Use a decimal point to avoid integer division:
select 18000.0*13/100000
You can also CAST() as DECIMAL or FLOAT a field, but I like to just use mycol *1.0

Sql find the last non-numeric character in a string [duplicate]

This question already has answers here:
SQL to find first non-numeric character in a string
(3 answers)
Closed 7 years ago.
How to find last non-numeric character in a string such as "10jnklgm51". In order to find 'm' in the example what is the best simple way?
The last non-numeric character is the first non-numeric character in the reverse string. So, something like this:
select substring(reverse(str),
patindex('%[^0-9]%', reverse(str)),
1)