I have a table structure like below.
id version REQ_REF_ID
3 1.2 6
2 1.1 6
1 1 6
My query is below
Select * from XYZ where REQ_REF_ID = 6606 order by version desc FETCH FIRST 2 ROWS ONLY
It gives me the latest 2 rows which id is 3 and 2.
But I want to get only those two row where version number is integer, not having decimal values.
In this case I want to get the row which id is 1.
You can check against ROUND function to get integer values
and ROUND(version) = version
ROUND returns n rounded to integer places to the right of the decimal point.
You may also use TRANSLATE
where translate(VERSION, '?1234567890', '?') is null
you can check like
and (version)%1 = 0
% by 1 will give you decimal parts if any
Related
Table_A
ID Number
-- ------
1 0
2 00
3 0123
4 000000
5 01240
6 000
The 'Number' column is data type varchar.
EDIT for clarity.
My question is, can I easily pull back all rows of data which contain a variable length string of 0's?
I have tried:
SELECT *
FROM Table_A
WHERE LEFT(Number,1) = '0' AND RIGHT(Number,1) = '0'
Using the above, it would still return the below, using the example table provided.
ID Number
-- ------
1 0
2 00
4 000000
5 01240
6 000
I was looking for a function which I could pass the LEN(Number) int into, and then it generates a string of a specfic character (in my case a string of 0's). I wasn't able to find anything though.
Oh, and I also tried adding a SUBSTRING to the WHERE clause, but sometimes the Number column has a number which has a 0's in the middle, so it still returned strings with other numbers except only 0.
SUBSTRING(Number,ROUND(LEN(Number)/2,0),1) = '0'
Any help is appreciated.
So, you want a string that doesn't contain anything that isn't a 0? Sounds like it's time for a double-negative:
SELECT *
FROM Table_A
WHERE NOT Number like '%[^0]%'
AND number like '0%' --But we do want it to contain at least one zero
(The final check is so that we don't match the empty string)
Answer:
Where number like '%0%'
Your can use this query :
SELECT * FROM Table_A WHERE Number LIKE '%0%';
It'll solve your problem.
SELECT *
FROM yourtable
WHERE len(Number) - len(replace(number,'0','')) >= 0
One more approach
You can use this following one also,you will get your expected result.
SELECT *
FROM Table_A
WHERE Nunber not like '%[1-9]%'
Thanks.
Platform: Windows 7 x-64
Database: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit.
How Oracle row_limiting_clause for rowcount| percent PERCENT rows works in Oracle 12c ?
Oracle row_limiting_clause acts strangely.
In Below code,table "fetch_test" has 10 rows
Case 1
When use row_limiting_clause with fetch first 0.5 rows only I get,
no rows selected
Case 2
When use row_limiting_clause clause with first 5 percent rows only I get
1 row as output.
Case 3
When use row_limiting_clause clause with first 1 percent rows only I get
1 row as output.
This means Case 1!=Case 2 [0.5!=0.5 (5 percent of 10 rows)]
SQL> select * from fetch_test;
COL1 COL2
---- ----
1 A
2 B
3 C
4 D
5 E
6 F
7 G
8 H
9 I
10 J
10 rows selected.
case 1:
SQL> select col1 from fetch_test
2 fetch first 0.5 rows only;
no rows selected
case 2:
SQL> select col1 from fetch_test
2 fetch first 5 percent rows only;
COL1
----
1
1 row selected
case 3:
SQL> select col1 from fetch_test
2 fetch first 1 percent rows only;
COL1
----
1
1 row selected
What Oracle Documentation has to say,
If row count includes a fraction, then the fractional portion is truncated. If
row count is NULL, then 0 rows are returned
"source"
This explain case 1,but i can not find possible explanation for case 2 and 3 in my above example.
Any Help will be highly appreciated.
Your observation is correct. A strict reading of the Oracle docs indicates that this behavior is correct:
rowcount case specifies that fractions are truncated.
no mention is made that percents are truncated. Therefore, it is reasonable to assume that the percents aren't truncated.
This seems reasonable, since a fractional number of rows doesn't make logical sense, but a fractional percentage does make logical sense.
Use rowcount to specify the number of rows to return. rowcount must be a number or an expression that evaluates to a numeric value. If you specify a negative number, then rowcount is treated as 0. If rowcount is greater than the number of rows available beginning at row offset + 1, then all available rows are returned. If rowcount includes a fraction, then the fractional portion is truncated. If rowcount is NULL, then 0 rows are returned.
Use percent PERCENT to specify the percentage of the total number of selected rows to return. percent must be a number or an expression that evaluates to a numeric value. If you specify a negative number, then percent is treated as 0. If percent is NULL, then 0 rows are returned.
http://docs.oracle.com/database/121/SQLRF/statements_10002.htm
And for this specific set of numbers, we can observe that Oracle rounds 5% of 10 to 1.
> select 10*.05, round(10*.05) from dual;
10*.05 round(10*.05)
------ -------------
0.5 1
update, rishabi notes:
a remainder (when using PERCENT in the row limiter) will ALWAYS return a whole number rounded UP, as per http://community.oracle.com/message/13176927#13176927
I need to order string containing this format
number .(dot) number .(dot) number .(dot) number and so on multiple levels so the string can be
1.1.1.1.1.1.5
or
1.1.1.1.1.1.1.1.1.1.1.1.1.1......9
or
5
or
5.5.6.7.8.1.2.3454.2.11213
I have tried doing cast, but i need a solution other than common table expresions, because it is pretty slow.
is there a way to order this like numbers so 10 be next to 9 and not to 1, thank you
This works, with a few assumptions, one being that the first digit always is an int.
insert into #t values ('1.0'),
('10.2.44.2'),
('5.2.523.242'),
('4.23.5511'),
('0.9.4343.1.6.2'),
('99.245.52371.0.1'),
('1.1.1.1.1.1.5'),
('1.1.1.1.1.1.1.1.1.1.1.1.1.1......9'),
('5.5'),
('5.5.6.7.8.1.2.3454.2.11213')
SELECT CAST(SUBSTRING(c, 0, COALESCE(CHARINDEX('.',c, 0), c)) AS INT) AS FirstDigit, c
from #t
order by FirstDigit
Results:
FirstDigit c
0 0.9.4343.1.6.2
1 1.0
1 1.1.1.1.1.1.5
1 1.1.1.1.1.1.1.1.1.1.1.1.1.1......9
4 4.23.5511
5 5.2.523.242
5 5.5
5 5.5.6.7.8.1.2.3454.2.11213
10 10.2.44.2
99 99.245.52371.0.1
I was working on a query today which required me to use the following to find all odd number ID values
(ID % 2) <> 0
Can anyone tell me what this is doing? It worked, which is great, but I'd like to know why.
ID % 2 is checking what the remainder is if you divide ID by 2. If you divide an even number by 2 it will always have a remainder of 0. Any other number (odd) will result in a non-zero value. Which is what is checking for.
For finding the even number we should use
select num from table where ( num % 2 ) = 0
As Below Doc specify
dividend % divisor
Returns the remainder of one number divided by another.
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/modulo-transact-sql#syntax
For Example
13 % 2 return 1
Next part is <> which denotes Not equals.
Therefor what your statement mean is
Remainder of ID when it divided by 2 not equals to 0
Be careful because this is not going to work in Oracle database. Same Expression will be like below.
MOD(ID, 2) <> 0
ID % 2 reduces all integer (monetary and numeric are allowed, too) numbers to 0 and 1 effectively.
Read about the modulo operator in the manual.
In oracle,
select num from table where MOD (num, 2) = 0;
dividend % divisor
Dividend is the numeric expression to divide. Dividend must be any expression of integer data type in sql server.
Divisor is the numeric expression to divide the dividend. Divisor must be expression of integer data type except in sql server.
SELECT 15 % 2
Output
1
Dividend = 15
Divisor = 2
Let's say you wanted to query
Query a list of CITY names from STATION with even ID numbers only.
Schema structure for STATION:
ID Number
CITY varchar
STATE varchar
select CITY from STATION as st where st.id % 2 = 0
Will fetch the even set of records
In order to fetch the odd records with Id as odd number.
select CITY from STATION as st where st.id % 2 <> 0
% function reduces the value to either 0 or 1
It's taking the ID , dividing it by 2 and checking if the remainder is not zero; meaning, it's an odd ID.
<> means not equal. however, in some versions of SQL, you can write !=
How can I select every thrid row from the table?
if a table has
1
2
3
4
5
6
7
8
9
records
it should pick up 3, 6,9 record. regards less what their data is.
Modulo is what you want...
Assuming contiguous values:
SELECT *
FROM Mytable
WHERE [TheColumn] Mod 3 = 0
And with gaps
SELECT *
FROM Mytable
WHERE DCount("TheColumn", "table", "TheColumn <= " & [TheColumn]) Mod 3 = 0
Edit: To exclude every 3rd record, ...Mod 3 <> 0
If its SQL you could use the row_number and over commands. see this, then where rownumvar % 3 =0 but not sure if that works in access.
Or you could put the table into a recordset and iterate through checking the index for % 3=0 if your using any kind of code.
How about a Count() on a field that has unique members. (id?) then % 3 on that.