MS Access SQL - Find numbers with decimals no end to Zero (,00) - sql

I am having a table and i would like to find all numbers where it doesn't end to ,00 but with decimal number like ,01 or ,23 or etc...
My Table Data Desire Output
id s id s c
1 32,00 1 32,00 NULL
2 13,13 2 13,13 13,13
3 55,05 3 55,05 55,05
4 76,00 4 76,00 NULL
I would like to create a sql query to create column c like
iif(s IS ZERO AFTER DECIMAL, NULL, s)
Is it possible in MS ACCESS SQL?

For positive numbers, you can use floor or int():
iif(s <> int(s), . . .

Related

SQL - split numeric into 2 columns?

I am trying to split some numeric keys in my table into separate columns (to help save space in SSAS, lower cardinality)
My data looks like the below..
LeadKey
1
2
3
5522
83746623
I want to split these into 2 columns... with 4 digits in each column. (where applicable, as anything 1>9999 won't have anything populated in the 2nd column)
So an example output of the above would be the below..
LeadKey Split1 Split2
1 1
2 2
35566 3556 6
5522 5522
83746623 8374 6623
How could I achieve this? I have split columns easily before using substring and a known character.. but never had to do a split like this. Does anyone have an approach to handle this?
Here is a solution in case you have the LeadKey numbers as int.
select LeadKey
,left(LeadKey, 4) Split1
,right(LeadKey, case when len(LeadKey)-4 < 0 then 0 else len(LeadKey)-4 end) Split2
from t
LeadKey
Split1
Split2
1
1
2
2
35566
3556
6
5522
5522
83746623
8374
6623
Fiddle
In this example, I used left for the Split1, and show the values past the 4th position for the Split2:
I've included a testing temporary table to hold our the testing values.
Feel free to adjust the code to work with your situation.
DECLARE #thelist TABLE
(
LeadKey int
);
INSERT INTO #thelist (LeadKey)
select 1 union all
select 2 union all
select 35566 union all
select 5522 union all
select 83746623
select cast(x1.LeadKey as varchar(19)),
Left(x1.LeadKey, 4) as 'Split1',
(case when len(x1.LeadKey) > 4 then right(x1.LeadKey, len(x1.LeadKey) - 4)
else '' end
) as 'Split2'
from #thelist as x1

Select only numeric value

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

Get the unique word count of each word in Hive

I am having a table such as follows,
select * from tablename;
ID sentence
1 This is a sentence
2 This might be a test
3 America
4 This this
I want to write a query to split the sentence into words and get the count of the words in the descending order. I want to have an output something like,
word count Unique(ids)
This 4 3
a 2 2
might 1 1
.
.
.
where count is the number of times the word has occurred in the column and Unique(ids) is the number of users with that word.
I am thinking in what way we can write a query to do this?
Can anybody help me doing this in hive?
Thanks
laterral View
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+LateralView
select id, word
from tablename tn lateral view explode( split( tn.sentense, ' ' ) ) tb as word
the result will be:
1 This
1 is
1 a
1 sentense
2 This
2 might
2 be
2 a
2 test
3 america
aggregate the result

Applying patterns to introduced text after Insert

select distinct SUBSTR(dni,4,2) as counter_dni
from persona
where SUBSTR(dni,4,2)<=10
order by counter_dni;
It returns:
1 - 1
2 - 10
3 - 2
4 - 3
The first number is just the row number, using Oracle SQL developer.
The problem is that I have multiple inserts like:
DNI1
DNI2
DNI3
until DNI 15 or so.
What i want to do is replace the format after DNI with a specific pattern like:
DNI0001
DNI0010
filling the gaps with 0 in a lenght of 4 digits for example. How can i do that after the rows were inserted?
Try
(select REGEXP_REPLACE(dni,'([0-9])+$', LPAD('\1',5,'0')) from counter_dni;
to check the output and
update counter_dni set dni = (REGEXP_REPLACE(dni,'([0-9])+$', LPAD('\1',5,'0')));
to update your values

order sql server string like numbers

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