Error with syntax replacing values in the query output - sql

I work within the SQL database, but am self taught, so there are a few holes in my knowledge. I have this query that although I would think is simple, I just cant get it right.
I have two columns, one with a 'Name', and the other 'Privacy'.
I am trying to to get a result where if the Privacy column has a numerical value (3) in it, all these rows will return a 'name suppressed' in the Name column. And all the rows with a NULL in the Privacy column are left alone.
But I don't want to update the database, just suppress the name in the query.
I have tried, replace, case, if then and a few more with no luck. Can someone please help me.
Thanks
Michele

You can go for simple CASE expression. I am assuming you only want to suppress if privacy = 3.
SELECT CASE WHEN Privacy = 3 THEN 'Name Suppressed'
ELSE Name
END AS Name, Privacy
FROM TableName

SELECT
CASE
WHEN [Privacy] IS NULL THEN [Name]
ELSE N'name suppressed'
END AS [Name],
[Privacy]
FROM
[dbo].[YourTable]

Related

SQL CASE WHEN in WHERE clause

searched for this problem, but couldn't find a question for exactly this problem.
I have a stored procedure that fetches data from a large amount of tables.
In my SP I have a parameter indicating the ID. Default Value of that ID is zero.
I want to return all values regarding this parameter in my query, unless a value is provided.
SELECT mT.*
FROM myTable mT
(various JOINS)
WHERE
CASE #myParameter
WHEN 0 THEN 1 = 1
ELSE mT.ID = #myParameter
END
Unfortunately I get the error message "Incorrect syntax near '='".
Things that are fixed: It has to be a SP, I get just one parameter where I can set a default value in case it is not filled. If it is not filled this part of the where clause shall not apply (there are more statements in the WHERE clause.
If I did not see a question that contained this problem please give me a hint where to find it.
Best Regards and thanks in advance!
You don't need CASE:
SELECT mT.*
FROM myTable mT
(various JOINS)
WHERE
#myParameter= 0 OR mT.ID = #myParameter

Replace value in result by a specific value

I need to make a query to collect some data from a database via SQL. In this data there is 1 value used as collection value. This are ID's of courses given. Sometimes a course can be given about f.e. Office. But people can do a course there for word, excel, powerpoint... But this is all given in 1 course by 1 tutor. Still for statistics I need to know if they participated the course for Word, Excel, Powerpoint ...
Is it possible to replace values in the resultset? With this i mean something like this:
if value = courseValue ==> replace value with specific courseValue (I can get the value via a subquery)
I hope this makes my problem clear and i appriciate all the help!
You can use a case statement in your select to return something other than the course id that is on the row. For example:
SELECT
field1 AS 'Name',
CASE
WHEN field2 = 'Foo'
THEN 'Bar'
WHEN field2 = 'Lorem'
THEN 'Ipsum'
ELSE 'Some Value'
END
AS 'Type',
field3 AS 'Description'
FROM table
If I understand you correctly, you will need something along the lines of this:
Create a new table with "courseID" and "replacementID" columns, fill it for the cases where there is a replacement
In your query do an outer join with this table over the courseID fields and also return the "replacementID", which can be null is there is no replacement
Use either the replacementID if it isn't null or the courseID

SQL Case statement returning true although should be false

I have a SQL statement that is something like this
SELECT t1.*
CASE
WHEN t1.COL1 = 0 THEN 0
WHEN
(t1.COL2 = 'val' OR t1.COL3 = 'val' etc) AND ((SELECT COUNT(*) FROM t1 WHERE etc...) > 0)
THEN Run same sub query
WHEN
something else
THEN defaultvalue
END as brokencase
Now, the second WHEN statement above, when run alone, is returning zero. However when it is run here, I am getting a bunch of null values back because that statement is somehow returning true.
So why is the count statement returning zero, but the WHEN statement thinks its true? I have left out a lot of details here for privacy reasons, but this is the general problem.
Usually when I see this, the programmer is missing an ELSE or DEFAULT case in their statements. To find out what scenario you're not capturing, set up a select like this:
SELECT DISTINCT columnTested1, columnTested2, columnTestedX, case statement
FROM blah
Show all the columns being tested, then your case, what you'll see in the result is the combination of columns that are causing your NULL. You need to capture those in your case statement or you'll continue to miss those options
When you test different columns in the different options, you might need multiple else statements to catch them all. Once you've identified the combination of columns causing you a problem, it becomes dead simple to fix the case statement.
If you want to exclude results that have null values you will have to make sure you are checking for that in your WHEN condition. You haven't specified your WHERE clause in your count query so I can't say for sure if this is the problem. It would look something like this:
SELECT COUNT(*) FROM t1 WHERE ExcludeValue IS NOT NULL

How to select an empty result set?

Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted.
I'm using a stored procedure in MySQL, with a CASE statement.
In the ELSE clause of the CASE ( equivalent to default: ) I want to select and return an empty result set, thus avoiding to throw an SQL error by not handling the ELSE case, and instead return an empty result set as if a regular query would have returned no rows.
So far I've managed to do so using something like:
Select NULL From users Where False
But I have to name an existing table, like 'users' in this example.
It works, but I would prefer a way that doesn't break if eventually the table name used is renamed or dropped.
I've tried Select NULL Where False but it doesn't work.
Using Select NULL does not return an empty set, but one row with a column named NULL and with a NULL value.
There's a dummy-table in MySQL called 'dual', which you should be able to use.
select
1
from
dual
where
false
This will always give you an empty result.
This should work on most DBs, tested on Postgres and Netezza:
SELECT NULL LIMIT 0;
T-SQL (MSSQL):
SELECT Top 0 1;
How about
SELECT * FROM (SELECT 1) AS TBL WHERE 2=3
Checked in myphp, and it also works in sqlite and probably in any other db engine.
This will probably work across all databases.
SELECT * FROM (SELECT NULL AS col0) AS inner0 WHERE col0 IS NOT NULL;
SELECT TOP 0 * FROM [dbo].[TableName]
This is a reasonable approach to constant scan operator.
SELECT NULL WHERE FALSE;
it works in postgresql ,mysql, subquery in mysql.
How about this?
SELECT 'MyName' AS EmptyColumn
FROM dual
WHERE 'Me' = 'Funny'
SELECT * FROM (SELECT NULL) WHERE 0
In PostgreSQL a simple
SELECT;
works. You won't even get any columns labeled 'unknown'.
Note however, it still says 1 row retrieved.

How can I limit columns returned based on their value?

I have a table which looks like
index customer_number ABC CWD ROE BEE
1 1 0 0 0 1
and I want to return only the field names that have value 1 in this case 'BEE'
I found that by SHOW FIELDS I can get the names of the fields but how I can say show the field names where field value = 1?
I would use CASE statement here.
SELECT
index, customer_number,
CASE
WHEN abc=0 THEN 'abc'
WHEN cwd=0 THEN 'cwd'
END
FROM
table_name
You can't do this in a general way.
What you can do is write a sql statement like this:
select index, customer_number, decode (ABC, 1, "ABC", null) || decode (CWD, 1, "CWD", null) || decode (ROE, 1, "ROE", null) || decode (BEE, 1, "BEE", null) from aTable
It will display the column names for each entry where the value equals to one. It is oracle sql, so if you use a different rdbms the syntax will vary.
The beat it to death answer is to use CASE statements, one for each column. Something like:
SELECT CASE WHEN index=1 THEN "index" ELSE "no_index" END as i,
CASE WHEN customer=1 THEN "customer" ELSE "no_customer" END as c,
CASE WHEN ...
This is not something that SQL was really meant to do, and would be better done with application logic.
That said, if you really wanted to do it, you would probably need to involve a temp table and a SPROC:
Get the row and determine which fields are set.
Use that information to create a temp table with only the set fields.
Insert the data into that temp table, then select the rows from there.
It would be a huge mess of SQL to replace what would amount to only a few lines of application code. Probably not worth it.