I want to select records by date using variable.
DECLARE @estDate datetime
SET @estDate = (SELECT GETDATE())
SELECT Meta FROM T_TData WHERE Estt_Date=@estDate
My table:
Meta | Estt_Date |
---|---|
12 | 2023-01-31 16:34:36.143 |
99 | 2023-01-31 16:34:36.143 |
45 | 2022-12-31 16:34:30.145 |
95 | 2023-01-25 16:34:36.143 |
My meta column in table int and Estt_Date is datetime.
How can I select records of particular date using variable.
CodePudding user response:
You can do this. Please find the following example
CREATE TABLE #tempData
(
Meta INT,
Estt_Date DATETIME
)
INSERT INTO #tempData(Meta,Estt_Date)
VALUES
(12,'2023-01-31 16:34:36.143'),
(12,'2023-01-31 16:34:36.143'),
(12,'2022-12-31 16:34:30.145'),
(12,'2023-01-25 16:34:36.143')
SELECT * FROM #tempData
DECLARE @estDate DATE = CAST(GETDATE() AS DATE)
SELECT * from #tempData WHERE CAST(Estt_Date AS DATE)=@estDate
DROP TABLE IF EXISTS #tempData
CodePudding user response:
DECLARE @ESTDATE DATE
SET @ESTDATE=GETDATE()
SELECT META FROM T_TDATA WHERE ESTT_DATE>=@ESTDATE AND ESTT_DATE<DATEADD(DD,1,@ESTDATE)