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 :)
Related
Here's what i'm having trouble with. I have a table where i have the column weight, the data in this column is varchar2, example: 60 kg.
When i try to make a WHERE condition asking to retrieve everything below 60 kg, it doesn't give me the wanted results.
I tried to convert using to_number but it's not working and giving me error "Invalid Number". I also tried to cut the kg with SUBSTR and convert, and doesn't work as well.
Any suggestions?
Thanks in advance.
You can extract leading digits and convert that to a number:
select to_number(regexp_substr(weight, '^[0-9]*'))
from t;
The regular expression is starting from the beginning of the string (the ^) and extracting digits. The * is for any number of digits. Regular expression match is "greedy" by default, meaning that it will keep matching as many digits in a row as there are.
If you have decimal places or negative values, this might be a tad bit more complicated.
Could you please help me to correct the syntax ?
I have to write below code in SQL server.
This is working perfectly fine in Oracle database.
Select to_number(substr((((END_TS - BEGIN_TS)*(24*60*60))),1,10)) AS EXECUTION_TIME
from TABLE B
Also END_TS and BEGIN_TS are of datetime datatypes.
In SQL Server math can not be performed directly on dates, as it can in Oracle. You need to apply a datediff function to calculate the difference before you manipulate it:
select convert(numeric(10,9),left(datediff(second,begin_ts,end_ts)/(24.0*60*60),10)) from table;
Note that the expression in the divisor needs to have a floating point number in it (hence the ".0") otherwise the result is rounded to an integer.
After performing the date calculation, the left function is the equivalent of the substring in Oracle. It converts to a varchar then takes the first 10 characters. Convert then returns to a numeric, which is the equivalent of Oracle's variable-length number. It is necessary to tell convert that you expect digits after the decimal, otherwise it will round.
The substring for the first 10 characters has a bad smell, I would leave it out. This snippet does the calculation without restricting to the first ten characters.
select datediff(second,begin_ts,end_ts)/(24.0*60*60) from table;
Also note that the Oracle version provides fractional dates. If you only wanted the whole day then use "day" as the datepart parameter to datediff.
I have tried many combinations of the SQL functions; so as to have a 12 digit number including the dot character, including leading zeroes and decimal points.
For example:
for the number 121.22, I want to format it to 000000121.22
or for the number 12.2, I want to format it to 000000012.20
or for the number 100, I want to format it to 000000100.00
I have used the following function; but I lost the decimal points if it's zero.
SELECT RIGHT('000000000000'+ STR(CONVERT(VARCHAR,MYNUMBER),12,2),12);
Any idea on how to solve this problem in Microsoft SQL?
If you're on SQL Server 2012 or later, you can use the format() function.
SELECT FORMAT(121.22, '000000000000.00')
SELECT FORMAT(12.2, '000000000000.00')
000000000121.22
000000000012.20
for ms sql versions not in (2012,2014):
cast(right('000000000',9-len(floor(the_number))) as varchar)
+ cast( cast(the_number as decimal(10,2))as varchar)
for ms sql versions in (2012,2014):
format(the_number ,'000000000000.00')
SELECT padded_id = REPLACE(STR(id, 12), SPACE(1), '0')
Is what I add to use (In SQL server) leading 0's as needed, change the 12 to whatever total number of digits you want it to be.
This allows for non hard coded values, just make sure id or whatever column/param you want to format is set.
I have column in a postgresql database. They are lottery numbers. Four digits in length to be exact. Initially I had the datatype of the column as int. I inserted all the lottery numbers. After I inserted all the numbers I realize it chopped off my zeros. For instance 0925 is 925. I fixed the datatype to be varchar but now I need to figure out how to fix it from int to varchar with the same data. The data needs to be 4 digits in length. I was trying to just figure out how many problem numbers there are and I couldn't write a select statement that told me how many rows have less than 4 digits.
How should I go about this?
Thanks.
I was trying to just figure out how many problem numbers there are and I couldn't write a select statement that told me how many rows have less than 4 digits.
SELECT COUNT(*)
FROM lottery
WHERE char_length(x) < 4
See it working online: sqlfiddle
To fix them, you may find lpad useful. Note that the WHERE clause is not actually needed.
UPDATE lottery
SET x = lpad(x, 4, '0')
See it working online: sqlfiddle
Format your numbers with to_char():
SELECT to_char(123, 'FM0000');
You might even just leave them as integer and use the expression in queries.
Or, to convert your column back from integer to text in place:
ALTER TABLE tbl ALTER column col TYPE text USING to_char(col, 'FM0000');
Since you seem to have already converted the numbers to varchar, the expression needs an additional cast to integer:
SELECT to_char(col::int, 'FM0000')
FROM tbl;
i could not understand why following code
SQL>
Select to_number('1234.64', '9999.9') from Dual;
returns this number 1234.6?is it something like rounding ,truncation or?please help me to understand this code,i know to_number functions,i have used many times this code for simple chars,but here it is not clear anything
This looks a lot like Oracle, but I suspect that the result would be similar in any SQL that used to_number.
The to_number function takes two arguments: the string to be converted to a number, and the format string for the conversion.
In the example, '12345.64' is the string to be converted, while '9999.9' is the format string. In this format string, a 9 stands for a digit while a . stands for the decimal point.
So the function is asking to convert the string '12345.64' to a number with up to 4 digits to the right of the decimal point, and only 1 digit after the decimal point.
The second argument is optional - under normal circumstances, I would omit it.
You should use
SELECT to_number('1234.64', '9999.99') from Dual;
Your mask tells engine you want just one decimal, so number gets rounded.
If you want to get exact number, don't specify any mask:
SELECT to_number('1234.64') from Dual;