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
Related
I am looking for a simple clean method to obtain the sequence {1, 2, 3, 4, 5, 6, 7,...,1000000} in MS Access SQL. I thought of creating a table with a column that numbers from 1 to 100000 however, this is inefficient.
Is there a way of generating numbers 1 to 10000000 in MS Access using SQL?
I tried the GENERATE_SERIES() function but MS Access SQL does not support this function.
id | number
------------
1. | 1
2. | 2
3. | 3
4. | 4
5. | 5
6. | 6
7. | 7
8. | 8
Yes, and it is not painfull - use a Cartesian query.
First, create a small query returning 10 records:
SELECT
DISTINCT Abs([id] Mod 10) AS N
FROM
MSysObjects;
Save it as Ten.
Then run this simple query:
SELECT
[Ten_0].[N]+[Ten_1].[N]*10+[Ten_2].[N]*100+[Ten_3].[N]*1000+[Ten_4].[N]*10000+[Ten_5].[N]*100000 AS Id
FROM
Ten AS Ten_0,
Ten AS Ten_1,
Ten AS Ten_2,
Ten AS Ten_3,
Ten AS Ten_4,
Ten AS Ten_5
which, in two seconds, will return Id from 0 to 999999.
Very painful but you can do the following:
create table numbers (
id int autoincrement,
number int
);
-- 1 row
insert into numbers (number) values (1);
-- 2 rows
insert into numbers (number) select number from numbers;
-- 4 rows
insert into numbers (number) select number from numbers;
-- 8 rows
insert into numbers (number) select number from numbers;
-- repeat a total of 20 times
The value of number is always 1, but id increments. You can make them equal using an update:
update numbers
set number = id;
If this were SQL server you could use a recursive CTE to do this.
WITH number
AS
(
SELECT num = 1
UNION ALL
SELECT num + 1
from number
where num < 1000000
)
SELECT num FROM number
option(maxrecursion 0)
But you are asking about MS Access, since access does not support recursive CTEs, you can try doing it with a macro and insert it into a table and read it out?
I have a table that has a column with 1 to 3 digits.
For example:
(342) 342-9324
(1) 234-3424
(04) 234-7744 etc
But I am not sure how to write the query. I use
SUBSTRING(x, 2, 3)
where x is name of column, but I only get 3 digits, anyone have any ideas to extract digits in brackets that could be 1, 2 or 3 digits? This is done using sql server. Also this table has more than 5 million rows of phone numbers
If area code is within (), some simple string functions should do.
Example
Declare #YourTable Table ([Phone] varchar(50)) Insert Into #YourTable Values
('(342) 342-9324')
,('(1) 234-3424')
,('(04) 234-7744')
Select *
,AreaCode = replace(left(Phone,charindex(')',Phone+')')-1),'(','')
From #YourTable
Returns
Phone AreaCode
(342) 342-9324 342
(1) 234-3424 1
(04) 234-7744 04
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
I want to write a statement that generates a random pick from a set of four values (2,4,6, and 8). Below is the select statement that I have so far
SELECT
CASE
WHEN RN_GENERATOR.RANDOM_NUMBER BETWEEN 0 AND 2.00 THEN 2
WHEN RN_GENERATOR.RANDOM_NUMBER BETWEEN 2.01 AND 4.00 THEN 4
WHEN RN_GENERATOR.RANDOM_NUMBER BETWEEN 4.01 AND 6.00 THEN 6
WHEN RN_GENERATOR.RANDOM_NUMBER BETWEEN 6.01 AND 8.00 THEN 8
END AS ORDER_FREQUENCY
FROM (SELECT ROUND(RAND()*8,2) AS RANDOM_NUMBER FROM DUMMY) RN_GENERATOR
is there a more intelligent way of doing this?
Looks to me as if your requirement can be fulfilled but his statement
select
ROUND(rand()*4, 0, ROUND_CEILING) * 2 as ORDER_FREQUENCY
from dummy;
RAND() * 4 spreads the value range of possible outcomes for the RAND() function from 0..1 to 0..4.
ROUND( ... , 0, ROUND_CEILING) rounds the number to the next larger or equal integer and leaves no decimal places. For this example this means that the output of this rounding can only be 1, 2, 3 or 4.
*2 simply maps the four possible values to your target number range 2, 4, 6, 8. If the multiplication wouldn't suffice, you could also use the MAP() function for this.
And that's it. Random numbers picked from the set of (2, 4, 6, 8).
You can randomly sort a data set by using
ORDER BY Rand()
and select first one as your random value
Here is an example
select top 1 rownum
from Numbers_Table(10) as nt
where rownum in (2,4,6,8)
order by rand();
Numbers_Table function returns a numbers table on HANA database and I filter only the values that you want to see as your possible random values using WHERE clause
TOP 1 clause in SELECT command returns the first integer which is randomly ordered
I hope it helps
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.