Home
Q&A
Education
Technology
Credit
General
Health
Insurance
Questions
Ask a Question
How do I perform an IF…THEN in an SQL SELECT?
Home
Education
How do I perform an IF…THEN in an SQL SELECT?
asked
Jan 28, 2022
in
Education
by
JackTerrance
How do I perform an IF...THEN in an SQL SELECT statement? For example: SELECT IF(Obsolete = 'N' OR InStock = 'Y' ? 1 : 0) AS Saleable, * FROM Product Select the correct answer from above options
sql
sql-server
tsql
if-statement
case
1
Answer
0
votes
answered
Jan 28, 2022
by
JackTerrance
Best answer
Follow the below steps to perform IF and THEN in the SQL statement. Want to learn SQL from basics! Here's the right video for you on SQL provided by Intellipaat: For Simple case you can use this code: SELECT CASE WHEN THEN WHEN THEN ELSE END AS FROM
For extended case you can use this: SELECT CASE WHEN THEN WHEN THEN ELSE END AS FROM CASE statement: It goes through conditions and returns a value when the first condition is met (like an IF-THEN-ELSE statement). Therefore, if a condition is true, it will stop reading and return the result. If none of the conditions are met, then it returns the value in the ELSE clause. Refer to this video to get a clear picture of CASE STATEMENT in SQL. Case Statement returns NULL, if there is no ELSE part and no conditions are true. You can even use case statements in an ORDER BY clause for better ordering. You can even use Case Statement in an order by clause for better ordering.
Related questions
0
votes
Q: How do I UPDATE from a SELECT in SQL Server?
In SQL Server, it's possible to insert into a table using a SELECT statement: INSERT INTO Table (col1, col2, ... .id = other_table.id Select the correct answer from above options...
asked
Jan 26, 2022
in
Education
by
JackTerrance
sql
sql-server
tsql
select
0
votes
Q: How can I list all foreign keys referencing a given table in SQL Server?
I need to remove a highly referenced table in a SQL Server database. How can I get a list of all the ... of the management studio.) Select the correct answer from above options...
asked
Jan 28, 2022
in
Education
by
JackTerrance
sql
sql-server
tsql
0
votes
Q: Should I use != or <> for not equal in T-SQL?
I have seen SQL that uses both != and for not equal. What is the preferred syntax and why? I like !=, ... reminds me of Visual Basic. Select the correct answer from above options...
asked
Jan 28, 2022
in
Education
by
JackTerrance
sql
sql-server
tsql
0
votes
Q: How to return only the Date from a SQL Server DateTime datatype
SELECT GETDATE() Returns: 2008-09-22 15:24:13.790 I want that date part without the time part: 2008-09-22 00: ... How can I get that? Select the correct answer from above options...
asked
Jan 28, 2022
in
Education
by
JackTerrance
sql
sql-server
tsql
date
datetime
0
votes
Q: Inserting multiple rows in a single SQL query?
I have multiple set of data to insert at once, say 4 rows. My table has three columns: Person, Id and ... a single SQL statement? Select the correct answer from above options...
asked
Jan 26, 2022
in
Education
by
JackTerrance
sql
sql-server
tsql
insert
0
votes
Q: Inserting multiple rows in a single SQL query?
I have multiple set of data to insert at once, say 4 rows. My table has three columns: Person, Id and ... a single SQL statement? Select the correct answer from above options...
asked
Jan 26, 2022
in
Education
by
JackTerrance
sql
sql-server
tsql
insert
0
votes
Q: Insert results of a stored procedure into a temporary table
How do I do a SELECT * INTO [temp table] FROM [stored procedure]? Not FROM [Table] and without defining [ ... not supply any columns. Select the correct answer from above options...
asked
Jan 28, 2022
in
Education
by
JackTerrance
sql
sql-server
sql-server-2005
tsql
stored-procedures
0
votes
Q: SQL User Defined Function Within Select
I have a user defined function in SQL called getBuisnessDays it takes @startdate and @enddate and returns the number of ... WHERE ... Select the correct answer from above options...
asked
Feb 8, 2022
in
Education
by
JackTerrance
sql
sql-server
select
user-defined-functions
0
votes
Q: SQL User Defined Function Within Select
I have a user defined function in SQL called getBuisnessDays it takes @startdate and @enddate and returns the number of ... WHERE ... Select the correct answer from above options...
asked
Feb 5, 2022
in
Education
by
JackTerrance
sql
sql-server
select
user-defined-functions
0
votes
Q: Merge SQL INSERT with Reference Table
I need to synchronize two tables. TableA Id Name TableB Id Name RefID --It's a Foreign key, defined as ... MATCHED BY TARGET THEN. Select the correct answer from above options...
asked
Feb 8, 2022
in
Education
by
JackTerrance
sql
sql-server-2008
tsql
ssis
0
votes
Q: Merge SQL INSERT with Reference Table
I need to synchronize two tables. TableA Id Name TableB Id Name RefID --It's a Foreign key, defined as ... MATCHED BY TARGET THEN. Select the correct answer from above options...
asked
Feb 5, 2022
in
Education
by
JackTerrance
sql
sql-server-2008
tsql
ssis
0
votes
Q: What should I learn for SQL Developer?
Could someone tell me what I should learn to become a SQL Developer? Select the correct answer from above options...
asked
Jan 6, 2022
in
Education
by
JackTerrance
sql
ms-sql
sql-server
0
votes
Q: Where can I learn Microsoft SQL Server?
Could anybody tell me where I can learn Microsoft SQL Server? Select the correct answer from above options...
asked
Jan 6, 2022
in
Education
by
JackTerrance
sql
ms-sql
sql-server
0
votes
Q: Add a column with a default value to an existing table in SQL Server
How can a column with a default value be added to an existing table in SQL Server 2000 / SQL Server 2005? Select the correct answer from above options...
asked
Jan 26, 2022
in
Education
by
JackTerrance
sql
sql-server
sql-server-2005
sql-server-2000
0
votes
Q: How to get first character of a string in SQL?
I have a SQL column with a length of 6. Now want to take only the first char of that column. Is there any ... in SQL to do this? Select the correct answer from above options...
asked
Feb 8, 2022
in
Education
by
JackTerrance
sql
sql-server
sql-server-2005
...