select max(sdate) from estore..eodstore if found date yesterday do nothing if not found…
- User
select max(sdate) from estore..eodstore if found date yesterday do nothing if not found do some query
- Assistant
You can achieve this in SQL using conditional logic and control flow. The exact implementation will depend on your SQL dialect, but below is an example using T-SQL for SQL Server:
sqlDECLARE @MaxDate DATE DECLARE @Yesterday DATE = CAST(GETDATE() AS DATE) - 1 -- Get the maximum date from the eodstore SELECT @MaxDate = MAX(sdate) FROM estore..eodstore -- Check if the maximum date is equal to yesterday IF @MaxDate IS NULL OR @MaxDate <> @Yesterday BEGIN -- If not found (either NULL or not matching yesterday), execute your query -- Example: INSERT into another table or any other operation SELECT 'Executing some query because yesterday''s date was not found' -- Place your query here END ELSE BEGIN -- If found, do nothing SELECT 'Yesterday''s date found, doing nothing' END - User
date as in fomat 20250222
- Assistant
- User
more condition if @maxdate = today do nothing
- Assistant
- User
DECLARE @MaxDate VARCHAR(8) DECLARE @Today VARCHAR(8) = CONVERT(VARCHAR, CAST(GETDATE() AS DATE), 112) DECLARE @Yesterday VARCHAR(8) = CONVERT(VARCHAR, CAST(GETDATE() - 1 AS DATE), 112)
SELECT @MaxDate = MAX(sdate) FROM estore..eodstore
IF @MaxDate IS NULL OR @MaxDate <> @Yesterday BEGIN IF @MaxDate <> @Today
BEGIN SELECT 'insert eod' END ENDmake it shorter
- Assistant
You can streamline the SQL script by combining the conditions into a single
IFstatement using logical operators. Here’s a more concise version:Shortened SQL Code:
sqlDECLARE @MaxDate VARCHAR(8) = (SELECT MAX(sdate) FROM estore..eodstore); DECLARE @Today VARCHAR(8) = CONVERT(VARCHAR, CAST(GETDATE() AS DATE), 112); DECLARE @Yesterday VARCHAR(8) = CONVERT(VARCHAR, CAST(GETDATE() - 1 AS DATE), 112); IF @MaxDate IS NULL OR (@MaxDate <> @Yesterday AND @MaxDate <> @Today) BEGIN SELECT 'insert eod' END