Doonie Darkoo
Doonie Darkoo

Reputation: 1495

Select today's date as default

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

Answers (3)

Michał Turczyn
Michał Turczyn

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

Emre Kabaoglu
Emre Kabaoglu

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

Jayasurya Satheesh
Jayasurya Satheesh

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

Related Questions