English 中文(简体)
鉴于时间有限,我利用古典正文获得第一个月和最后一个月。
原标题:Given a datehow do I get the first and the last date of that month using classic asp

I have a date field in the database that store values in format mm/dd/yyy. Using this how can I get the first and the last day of a given date. I m using this following code to display records from database

<%
sql="select * from order_details where emp_name= "&session("emp")&"  order by 1,2,3,4,5,6,7"
        rs.open sql,con,1,2
        do while not rs.eof
        session("wk")=weekdayname(weekday((rs("date"))))
    %>
<tr>

    <td align="center"><%=rs("emp_name")%></td>
    <td align="center"><%=rs("food_had")%></td>
    <td align="center"><%=rs("quantity")%></td>
    <td align="center"><%=session("wk")%></td>
    <td align="center"><%=day(rs("date"))&"/"&month(rs("date"))&"/"&year(rs("date"))%></td>
    <td align="center"><%=rs("grand_total")%></td>

</tr>
<%
        rs.movenext
        loop
        rs.close
%>

“会议”给我一个星期日的名称。

Now That I have the code I edited the query so that i can use firstdayofthemonth and lastdatofthemonth to display records but its not working this is what I used

<%
sql="select * from order_details where emp_name= "&session("emp")&"  and date BETWEEN  "&session("firstDayOfMonth")&"  AND  "&session("firstDayOfMonth")&" "
        rs.open sql,con,1,2
        do while not rs.eof
        session("m")=monthname(month((rs("date"))))
        session("firstDayOfMonth") = rs("date") + 1 - day(rs("date"))
        session("lastDayOfMonth") = dateadd("m", 1, session("firstDayOfMonth") ) - 1
    %>   
<tr>   

    <td align="center"><%=rs("emp_name")%></td>   
    <td align="center"><%=rs("food_had")%></td>   
    <td align="center"><%=rs("quantity")%></td>   
    <td align="center"><%=session("wk")%></td>   
    <td align="center"><%=day(rs("date"))&"/"&month(rs("date"))&"/"&year(rs("date"))%></td>   
    <td align="center"><%=rs("grand_total")%></td> 
    <td align="center"><%=session("firstDayOfMonth")%></td>   
    <td align="center"><%=session("lastDayOfMonth")%></td> 


</tr>
<%
        rs.movenext
        loop
        rs.close
    %>

if I remove "and date BETWEEN "&session("firstDayOfMonth")&" AND "&session("firstDayOfMonth")&" " from the query shows the first and the last date...but I want to display records using firstdate and lastdate ...am I misssing something in my query on top..

问题回答

您可以计算第一个数字。 DayOfMonth非常容易地从“日期”变数中扣除日数。

接着,到月的最后一天,你又增加了一个月,以日子(日期)为功能,并用一天为后继:

        firstDayOfMonth = rs("date") + 1 - day(rs("date"))
        lastDayOfMonth = dateadd("m", 1, firstDayOfMonth ) - 1

我在你们的法典中将它树立如下榜样:

<%   
sql="select * from order_details where emp_name= "&session("emp")&"  order by 1,2,3,4,5,6,7"   
        rs.open sql,con,1,2   
        do while not rs.eof   
        session("wk")=weekdayname(weekday((rs("date"))))   
        firstDayOfMonth = rs("date") + 1 - day(rs("date"))
        lastDayOfMonth = dateadd("m", 1, firstDayOfMonth ) - 1
    %>   
<tr>   

    <td align="center"><%=rs("emp_name")%></td>   
    <td align="center"><%=rs("food_had")%></td>   
    <td align="center"><%=rs("quantity")%></td>   
    <td align="center"><%=session("wk")%></td>   
    <td align="center"><%=day(rs("date"))&"/"&month(rs("date"))&"/"&year(rs("date"))%></td>   
    <td align="center"><%=rs("grand_total")%></td> 
    <td align="center"><%=fdate(firstDayOfMonth) ></td>   
    <td align="center"><%=fdate(lastDayOfMonth) ></td> 

</tr>   
<%   
        rs.movenext   
        loop   
        rs.close   
%>   

Edit: If you want to filter for the current month s data you can change up your sql statement to add a WHERE date >= firstDayOfMonth AND date <= lastDayOfMonth clause like the following:

 sql="select * from order_details where emp_name= " & session("emp") & "  " & _ 
     "WHERE date >= DATEADD(DAY, 0, DATEDIFF(DAY, 0, GETDATE())) + 1 - DAY(DATEADD(DAY, 0, DATEDIFF(DAY, 0, GETDATE()))) " &_
     "AND date <= DATEADD(MONTH, 1, DATEADD(DAY, 0, DATEDIFF(DAY, 0, GETDATE())) + 1 - DAY(DATEADD(DAY, 0, DATEDIFF(DAY, 0, GETDATE())))) - 1 " &_
     "order by 1,2,3,4,5,6,7" 

(我相信有些人会改写,不要说是绝对的东西)





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签