i would like to make a query in ma access can extract text between first and second character "/" and when there are not "/" in field in returns null.
now data in my table are like below
No option1
1 100
2 145/Mechanical/0800
3 120/electrical/1620
4 131/mechanical/0200/dw-001
Now I like to make a query can extract text between first and second character "/" like below:
No option1 discipline
1 100 null
2 145/Mechanical/0800 Mechanical
3 120/electrical/1620 electrical
4 131/mechanical/0200/dw-001 mechanical
SELECT [No], option1,
IIF(INSTR(option1,'/') > 0,
MID(option1,
INSTR(option1,'/')+1,
INSTR(INSTR(option1,'/')+1,option1,'/')-INSTR(option1,'/')-1
),
NULL
) AS discipline
FROM YourTable
Related
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
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), . . .
so given the table:
id | names
===============
1 {John, , Wayne}
2 {Luke, Harold, }
3 {Bill}
4 {Will, , }
They don't have a standard and some values may come empty ( for example {Will, , }).
I tried:
SELECT array_length(names, 1)
FROM nameTable
But I get this:
names
======
3
3
1
3
and I want it to return:
names
======
2
2
1
1
So I need something which gives me the length only of the populated fields (empty spaces like ' ') shouldn't be counted.
You can remove the NULL values and then count:
array_length(array_remove(names, NULL), 1)
For one-dimensional arrays, I find that cardinality() is convenient:
cardinality(array_remove(names, NULL))
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
My table structure is as follows :
id category
1 1&2&3
2 18&2&1
3 11
4 1&11
5 3&1
6 1
My Question: I need a sql query which generates the result set as follows when the user searched category is 1
id category
1 1&2&3
2 18&2&1
4 1&11
5 3&1
6 1
but i am getting all the results not the expected one
I have tried regexp and like operators but no success.
select * from mytable where category like '%1%'
select * from mytable where category regexp '([.]*)(1)(.*)'
I really dont know about regexp I just found it.
so please help me out.
For matching a list item separated by &, use:
SELECT * FROM mytable WHERE '&'||category||'&' LIKE '%&1&%';
this will match entire item (ie, only 1, not 11, ...), whether it is at list beginning, middle or end.