I’m working in Eloqua BI. I am trying to create one column in which I’d like to include data from couple of other columns. Those other columns contain numbers of customers.
Those columns have been created so far with a following SQL logic (in Edit Column Formula Field):
CASE
WHEN column_name = '1' THEN 'a'
WHEN column_name = '2' THEN 'b'
WHEN column_name = '3' THEN 'c'
ELSE ‘def’
What I’ve find out is the code above returns correct value for only the first WHEN. All other columns have random values.
How could I fix the code above to receive correct values for all columns? Why that statement is incorrect?
Related
I'm trying to find a way to check if two values are the same or not in two different columns using sql redshift
Here is what I've tried:
CASE
WHEN table_1.column_a = table_2.column_b THEN 'Match'
ELSE 'No match'
END AS Does_it_match?
table_1 and table_2 are joined but it keeps returning the error 'No value specified for parameter 1'
Is it possible to do this kind of CASE statement with sql redshift? If not, is there another way around it?
I have the following column, B represents boolean and the rest are empty values. I have to change all the values in this column to the word COLUMN A.
COLUMN
-----
B
I have tried different things, for example
SELECT COLUMN
FROM TABLE
WHERE COALESCE(NULLIF(COLUMN,''), 'COLUMN A');
And I receive the error: "Invalid character found in a character string argument of the function "BOOLEAN"." I'm kind of stuck to this question and I'm getting confused with this boolean value. I will be really happy if someone can help me, thanks!
The easiest thing is to use CASE expression. I am not familiar in db2, so you may want to research it further, but in other DBMSs it works like this:
SELECT CASE
WHEN COLUMN = '' THEN 'COLUMN A' -- if COLUMN = '', replace it with 'COLUMN A'
ELSE COLUMN -- otherwise, keep COLUMN as is.
END as 'COLUMN' -- name the column in the result 'COLUMN'
FROM TABLE
This is an article that explains how it works in db2:
https://www.ibm.com/support/knowledgecenter/en/SSEPEK_11.0.0/sqlref/src/tpc/db2z_caseexpression.html
The WHERE clause is unfinished. Compare the COALESCEd value to something:
SELECT COLUMN
FROM TABLE
WHERE COALESCE(NULLIF(COLUMN,''), 'COLUMN A') = 'COLUMN A';
Or better:
SELECT COLUMN
FROM TABLE
WHERE COLUMN IS NULL OR COLUMN = ''
Doesn't require any thinking/calculating to work out your selection logic. More maintainable, nicer for peer developers
*The above is generic advice for usual cases NOT involving boolean datatypes (which typically require some different treatment)
Now, you say you have to change the value to something. That requires an UPDATE statement. If this column is a boolean then it won't have a value of empty string. The blanks will be nulls:
UPDATE TABLE SET COLUMN = (some boolean) WHERE COLUMN IS NULL
If you don't want to permanently change the table data to something, but instead want to select it out as some value where a blank occurs, but keep the blanks stored in the table:
SELECT COALESCE(column, (some boolean)) FROM TABLE
Might be worth noting that not all versions of DB2 can return a boolean in a result set - this is quite typical of database vendors. Convert the boolean to something else representable using a case when, if your DB2 version is thus restricted
SELECT CASE WHEN column = TRUE THEN 'true' ELSE 'false' END FROM TABLE
I need to mask data in my tables, for example data like:
ABCDEFG
XYZABCD
LMNOPQR
Should appear like:
AB*****
XY*****
LM*****
What update query can I use? Also, can I use a single query for updating multiple columns?
You can just mask it when showing the data
select stuff(stuff(stuff(col,3,3,'*'),7,3,'*'),10,3,'*')) as col from table
Suppose the column you want to mask is called column from table table, than you can use the following query, which is standard in SQL, to update the value in the column:
update table
set column = substring(column from 1 for 2) || '****';
If on the other hand you want only to select the values to show them, you can use the following query:
select substring(column from 1 for 2) || '****'
from table;
I have a column x-property which has values xxx-abc, xxx-def, 123, mno ....etc.
I have another column isx.
I wish to update table to fill up column isx such that if the row in x-property column contains xxx then add abc, else add xyz.
I do not have SQL full text search in my table.
Any help is appreciated.
Instead of contains use like, because from the docs:
CONTAINS is a predicate used in the WHERE clause of a Transact-SQL SELECT statement to perform SQL Server full-text search on full-text indexed columns containing character-based data types.
This is your query:
update
table
set
isx =
case
when x-property like '%xxx%' then 'abc'
else 'xyz'
end
I understand that AS is used to create an alias. Therefore, it makes sense to have one long name aliased as a shorter one. However, I am seeing a SQL query NULL as ColumnName
What does this imply?
SELECT *, NULL as aColumn
Aliasing can be used in a number of ways, not just to shorten a long column name.
In this case, your example means you're returning a column that always contains NULL, and it's alias/column name is aColumn.
Aliasing can also be used when you're using computed values, such as Column1 + Column2 AS Column3.
When unioning or joining datasets using a 'Null AS [ColumnA] is a quick way to make sure create a complete dataset that can then be updated later and a new column does not need to be created in any of the source tables.
In the statement result we have a column that has all NULL values. We can refer to that column using alias.
In your case the query selects all records from table, and each result record has additional column containing only NULL values. If we want to refer to this result set and to additional column in other place in the future, we should use alias.
It means that "aColumn" has only Null values. This column could be updated with actual values later but it's an empty one when selected.
---I'm not sure if you know about SSIS, but this mechanism is useful with SSIS to add variable value to the "empty" column.
When using SELECT you can pass a value to the column directly.
So something like :
SELECT ID, Name, 'None' AS Hobbies, 0 AS NumberOfPets, NULL AS Picture, '' AS Adress
Is valid.
It can be used to format nicely a query output when using UNION/UNION ALL.
Query result can have a new column that has all NULL values. In SQL Server we can do it like this
SELECT *, CAST(NULL AS <data-type>) AS as aColumn
e.g.
SELECT *, CAST(NULL AS BIGINT) AS as aColumn
How about without using the the as
SELECT ID
, Name
, 'None' AS Hobbies
, 0 AS NumberOfPets
, NULL Picture
Usually adding NULL as [Column] name at the end of a select all is used when inserting into another table a calculated column based on the table you have just selected.
UPDATE #TempTable SET aColumn = Column1 + Column2 WHERE ...
Then exporting or saving the results to another table.