Convert fraction to decimal [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Working on 10g.
I am writing a query to sort a list, and sorting is an important aspect of the list.
I have already isolated the fraction from the mixed number.
I have data that come in fraction form. (3/4, 5/8, 1/2)
I need to convert them to decimal to be able to 'order by' with them.
Any help would be appreciated.

This will blow up badly if the input is not a fraction such as 3/4, 5/8, etc., but here goes:
CAST(SUBSTR(theFraction, 1, INSTR(theFraction, '/')-1) AS NUMBER) /
CAST(SUBSTR(theFraction, INSTR(theFraction, '/')+1) AS NUMBER)
The logic is basically "get everything before the '/' and convert it to a number, then divide it by everything after the '/' converted as a number".

Related

using mid function in bigquery [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a column that has data like this:
TEXT000612
TEXT721
TEXT8
Expected output:
000612
721
8
The column only has a 4 letter text text and its only at the beginning at the cell. But the numbers can vary in length. Also, I want to make sure the numbers are string.
There is no mid function in BQ.
Also, if you dont like my question or think it needs to be improve please give me a chance to improve it before flagging it.
Few options for BigQuery Standard SQL
#standardSQL
SELECT col,
SUBSTR(col, 5),
REGEXP_REPLACE(col, r'^TEXT', '')
FROM `project.dataset.table`
If you just want the numbers, use substr():
substr(col, 5)
You can cast() this to a number if you want.

convert a string (43677) to date format in SQL(Redshift) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How to convert a string (43677) to date in SQL(Redshift)? (I'm trying to convert strings, which are number format of any particular date in excel)
It depends what day you want, but I think:
select dateadd(day, 43677, '1899-12-30')
This interprets the value as an Excel date. It produce 2019-07-31.

How do i find difference of two columns in SQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to find if the difference between 2 columns. How do I achieve that?
For example,
select(col1-col2) output is 1
select(col2-col1) output is -1
Is there a way to get just the difference as 1 without the negative (-) sign?
use abs() function
select abs(col2-col1) as diff

How do I show ☺♦♦ characters in SQL Server? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
You can I display special Unicode characters in my result set on my SQL Server?
e.g. How can i display those characters ☺♦♦?
Well due to the fact that your question is very unspecific here are some solutions.
You need to specify a string as unicode string. This can be achieved by adding an N in front of the string. See example:
SELECT N'☺♦♦' as a, '☺♦♦' as b
Result:
a b
---- ----
☺♦♦ ???
If you want to store those symbols, you need a type as nvarchar or nchar.
Hopefully this helps you.

Fill with zero to complete a defined number in sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to complete cards numbers in sql. I have the prefix =11111 and the number of the card which is variable, therefore it could be '25' or '2130' but at the end I must have 14 numbers. So I need to fill spaces with zeros.
I've read about 'LPAD' but I don't understand very well this method.
You could use lpad, but if you're starting with a number you could use a 9-digit format model instead, and concatenate that onto your prefix:
select '11111' || to_char(25, 'FM000000000') from dual;
11111000000025
The FM format modifier stops Oracle adding a space for a potential +/- sign indicator.
SQL Fiddle demo
Use the ZEROFILL attribute.
But your database should only be responsible for saving data and not changing it before saving.
The best way would be to send the zerofilled data to the database server.