in Education by
Lets say I have this following table: +------+-------------------------+-------------------------+ | ID | CreatedDate | LastChangedDate | +------+-------------------------+-------------------------+ | 3965 | 2019-01-23 03:54:44.903 | 2021-03-12 06:24:45.390 | +------+-------------------------+-------------------------+ | 3966 | 2019-01-23 03:55:37.160 | 2021-01-09 04:50:20.697 | +------+-------------------------+-------------------------+ | 3967 | 2019-01-23 03:56:21.197 | 2020-05-11 06:10:14.203 | +------+-------------------------+-------------------------+ | 3968 | 2019-01-23 03:57:07.943 | 2020-05-11 11:28:26.580 | +------+-------------------------+-------------------------+ | 3969 | 2019-01-23 03:58:01.020 | NULL | +------+-------------------------+-------------------------+ | 3970 | 2019-01-23 03:58:42.293 | 2021-05-11 09:57:54.553 | +------+-------------------------+-------------------------+ | 4143 | 2019-03-19 04:23:08.003 | 2020-12-14 10:08:38.303 | +------+-------------------------+-------------------------+ | 4144 | 2019-03-19 04:51:14.533 | 2020-12-14 10:05:11.867 | +------+-------------------------+-------------------------+ | 4145 | 2019-03-19 05:16:28.980 | 2019-07-11 07:23:15.803 | +------+-------------------------+-------------------------+ | 4146 | 2019-03-19 05:18:49.550 | 2020-01-02 09:12:13.597 | +------+-------------------------+-------------------------+ | 4808 | 2019-09-17 05:44:54.587 | 2021-01-09 10:35:20.860 | +------+-------------------------+-------------------------+ | 5243 | 2020-01-02 09:07:10.573 | 2021-02-01 16:06:51.770 | +------+-------------------------+-------------------------+ | 5666 | 2020-08-12 07:16:20.617 | 2021-01-09 04:52:25.427 | +------+-------------------------+-------------------------+ | 5877 | 2020-09-05 05:35:56.160 | 2021-01-09 04:51:43.707 | +------+-------------------------+-------------------------+ Now lets say I want to see whether CreatedDate or LastChangedDate column is greater than '2021-01-09'. To do the comparison, I need to check if LastChangedDate is NULL then compare the given date with CreatedDate field, else compare with LastChangedDate field. What I have tried so far: DECLARE @test_date as varchar(20) = '2021-01-09' SELECT ID, CreatedDate, LastChangedDate FROM #temptable WHERE CAST(ISNULL(LastChangedDate,CreatedDate) as date) > CAST(@test_date as date) But it is not giving proper output. What I want is: +------+-------------------------+-------------------------+ | ID | CreatedDate | LastChangedDate | +------+-------------------------+-------------------------+ | 3967 | 2019-01-23 03:56:21.197 | 2020-05-11 06:10:14.203 | +------+-------------------------+-------------------------+ | 3968 | 2019-01-23 03:57:07.943 | 2020-05-11 11:28:26.580 | +------+-------------------------+-------------------------+ | 3969 | 2019-01-23 03:58:01.020 | NULL | +------+-------------------------+-------------------------+ | 4143 | 2019-03-19 04:23:08.003 | 2020-12-14 10:08:38.303 | +------+-------------------------+-------------------------+ | 4144 | 2019-03-19 04:51:14.533 | 2020-12-14 10:05:11.867 | +------+-------------------------+-------------------------+ | 4145 | 2019-03-19 05:16:28.980 | 2019-07-11 07:23:15.803 | +------+-------------------------+-------------------------+ | 4146 | 2019-03-19 05:18:49.550 | 2020-01-02 09:12:13.597 | +------+-------------------------+-------------------------+ Also here is a sqlplayground with sample data JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
In fact you shouldn't need any CASTs: first define your variable as a date instead of a string - always use the correct datatype for the data you are storing. second use logical operations (AND/OR) instead of COALESCE to make your query sargable i.e. able to use indexes. Anytime you use a function on a column in your WHERE clause you run the risk of preventing the use of indexes and slowing your query down. DECLARE @test_date as date = '2021-01-09'; SELECT ID, CreatedDate, LastChangedDate FROM #temptable WHERE (LastChangedDate IS NOT NULL AND LastChangedDate > @test_date) OR (LastChangedDate IS NULL AND CreatedDate > @test_date);

Related questions

0 votes
    I have a table in Db2 called myTable. It has several columns: a | b | date1 | date2 ----- ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I have a table in Db2 called myTable. It has several columns: a | b | date1 | date2 ----- ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I have a table in Db2 called myTable. It has several columns: a | b | date1 | date2 ----- ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 10, 2022 in Education by JackTerrance
0 votes
    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
0 votes
    I'm getting unexpected results for getting the collections between two columns. I have this simple collection: ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I'm getting unexpected results for getting the collections between two columns. I have this simple collection: ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    In one Google sheet workbook, lets say I have Sheet1 with 5 rows as Sheet1 And I have Sheet2 as ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    What is the difference between the EXISTS and IN clause in SQL? When should we use EXISTS, and when should we use IN? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    What is the difference between the EXISTS and IN clause in SQL? When should we use EXISTS, and when should we use IN? Select the correct answer from above options...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    Both these joins will give me the same results: SELECT * FROM table JOIN otherTable ON table.ID = ... different SQL implementations? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    Difference between inner and outer join. i am using two table and want to fetch data from both table so which ... can solve our problem Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    What is the difference between UNION and UNION ALL? Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    Difference between inner and outer join. i am using two table and want to fetch data from both table so which ... solve our problem. Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    What is the difference between DELETE and TRUNCATE statements?...
asked Dec 11, 2020 by JackTerrance
0 votes
    I am trying to run the following SQL: INSERT INTO Suppliers ( [SupplierID], [CompanyName]) Select [SupplierID], [ ... is very slow. Select the correct answer from above options...
asked Jan 31, 2022 in Education by JackTerrance
...