Reputation: 1495
I have a question which I'm not sure if possible or not but let's say I have a table with Employeeid, Date and AttendanceStatus i.e "Present"/"Absent".
I can get the status of all employees for all dates or for dates I specify in the query. My question is that is it possible to get today's date by default so whenever I run the query it give me the data rows for today's instead of all the records from the database. I want to use default so that I don't have to change the date everyday. Thanks in advance
Upvotes: 2
Views: 59
Reputation: 37347
Try this:
select * from TABLE_NAME where
where cast([Date] as date) = cast(getdate() as date)
Also, as already mentioned, you could create a view:
create view V_TABLE_NAME as
select * from TABLE_NAME where
where cast([Date] as date) = cast(getdate() as date)
Upvotes: 2
Reputation: 13146
You can apply where clause for current day;
select * from table where
Date > cast(getdate() as date) and
Date < DATEADD(day,1,cast(getdate() as date))
Or you can create a view;
create view v_table
as
select * from table where
Date > cast(getdate() as date) and
Date < dateadd(day,1,cast(getdate() as date))
Then query;
select * from v_table
Upvotes: 1
Reputation: 8033
Try This Logic
DECLARE @MyDate DATE
SELECT
*
FROM YourTable
WHERE DateField = ISNULL(@MyDate,GETDATE())
Here, If You are not passing any values in the Parameter @MyDate, it will take Current Date as Default. The following can also be used
DECLARE @MyDate DATE
SELECT
*
FROM YourTable
WHERE
DateField = CASE WHEN ISDATE(@MyDate) = 1 THEN @MyDate ELSE GETDATE() END
Upvotes: 0