if date is given display data based on date other wise all record in select query
Loading
if date is given display data based on date other wise all record in select query
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal JoshiRank #377 · 6.7K reputationPosted Oct 27, 2023, 4:59 AM EDT
Hello Lejo,
You can achieve the task by writing the stored procedure or if it's in code you can write a dynamic query. please find the below SQL server code to get the result.
declare @sql nvarchar(max) set @sql = N'select * from Tbl_StampDutyException1 where 1 = 1' if len(isnull(@dateFilter, '')) > 0 set @sql = @sql + N' and date = ' + CONVERT(Datetime, @dateFilter, 120) -- add all parameters; you need to cast them to expected type if they have another type exec (@sql)Thanks
Vishal Joshi
Uday DodiyaRank #239 · 10.3K reputationPosted Oct 27, 2023, 5:13 AM EDT
Hi lejo,
Use the following query
DECLARE @YourDate DATE = '2023-10-27'; -- Replace with the desired date SELECT * FROM your_table WHERE (your_date_column = @YourDate OR @YourDate IS NULL); -- If @YourDate has a specific date, it will filter records for that date. -- If @YourDate is NULL, it will return all records.If your parameter value is NULL, then it returns all records. For example,
DECLARE @YourDate DATE = NULL; -- Replace with the desired date SELECT * FROM your_table WHERE (your_date_column = @YourDate OR @YourDate IS NULL); -- If @YourDate has a specific date, it will filter records for that date. -- If @YourDate is NULL, it will return all records.Jignesh KumarRank #91 · 22.9K reputationPosted Oct 27, 2023, 6:06 AM EDT
Hi Lejo,
You can use this query to get conditional date filter, Pass your date parameter null then use below query
SELECT * FROM your_table WHERE @your_date_parameter IS NULL OR dateColumn == @your_date_parameterAnandu G NathRank #681 · 3.5K reputationPosted Jan 3, 2024, 12:25 AM EST
You can achieve this in SQL by using a conditional
WHEREclause to filter records based on a given date or show all records if no date is provided.SELECT *
FROM your_table
WHERE
(your_date_column = @given_date OR @given_date IS NULL)
OR
(@given_date IS NULL) -- Include this condition to show all records when no date is given
Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 3, 2024, 2:44 AM EST
Hello Lejo,
Try below query.
DECLARE @provided_date date = NULL; -- Set @provided_date to a specific date if needed DECLARE @sql nvarchar(max); SET @sql = N'SELECT * FROM your_table'; IF @provided_date IS NOT NULL BEGIN SET @sql = @sql + N' WHERE your_date_column = @provided_date'; END EXEC sp_executesql @sql, N'@provided_date date', @provided_date;Thanks
Satyaprakash SamantarayRank #101 · 21.1K reputationPosted Jan 3, 2024, 3:30 AM EST
https://www.c-sharpcorner.com/article/filter-records-based-on/
ALTER PROCEDURE [dbo].[GetDataByDate] @status varchar(10), @Fromdate DATETIME=null, @Todate DATETIME=null AS BEGIN if @status ='GET' BEGIN Set NoCount ON Declare @SQLQuery AS NVarchar(4000) Declare @ParamDefinition AS NVarchar(2000) Set @SQLQuery ='SELECT P.*,C.catName,g.Vch_GenderName as Gender from [dbo].[Post] P Join Tbl_Gender g on P.int_GenderID = g.int_GenderID JOIN [dbo].[Category] C ON P.catId=C.catId where p.catid<>0' If (@Fromdate Is Not Null) AND (@Todate Is Not Null) Set @SQLQuery = @SQLQuery + 'And (p.fromdt BETWEEN @Fromdate AND @Todate)' Set @ParamDefinition = '@Fromdate DATETIME,@Todate DATETIME' Execute sp_Executesql @SQLQuery,@ParamDefinition,@Fromdate,@Todate END ENDJayraj ChhayaRank #304 · 8.2K reputationPosted Jan 3, 2024, 12:50 AM EST
To display data based on a specific date in a SQL SELECT query, you can use the WHERE clause to filter the records. If a date is given, you can specify the date condition in the WHERE clause. If no date is given, you can omit the date condition, and the query will return all records.
Here's an example of how you can achieve this in SQL:
-- Displaying data based on a specific date SELECT * FROM your_table WHERE date_column = '2022-01-01'; -- Displaying all records SELECT * FROM your_table;