Is it possible in an SQL query to only show a field if another field has data? For example, if Field1 <> '', then show the value in Field2 else don't show the value?
It can be done using a case statement. (At least in SQL Server)
select case when Field1 <> ''
then Field2
end as Field2
from YourTable
Sure (this works in Oracle and SQLite):
select
field1,
(case
when field1 is null then null
else field2
end) field2_wrapped
from my_table
if 'has no data' means the empty string (''), you need to use this statement:
SELECT Filed2 FROM Table1 WHERE Filed1<>''
If 'no data' means NULL value, you need use
SELECT Filed2 FROM Table1 WHERE NOT (Filed2 IS NULL)
Take a look at Standard SQL functions COALESCE() and NULLIF():
COALESCE(NULLIF(Field1, ''), Field2)
Related
Is it possible to add a select statement in case or if function in SQL
select case when :A='do' then (select col1 from table1) else 'n/a' end;
or
select if(:A='do',(select col1 from table1),'N/A');
If my parameter is 'do' it should display all the value in table or else it should just display 'N/A'.
Please help me. Thanks!
If this is Oracle, as per your tag, then neither of your queries are valid as they're not actually selecting from a table.
Perhaps what you're after is something like:
select case when :A='do' then col1 else 'n/a' end col
from table1;
Or maybe you're after something like:
select col1
from table1
where :A != 'do'
union all
select 'N/A' col1
from dual
where :A = 'do';
You didn't provide any example data, so I'm not sure if what you're trying to do is make all the values of col1 appear as 'N/A' if the bind variable is "do" or whether you only want a single row containing 'N/A'.
Hello I have a query where I want to select the value of one of two fields depending if one is empty.
field1 and field2
I want to select them as complete_field
IF field1 is empty, then complete_field is field2
ELSE complete_field is field1
In php it would be done like:
$complete_field = $field1 == '' ? $field2 : $field1;
How would I do this in PostgreSQL?
I tried:
SELECT
(IF field1 = '' THEN field2 ELSE field1) AS complete_field
FROM
table
But it doesnt work.
Please help me :) Thanks
Use CASE WHEN .. THEN .. ELSE .. END, e.g.:
SELECT
(CASE WHEN (field1 IS NULL OR field1 = '') THEN field2 ELSE field1 END)
FROM table;
Check it out at the official docs.
Try COALESCE with NULLIF:
SELECT COALESCE(NULLIF(field1, ''), field2) AS the_field FROM my_table;
The ANSI SQL function Coalesce() is what you want.
select Coalesce(field1,field2)
from table;
I am trying to run an sql statement on an iSeries that will output retuls based on a type parameter I pass in.
Just say mytable has a field called field1. field1 contains Y,N and NULL values.
A type of 'Y' should return just 'Y' values.
A type of 'N' should return not 'Y' values. (ie. Null, N and any other junk in the field)
I tried this...
select *
from mytable
where field1 in case when :type = 'Y' then 'Y'
else (select field1 from mytable where field1 <> 'Y') end
However, this does not work.
I believe the logic you are looking for is this:
SELECT *
FROM myTable
WHERE (:type = 'Y' AND field1 IS NOT null AND field1 = 'Y')
OR (:type <> 'Y' AND (field1 IS null OR field1 <> 'Y'))
(keep in mind the fact that short-circuit logic is not garuanteed with SQL...)
Remember that null doesn't really compare to anything, and it's best to call out the fact that you actually want it (in the second case).
I have a query in Access and would like to know if it were possible to use the where not exists clause to display a specific text for each field when there are no returned rows.
Example query:
Select Field1, Field2, Field3
From TableA
Where Field1 = "test";
If there are no returned results I would like the following to return:
Field1 = "test"
Field2 = "not provided"
Field2 = "not provided"
How about:
SELECT Field1, Field2
FROM Table
WHERE ID=3
UNION ALL SELECT DISTINCT "None","None" FROM AnyTableithAtLeastOneRow
WHERE 3 NOT IN (SELECT ID FROM Table)
The usual way to do what you're asking is:
Select Field1, isnull(Field2, 'Not Provided'), isnull(Field3, 'Not Provided')
edit
whoops, you're using Access, in that case the equivalent function is "nz" (what?! :p)
Select Field1, nz(Field2, 'Not Provided'), nz(Field3, 'Not Provided')
I have to write a query that performs a union between two tables with similar data. The results need to be distinct. The problem I have is that some fields that should be the same are not when it comes to empty values. Some are indicated as null, and some have empty string values. My question is, is there a better way to perform the following query? (without fixing the actual data to ensure proper defaults are set, etc) Will using the Case When be a big performance hit?
Select
When Column1 = '' Then NULL Else Column1 as [Column1],
When Column2 = '' Then NULL Else Column2 as [Column2]
From TableA
UNION ALL
Select
When Column1 = '' Then NULL Else Column1 as [Column1],
When Column2 = '' Then NULL Else Column2 as [Column2]
From TableB
I don't think it would make any difference in performance, but NULLIF is another way to write this and, IMHO, looks a little cleaner.
Select
NULLIF(Column1, '') as [Column1],
NULLIF(Column2, '') as [Column2]
From TableA
UNION
Select
NULLIF(Column1, '') as [Column1],
NULLIF(Column2, '') as [Column2]
From TableB
Use UNION to remove duplicates - it's slower than UNION ALL for this functionality:
SELECT CASE
WHEN LEN(LTRIM(RTRIM(column1))) = 0 THEN NULL
ELSE column1
END AS column1,
CASE
WHEN LEN(LTRIM(RTRIM(column2))) = 0 THEN NULL
ELSE column2
END AS column2
FROM TableA
UNION
SELECT CASE
WHEN LEN(LTRIM(RTRIM(column1))) = 0 THEN NULL
ELSE column1
END,
CASE
WHEN LEN(LTRIM(RTRIM(column2))) = 0 THEN NULL
ELSE column2
END
FROM TableB
I changed the logic to return NULL if the column value contains any number of spaces and no actual content.
CASE expressions are ANSI, and more customizable than NULLIF/etc syntax.
A Case should perform fine, but IsNull is more natural in this situation. And if you're searching for distinct rows, doing a union instead of a union all will accomplish that (thanks to Jeffrey L Whitledge for pointing this out):
select IsNull(col1, '')
, IsNull(col2, '')
from TableA
union
select IsNull(col1, '')
, IsNull(col2, '')
from TableB
You can keep your manipulation operations separate from the union if you do whatever manipulation you want (substitute NULL for the empty string) in a separate view, then union the views.
You shouldn't have to apply the same manipulation on both sets, though.
If that's the case, union them first, then apply the manipulation to the resulting, unioned set once.
Half as much manipulation code to support that way.