获取两个日期之间每个星期五的数据

时间:2014-10-30 17:25:33

标签: sql sql-server database

我有一个SQL-Server数据库,我希望在两个给定日期之间从列VL_DR获取数据,日期是星期五。

"SELECT VL_DR FROM table_VL WHERE date>='" + date_start
                + "' AND date<='" + date_end
                + "' AND DATENAME(dw, date)='Friday'"

但是在请求之后我有一个空列表,而我应该有一些值。 我应该如何解决以获得正确的请求?

3 个答案:

答案 0 :(得分:2)

如果是SQL Server

尝试

SELECT VL_DR FROM table_VL WHERE ([date] BETWEEN date_start and date_end) AND LOWER(DATENAME(dw, [date]))='friday'

如果是MySQL

尝试

SELECT VL_DR FROM table_VL WHERE ([date] BETWEEN date_start and date_end) AND DAYNAME([date])='Friday'

答案 1 :(得分:0)

假设MySQL。 DAYNAME应该有效

"SELECT VL_DR FROM table_VL WHERE date>='" + date_start
            + "' AND date<='" + date_end
            + "' AND DAYNAME(date)='Friday'"

示例:

SELECT DAYNAME('2008-05-15');

结果Thursday

答案 2 :(得分:0)

此代码将根据我拥有的共享代码数据库中的数据工作。这适用于MS-SQL。

SELECT
    CloseDate, DatePart(WEEKDAY,CloseDate) AS DAY_OF_WEEK_No , SharePrice 
FROM
    SharePrice 
WHERE
    CloseDate BETWEEN '01 October 2014' AND '31 October 2014'
AND
    DATEPART(WEEKDAY,CloseDate) = 6 -- 1=Sunday; 7=Saturday
AND
    ShareCode = 'AGL'