I have a query which looks something like below which aim is to see if a two letter code exists in a string. If it does, set a column to yes, if it doesnt set a column to no
SELECT ID,
case when Table_a.item LIKE ANY ('%AA%','%AB%','%AC%','%AD%','%AE%','%FF%',' %GG%','%HR%','%TR%','%ST%','%VL%') THEN 'YES' else 'NO' end AS foo,
item
FROM Table_a
Unfortunately its causing rows to appear twice
ID foo item
112 yes AA-FF-TT-RR
112 no AA-FF-TT-RR
Does anyone know why.
Am i misusing the LIKE ANY function
We are not on Teradata 14 yet
Thank you for your time
Related
Hi I am trying to create a query for SQLite which has a variable part in it. By variable I mean that a certain part within the string can possibly contain a variable but also an empy value
I tried this but I am not sure whether this works.
SELECT * FROM table WHERE attr LIKE 'ABC% %DEF'
Adding onto my comment, check the below code to test your values.
SELECT CASE WHEN 'ABC G DEF' LIKE 'ABC%DEF'
THEN 1
ELSE 0 END as test_space,
CASE WHEN 'ABCGGGDEF' LIKE 'ABC%DEF'
THEN 1
ELSE 0 END AS test_all
I'm using SQLite to deal with tons of data (like 100gb of data).
I need to seach the value of one column in other table in the fastest way possible.
For example, I need to find the following values of Table 1
[COD]
C62
K801
And then find them in Table 2:
[COD_2]
C60-C63
K80-K81
My desired result is something like:
[COD_1] [COD_2]
C62 C60-C63
K801 K80-K81
Since I have a lot of data, it is inefficient to do something like:
SELECT *
FROM TABLE_1, TABLE_2
WHERE COD_1 LIKE '%' || COD_2 || '%';
Instead, I was trying to do this:
SELECT *
FROM TABLE_1
WHERE COD_1 IN (SELECT COD_2 FROM TABLE_2);
Of course that this doesn't result because the codes are not exactly the sames. Is there a way to search for similar values of one column (something like the LIKE operator) in other table by using IN? Or other way that doesn't cross TABLE_1 and TABLE_2?
Thank you!!!
useful to me.
Based on the small data set shown, and my presumed answer to #Shawn's question (K801 is a typo and is meant to be K80 or K81) I assume the following problem description:
Find a row in COD_2 such that the value in COD_1 is between {value1}-{value2} in COD_2; the - being significant and dependable.
I cannot speak to speed, but I would approach it this way:
SELECT value1, value2
from COD_1,COD_2
where value1 between substr(value2,1,instr(value2,'-')-1) and substr(value2,instr(value2,'-')+1)
The thought being: split the value from COD-2 into a "start" and an "end" value.
I made a stupid mistake and created a column like this:
CREATE TABLE mytable (mycol INTEGER, ...)
As you can see, I forgot to define a default value like "DEFAULT 0".
In my code, I need to raise the value in "mycol" by 1.
I was baffled when I found out that this code didn't have any effect.
UPDATE mytable SET mycol=(mycol+1)
The column value stays as it is. In my case "EMPTY" (=no value at all).
I would like to avoid re-creating the table if possible.
I would like to ask if there is any easy way to fix this in the SQL statement so that an EMPTY value is seen as 0 so that
UPDATE mytable SET mycol=(mycol+1)
on a column value of EMPTY would finally produce the new column value of 1.
You can use such as below if your column has null value:
UPDATE mytable SET mycol= ifnull(mycol,0)+1
I'm working on a table where I have two boolean columns a and b. I want to make sure that a will never equal b, but I can't seem to get it work using the following constraint--and google doesn't seem to have anything on how to do this kind of thing in SQLite, but it may have been the way that I was wording things:
create table foobar
(
a boolean,
b boolean,
Check(a<>b)
);
I've also tried defining the table like this:
create table foobar
(
a boolean Check(a<>b),
b boolean Check(b<>a)
);
But it seems like no matter what I do, when I go to insert the same value into both columns, SQLite doesn't seem to recognize that I've specifically told it--tried to at any rate--not to let b equal whatever a is, and vice a versa.
insert into foobar values (1,1);
select * from foobar;
a b
---------- ----------
1 1
Any ideas? I feel like I've got the right general idea, except that I'm missing something horribly obvious.
Just omit a; set b to false where a would have been true, and b to true where a would have been false... at least, I think that'd work.
I need to select v_col1, from table_x and that column gives me string that i need to put(update) into same
rowid but into diffrent column(h_col2) in sama table table_x - sorry it seems easy but i am beginner....
tabl_x
rowid V_col1, h_col2 etc .....
1 672637263 GVRT1898
2 384738477 GVRT1876
3 263237863 GVRT1832
like in this example i need to put GVRT1898 (update) instead of 672637263 and i need to
go into every row in this table_x and fix -
like next line would be (rowid2 would be GVRT1876 instead of 384738477 :-)
this table has 40000 lines like this and i need to loop for every rowid
THX for your responce Justin - this is a little more complex,
i have this string in h_col and need to take only GVRTnumber out and put into v_col - but it's
hard becouse GVRTnumber is in various place in column see down here....
"E_ID"=X:"GVRT1878","RCode"=X:"156000","Month"=d:1,"Activate"=d:5,"Disp_Id"=X:"4673498","Tar"=X:"171758021";
2"E_ID"=X:"561001760","RCode"=X:"156000","Month"=d:1,"Activate"=d:5,"Disp_Id"=X:"GVRT1898","Tar"=X:"171758021";
h_col column have this number that i want but in various place like somethimes it's in this 600byte column it's in byte nr 156 - sometimes in 287 but the only unique is "GVRT...." how can i take that string and put it to v_col -
Can you show me how to write such SQL pl/sql ?
regards & thanks
It sounds like you just want
UPDATE tabl_x
SET h_col2 = v_col1
Of course, if you do something like this, that implies that one of the two columns should be dropped or the data model needs to get fixed. Having two copies of the same data in each row is a bad idea from a normalization standpoint if nothing else.