I have numbers (from my select)
col1 col2
1. 3.6 and 3
2. 3.6 and 5
I want this
if(col1/col2 = integer)
update column
else(col1/col2 = decimal number)
update column
Any idea how to get is number integer or decimal?
You could use modulo 1 (% 1), and if the remainder is zero then you have your 'integer'. For example, with SQL Server:
if (col1 / col2 ) % 1 = 0
-- integer
else
-- decimal
Related
I would like to add a column to an SQL table with unknown columns and explode the entries in that table by a set of fixed values for that column. E.g. Turn
unknown col 1
...
unknown col x
1
...
foo
2
...
bar
into
unknown col 1
...
unknown col x
new col
1
...
foo
1
2
...
bar
1
1
...
foo
2
2
...
bar
2
The number of unknown columns is also unknown. I know the query to turn the original table into
unknown col 1
...
unknown col x
new col
1
...
foo
1
2
...
bar
1
I don't know the INSERT query that would turn it in to the desired table further above. The table is on Google BigQuery.
p.s: I can think of workarounds, e.g. multiply the number of rows in the original table by n, where n is the number of values the new column can take, then add the column and set the value based on the row number (which is not trivial to set) for each row. I am looking for a cleaner way.
add a column to an SQL table with unknown columns and explode the entries in that table by a set of fixed values for that column.
Below should do the "trick" - example
with new_col_values as (
select [1, 2, 3, 4] values
)
select t.*, val
from `project.dataset.your_table` t,
new_col_values, unnest(values) val
I have a table in SQL database and I want to find the location of a cell like a coordinate and vice versa. Here is an example:
0 1 2 3
1 a b c
2 g h i
3 n o j
When I ask for i, I want to get row=2 and column=3. When I ask for a cell of row=2 and column=3, I want to get i.
You need to store your matrix in table specifying the columns and rows like this
create table matrix (
row int,
column int,
value varchar2(20)
);
Then you insert your data like this
insert into matrix values (1, 1, 'a');
insert into matrix values (1, 2, 'b');
//and so on.
And then you can simply find what you need using two queries
select column, row from matrix where value = 'i';
select value from matrix where column = 2 and row = 3;
In Oracle, you would do:
select "3"
from t
where "0" = 2;
Naming columns as numbers is not recommended. Your whole data model is strange for SQL. A better representation would be:
row col val
1 1 a
1 2 b
1 3 c
2 1 g
. . .
Then you could do:
select val
from grid
where row = 2 and col = 3;
Create a primary key column such as 'id' and for example, the related row is 'col'
select col from db where id = 2;
this returns you a specific cell (x,2)
I want to select myNum from myTable where myNum's 3rd number after decimal point is above 0.
So
123.456 would be returned.
123.450 would not.
(sybase)
SELECT myNum
FROM myTable
WHERE FLOOR(myNum*1000) % 10 > 0
Explanation:
Seeing is believing, so given the input 123.456:
myNum*1000 = 123456.xx (possibly data last 3rd decimal place)
FLOOR(myNum*1000) = 123456
FLOOR(myNum*1000) % 10 = 6, which is greater than zero
I have a table with a1 and a2 float columns,
The values in a2 are calculated from a1, as a2 = 3*a1
The condition is:
If the value in a1 is 9.5, I need to get the ceiling value in a2
i.e., if the numeric value after the decimal point is greater than or equal to 5 I need to get ceiling value, else I need to get the floor value.
I have written below query
SET a2 =(case when substring(cast((a1 * 3) as varchar(6)),CHARINDEX('.',(a1*3)),1) >=5 then CEILING(a1 * 3) else FLOOR(a1 * 3) end) from table
but it obviously returns the below error:
Conversion failed when converting the varchar value '.' to data type int.
Since it, can't take varchar into ceiling or floor.
Is there any way by which I can achieve this?
Your help will be greatly appreciated.
The value of a2 keeps changing based on a1, if a1 is 4.5 a2 should ceiling of that, if a1 is 4.9 a2 should be again ceiling value but if a1 is anything below 4.5 as 4.3,4.2,4.1 then it should be a floor value
Any other approach for this would also do except ceiling and floor.
How about using round()? It implements this logic directly in the database:
set a2 = round(a1, 0);
An alternative method is to subtract 0.5:
set a2 = floor(a1 + 0.5)
If you want a2 as a string value (you say you want a float but the code returns a string), then use str():
set a2 = str(a1)
str() rounds by default.
That because you trying to compare varchar >=5, cast it to INT:
SET a2 =(case when cast(substring(cast((a1 * 3) as varchar(6)),CHARINDEX('.',(cast((a1 * 3) as varchar(6))),1) as INT) >=5 then CEILING(a1 * 3) else FLOOR(a1 * 3) end) from table
How do I assign, say, integers 1-10 value "x" and integers 11-20 value "y" without creating 20 different entries?
For example, if you are buying a concert ticket, and you are assigned integer 2, you will sit in row 3. likewise, if you are assigned integer 9, you will also sit in row 3. however, integer 13 will sit in row 5, as will integer 19.
the integers assigned are purposely assigned, and some integers may be skipped (They are not consecutive).
Use a CASE expression
UPDATE YourTable
SET row = CASE WHEN number <= 10 THEN 3
WHEN number <= 20 THEN 5
WHEN number <= 30 THEN 4
...
END