Return a value if no rows are found in Microsoft tSQL
SELECT CASE WHEN COUNT(1) > 0 THEN 1 ELSE 0 END AS [Value]
FROM Sites S
WHERE S.Id = @SiteId and S.Status = 1 AND
(S.WebUserId = @WebUserId OR S.AllowUploads = 1)
SQL query - Return 0 for the column if no record found
Try below query:
SELECT A.Customer, ISNULL(B.Address, 0),
ISNULL(B.[Number of lines],0), ISNULL(B.Date, '30-05-2022') Date
FROM
(
SELECT DISTINCT Customer
FROM table_name
) A
LEFT JOIN table_name B
ON A.Customer = B.Customer
AND B.Date = '30-5-2022'
This will output all the customers present in the table. You can filter the customers with WHERE
clause in the end of the above query based on your requirement.
dbfiddle Link
SQL select return 0 if no records found, else return value
You can use this
SELECT ISNULL(( SELECT TOP 1
[avail]
FROM [table1]
where [name] = 'abc'
order by [datetime] desc), 0) AS [avail]
Related Topics
Get the Last Day of the Month in SQL
How to Create a Foreign Key in SQL Server
SQL (Oracle): Order by and Limit
SQL Server Ignore Case in a Where Expression
The Parameterized Query Expects the Parameter Which Was Not Supplied
MySQL Returning the Top 5 of Each Category
Oracle SQL - Identify Sequential Value Ranges
Paging SQL Server 2005 Results
How to Compare SQLite Timestamp Values
Sub Queries in Check Constraint
How to Calculate a Running Total in SQL Without Using a Cursor
How to Return Multiple Rows from the Stored Procedure? (Oracle Pl/Sql)
How to Declare Global Variable in SQL Server..
SQL Server Operating System Error 5: "5(Access Is Denied.)"
SQL Server Like Containing Bracket Characters
Coldfusion Adding Extra Quotes When Constructing Database Queries in Strings