This question already has answers here:
Converting words to numbers in PHP
(6 answers)
Closed 8 years ago.
How to convert any string into digit in SQL without CASE and Decode function.
eg. THREE to 3
FOUR to 4
FIVE to 5
SIX to 6
Range is not decided.. can be vary upto N.
Well, I'm not sure whether this is what you need, but what about defining a table, say digits, like this:
digit: text | value: int
------------+-----------
one | 1
two | 2
three | 3
etc.
Then use a query, for example, like this one:
SELECT value FROM digits WHERE digit = 'FIVE'
Sure, it's pretty weird (to say the least), but nonetheless the use of CASE is avoided.
Related
This question already has answers here:
Convert the string 2.90K to 2900 or 5.2M to 5200000 in pandas dataframe
(6 answers)
Closed 3 years ago.
I have a column with data as:
1 77M
2 118.5M
3 72M
4 102M
5 93M
6 67M
I need to change this to its numerical value as:
77,000,000
and so on.
I have tried different ways but could not come up with a definite solution.
okay this should work
(df[1].str.replace('M','').astype(float) * 1000000).astype(int).astype(str).apply(lambda x : x[:-6]+','+x[-6:-3]+','+x[-3:])
Output
0 77,000,000
1 118,500,000
2 72,000,000
3 102,000,000
4 93,000,000
5 67,000,000
Name: 1, dtype: object
We have a process that reads an XML file into our database and inserts any rows that aren't currently in another table to that table.
This process also has a trigger to write to an audit table and a nightly snapshot is also held in another table.
In the XML holding table a field looks like 1234567890123456 but it exists on our live table as 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6. Those spaces will not be removed by any combination of REPLACE functions. We have tried all CHAR values and it does not recognise the character. The audit table and nightly snapshot, however, contain the correct values.
Similarly, if we run a comparison between SELECT CASE WHEN '1234567890123456' = '1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 ' THEN 1 ELSE 0 END, this returns 1, so they match. However LEN('1234567890123456') is 16 and LEN('1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 ') is 32.
We have ran some queries to loop through the characters in the field and output the ASCII and Unicode values for the characters. The digits return the correct ASCII/Unicode values, but this random whitespace character does not return a value.
An example of the incorrectly displayed one is 0x35000000320000003800000036000000380000003300000039000000370000003800000037000000330000003000000035000000340000003000000033000000 and a correct one is 0x3500320038003600380033003200300030003000360033003600380036003000. Both were added by the same means on the same day. One has the extra bytes, the other is fine.
How can we identify this character and get rid of it? Is there a reason this would have been inserted originally? How can we avoid this in future?
Data entry
It looks like some null (i.e. Char(0)) characters have got into the data.
If the data was supposed to be ASCII when it was entered but UTF-16 data got, then it could be:
Entered character codes: 48 00
Sent to database: 48 00 00 00
To avoid that, remove disallowed characters as the first step in processing the input, say by using a regex to replace [\x00-\x1F] with an empty string.
Data repair
Search for entries which a Char(0) in them to confirm that they can be found that way.
If so, replace the Char(0) with an empty string.
If that doesn't work, you could convert the data to the format '0x35000000320000003800000036000000380000003300000039000000370000003800000037000000330000003000000035000000340000003000000033000000', replace '000000' with '00', and then convert back.
This question already has answers here:
how to display number value in words
(4 answers)
Closed 6 years ago.
Suppose in table I have a number column as
1
2
3
4
I want to display those rows as
one
two
three
four
How can I do using SQL
You can use a technique from this blog which uses a hack with dates to get the text version of numeric fields. The blog post goes into much more detail, but in short, it converts the number to a Julian date which lets TO_CHAR use the format specifier sp (spelling out in text)
SELECT num, TO_CHAR(TO_DATE(num, 'J'), 'Jsp') num_as_text
FROM myTable
ORDER BY num;
# num num_as_text
# ----------------
# 1 One
# 2 Two
# 3 Three
# 4 Four
You could use the j --> jsp technique to spell the number. It's been a FAQ.
j = julian. take the number and pretend it is a julian date, convert
it into a date.
jsp = Take that date and spell the julian number it represents.
For example,
SQL> SELECT LEVEL,
2 to_char(to_date(LEVEL,'j'), 'jsp') num_spell
3 FROM dual
4 CONNECT BY LEVEL <= 10;
LEVEL NUM_SPELL
---------- ----------
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
10 ten
10 rows selected.
SQL>
Assuming the schema is structured as you suggest like:
Table
1 2 3 4
value value value value
value value value value
value value value value
You can use AS to rename the column in a query:
SELECT 1 AS one
2 AS two
3 AS three
4 AS four
FROM table
This question already has answers here:
Is there a way to cast float as a decimal without rounding and preserving its precision?
(3 answers)
Closed 9 years ago.
I have a table in SQl with a column in float type , this table is used to send values to a one fiscal printer,
code name price
----------------------------------
34 cUP 2,5
36 BOOK 2
37 COMET 1,2
38 TOY 1
IS posible configure SQl to show 1,00 o 2,00 when the value have not cents.
When i send to the printer i use this line :
string preco = vercup.Rows[i]["unitario"].ToString();
how can i force to show 1,00 when the values comes 1.
How you store the data isn't related to how the data is presented. yes, when you present the data you can force it to display two decimals.
select convert(decimal(9,2), price) from table
That's just 1 possible solution.
Suppose you have a result that is 100 chars long but you only have a 50 char width. How do you split a MYSQL result into two rows of 50 chars each?
Could you clarify the question a bit? Are you looking to insert 100 chars of data into a 50 char column? Or do you have 100 chars in the database but only have space in your app to display 50 chars?
I have 100 chars in the database result set but I want the result set string to have a break after the 50th char and continue onto the next line.
Example
SELECT * FROM FOO
returns
1 2 3 4 5 6 7 8 9...50 51 52 53..98 99 100
but I want
1 2 3 4 5 6 7 8 9...50
51 52... 99 100
Is this possible?
SELECT substring(col, 1, 50) FROM foo
UNION ALL
SELECT substring(col, 51) FROM foo
Your'e asking a question about formatting data for viewing. SQL is a declarative data retrieval language, not a data pretty formatting language. You should solve this problem in your non-SQL code.
Formatting data in a SQL query is not a good idea, unless you have to write something that will run in a query analyzer. Your question isn't specific about whether or not that is the case.
Do you want to return the result set in PHP or MySQL? If the former, then it's easier.
Take the string, and take the first 100 characters, put in a line break, and then the rest of the string.
MySQL would work on the same principle, but you may have issues with line-break characters.