I have a requirement in hive to find a value in a column, data is ";" separated values in single column. if my required value present it should result TRUE else FALSE.
Ex: data in a single column is
Row1: insurance;finance;telecom
Row2: insurance, retail
Row3: finance, telecom, internal
Like this I have different values in a column.
I wanted to find if the column contains "finance" or not. If present it should result TRUE else FALSE
output:
Row1: TRUE
Row2: FALSE
Row3: TRUE
Please help. Thanks in advance.
If you are looking for single hardcode value, you can use like operator.
where col like '%finance%'.
But if you want it to be a variable qnd coming from another table you can use it like this
Select...
From table1 t1,
join table2 t2 on t1.col like concat('%', t2.colpattern, '%')
Related
I want to add a column called "Sweep" that contains bools based on whether the "Result" was a sweep or not. So I want the value in the "Sweep" column to be True if the "Result" is '4-0' or '0-4' and False if it isn't.
This is a part of the table:
I tried this:
ALTER TABLE "NBA_finals_1950-2018"
ADD "Sweep" BOOL;
UPDATE "NBA_finals_1950-2018"
SET "Sweep" = ("Result" = '4-0' OR "Result" = '0-4');
But for some reason, when I run this code...:
SELECT *
FROM "NBA_finals_1950-2018"
ORDER BY "Year";
...only one of the rows (last row) has the value True even though there are other rows where the result is a sweep ('4-0' or '0-4') as shown in the picture below.
I don't know why this is happening but I guess there is something wrong with the UPDATE...SET code. Please help.
Thanks in advance.
NOTE: I am using PostgreSQL 13
This would occur if the strings are not really what they look like -- this is often due to spaces at the beginning or end. Or perhaps to hyphens being different, or other look-alike characters.
You just need to find the right pattern. So so with a select. This returns no values:
select *
from "NBA_finals_1950-2018"
where "Result" in ('4-0', '0-4');
You can try:
where "Result" like '%0-4%' or
"Result" like '%4-0%'
But, this should do what you want:
where "Result" like '%4%' and
"Result" like '%0%'
because the numbers are all single digits.
You can incorporate this into the update statement.
Note: double quotes are a bad idea. I would recommend creating tables and columns without escaping the names.
I have a small query that looks like this:
SELECT
CS.ID,
CS.Import_Date,
CS.Secondary_Date
FROM
Center_Summary CS
ORDER BY CS.Import_Date
Which returns values like this:
And I want to replace these "empty" values which are pulling as 01/01/1900 with the value of 05/01/2019. In this case, it's because the ID and Import_Date match, so the Secondary_Date should match as well. I've thought to use REPLACE() (REPLACE(CS.Secondary_Date, '01/01/1900', ???), but I'm not sure how to write logic to pull in a matching value from the column Secondary_Date based on ID and Import_Date - what function should I be looking to use here?
How it's currently pulling (the dates in red I want to replace):
What my expected result is:
Why not just use a UPDATE with a WHERE?
UPDATE dbo.YourTable
SET SecondaryDate = ImportDate
WHERE SecondaryDate = '19000101';
"Empty" values are best represented by NULL. I would recommend converting them to NULL:
nullif(secondary_date, '1900-01-01')
If you really want another value, then you can use coalesce() or a case expression:
coalesce(nullif(secondary_date, '1900-01-01'), '2019-05-01')
However, I'm not generally a fan of such magic values in the code.
I have a column with many nulls in table 1, but now and then there is a value. If there is a value, I want to go to table 2 and take the the corresponding value from there. This should create an extra column called user_original:
CASE
WHEN table1.user_changed IS NOT NULL
THEN table2.user_original
ELSE -- do nothing
END as user_original
I basically want to replace
is nothing
Is this correct ? How can this be done? Is there a better way?
do you mean you need below
CASE
WHEN table1.user_changed IS NOT NULL
THEN table2.user_original
END as user_original
Do you want a correlated subquery?
(CASE WHEN table1.user_changed IS NOT NULL
THEN (SELECT table2.user_original FROM table2 WHERE table2.? = table1.?)
END) as user_original
The ? is for the columns that identifying "corresponding" values.
If this is the case, you can probably simplify this to:
(SELECT table2.user_original FROM table2 WHERE table2.? = table1.?) as user_original
My guess is there will be no matching value if the original value is NULL.
I created a view for report. I imported data to the report. In columns null values are also present. If i filter one row using filter in vb.net the null values of row cannot be displayed.
For example, column names are ID, Name, Number, Place. In this some of the places number has null values I include filter ID, Name ,Number,Place. If I filter using ID the null values of number cannot be displayed.
This is the code I tried but not filter
IN FORM TextBox1.Text=""
TABLE1BindingSource.Filter = "YOUR FIELDNAME LIKE '" + TextBox1.Text.Equals (String.Empty) + "'")
Expected Result
Table
YOUR FIELDNAME
value1
value2
NULL
value3
NULL
NULL
value4
That filter doesn't make sense. This part:
TextBox1.Text.Equals(String.Empty)
is going to evaluate to Boolean, i.e. True if the TextBox is empty and False if it's not. That means that your filter ends up being:
YOUR FIELDNAME LIKE 'False'
or the like. What you should actually be doing is something like this:
Dim columnName As String
Dim fieldValue As String
'...
TABLE1BindingSource.Filter = String.Format("{0} LIKE '{1}%'", columnName, fieldValue)
Note a number of things there. Firstly, the use of String.Format to make this sort of code more readable. Secondly, the use of the actual value entered by the user and not a Boolean indicating whether that value was empty or not. Thirdly, the use of a wildcard because using LIKE without a wildcard makes no sense.
It should also be noted that that code is only going to work with text fields, because they are the only ones that get delimited by single quotes and the only ones that you can use LIKE with. If you want to filter on a numeric field or some other data type then you'll have to write your code to create the filter differently.
I have a field in sql that contains a 1 or 0 at the end. What I am trying to do is if the field has a 1 at the end but no corresponding 0 I would like to add that record.
3 different examples
Field value
Data1 ( I would like to add another record containing Data0)
Data0 ( I would like to add another record containing Data1)
Data0 and Data1 both exists in table ( Do nothing )
insert into test(col, another_column_1,...,another_column_n)
select substr(col,1, length(col)-1) || --this is the base value
max(mod(substr(col,length(col),length(col))+1,2)) --this is the termination (0 or 1)
as col ,
max(another_column_1),
...
max(another_column_n)
from test
where substr(col,length(col),length(col)) between '0' and '1'
group by substr(col,1, length(col)-1)
having count(*)=1;
you can see test here
Updated for Oracle
If I understood your question correctly, you can use the LIKE operator.
You can do something like:
(field like '%1' or field like '%0') and not field like '%0%1'
But generally, SQL is not suitable for text processing. This LIKE thing is pretty much the most advanced text processing feature in SQL.