选择今天的日期作为默认值。

时间:2022-09-18 12:56:04

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".

我有一个问题,我不确定是否可能,但假设我有一个表格,包含Employeeid, Date和AttendanceStatus I。“现在”/“缺席”。

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

我可以获取所有日期或查询中指定日期的所有员工的状态。我的问题是,是否有可能在默认情况下得到今天的日期,所以每当我运行查询时,它会给我今天的数据行,而不是数据库中的所有记录。我想使用默认值,这样我就不用每天更改日期。谢谢提前

3 个解决方案

#1


2  

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)

#2


1  

You can apply where clause for current day;

你可以申请where条款为当天;

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

#3


0  

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

在这里,如果您没有在参数@MyDate中传递任何值,它将以当前日期为默认值。还可以使用以下内容

DECLARE @MyDate DATE
    SELECT
    *
    FROM YourTable
    WHERE 
DateField = CASE WHEN ISDATE(@MyDate) = 1 THEN @MyDate ELSE GETDATE() END

#1


2  

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)

#2


1  

You can apply where clause for current day;

你可以申请where条款为当天;

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

#3


0  

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

在这里,如果您没有在参数@MyDate中传递任何值,它将以当前日期为默认值。还可以使用以下内容

DECLARE @MyDate DATE
    SELECT
    *
    FROM YourTable
    WHERE 
DateField = CASE WHEN ISDATE(@MyDate) = 1 THEN @MyDate ELSE GETDATE() END