SQL Server provides date/time functions for extracting parts, calculating intervals, filtering ranges and creating default timestamps. These examples preserve the original dt practice table while updating the queries and ASP data handling.
SELECT DAY(issue_dt) AS issue_day, issue_dt
FROM dbo.dt
ORDER BY issue_dt;| issue_day | issue_dt |
|---|---|
| 28 | 2007-12-28 19:54:56 |
| 31 | 2010-10-31 19:23:47 |
| 31 | 2010-10-31 19:30:14 |
| 31 | 2010-10-31 19:30:20 |
| 28 | 2010-02-28 19:54:56 |
SELECT MONTH(issue_dt) AS issue_month, issue_dt
FROM dbo.dt
ORDER BY issue_dt;| issue_month | issue_dt |
|---|---|
| 12 | 2007-12-28 19:54:56 |
| 10 | 2010-10-31 19:23:47 |
| 10 | 2010-10-31 19:30:14 |
| 10 | 2010-10-31 19:30:20 |
| 2 | 2010-02-28 19:54:56 |
rs1.Open "SELECT MONTH(issue_dt) AS issue_month, COUNT(*) AS issue_count FROM dbo.dt GROUP BY MONTH(issue_dt) ORDER BY issue_month", conn| Month | Issue count |
|---|---|
| 2 | 1 |
| 10 | 3 |
| 12 | 1 |
If the table spans multiple years, group by both year and month when you need separate monthly totals for each year.
SELECT YEAR(issue_dt) AS issue_year, issue_dt
FROM dbo.dt
ORDER BY issue_dt;| issue_year | issue_dt |
|---|---|
| 2007 | 2007-12-28 19:54:56 |
| 2010 | 2010-10-31 19:23:47 |
| 2010 | 2010-10-31 19:30:14 |
| 2010 | 2010-10-31 19:30:20 |
| 2010 | 2010-02-28 19:54:56 |
SELECT MONTH(issue_dt) AS issue_month, COUNT(*) AS issue_count
FROM dbo.dt
WHERE issue_dt >= '20100101' AND issue_dt < '20110101'
GROUP BY MONTH(issue_dt)
ORDER BY issue_month;| Month | Issue count |
|---|---|
| 2 | 1 |
| 10 | 3 |
The range predicate keeps the date column unwrapped, which is generally more index-friendly than filtering with YEAR(issue_dt)=2010.
SELECT *
FROM dbo.dt
WHERE issue_dt >= CAST(GETDATE() AS date)
AND issue_dt < DATEADD(day, 1, CAST(GETDATE() AS date));Comparing a timestamp directly to GETDATE() normally fails to find “today's” rows because the times must match exactly. The half-open range covers the complete current date.
SELECT DATEDIFF(day, issue_dt, GETDATE()) AS days_since_issue, issue_dt
FROM dbo.dt;DATEDIFF() counts datepart boundaries crossed between two values; it is not a fractional elapsed-time calculator.
SELECT DATEDIFF(day, issue_dt, GETDATE()) AS days_since_issue, issue_dt
FROM dbo.dt
ORDER BY days_since_issue DESC;
SELECT *
FROM dbo.dt
WHERE issue_dt >= DATEADD(day, -15, GETDATE())
AND issue_dt <= GETDATE()
ORDER BY issue_dt DESC;A direct date-range predicate is clearer than applying DATEDIFF() to every row and also avoids accidentally including future dates.
DATEDIFF(datepart, startdate, enddate)Common dateparts include day, month, year, hour, minute and second.
rs1.Open "SELECT book_id, DATEDIFF(month, issue_dt, return_dt) AS month_diff, issue_dt, return_dt FROM dbo.dt ORDER BY book_id", conn
rs1.Open "SELECT book_id, DATEDIFF(year, issue_dt, return_dt) AS year_diff, issue_dt, return_dt FROM dbo.dt ORDER BY book_id", conn
<%
Dim conn, rs1
Set conn = Server.CreateObject("ADODB.Connection")
conn.Mode = adModeRead
conn.ConnectionString = aConnectionString
conn.Open
Set rs1 = Server.CreateObject("ADODB.Recordset")
rs1.Open "SELECT book_id, DATEDIFF(year, issue_dt, return_dt) AS year_diff, issue_dt, return_dt FROM dbo.dt ORDER BY book_id", conn
Response.Write "<table class='table table-striped'>"
Do While Not rs1.EOF
Response.Write "<tr><td>" & Server.HTMLEncode(CStr(rs1("book_id"))) & "</td><td>" & _
Server.HTMLEncode(CStr(rs1("issue_dt"))) & "</td><td>" & _
Server.HTMLEncode(CStr(rs1("return_dt"))) & "</td><td>" & _
Server.HTMLEncode(CStr(rs1("year_diff"))) & "</td></tr>"
rs1.MoveNext
Loop
Response.Write "</table>"
rs1.Close
Set rs1 = Nothing
conn.Close
Set conn = Nothing
%>
CREATE TABLE dbo.dt (
book_id INT IDENTITY(1,1) NOT NULL CONSTRAINT PK_dt PRIMARY KEY,
issue_dt DATETIME2(0) NOT NULL,
return_dt DATETIME2(0) NOT NULL
);Sample rows:
3,2007-08-23 00:00:00,2007-09-25 00:00:00
4,2007-12-15 00:00:00,2008-01-02 00:00:00
5,2007-07-05 19:59:56,2007-07-20 19:54:56
Validate the request value with IsDate(), convert it to a date value, then pass it as a parameter. Do not concatenate the visitor-entered date into SQL.
<%
Dim cmd, dtt, todo
todo = Request.Form("todo")
If todo = "update" Then
dtt = Request.Form("dtt")
If IsDate(dtt) Then
Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandType = adCmdText
cmd.CommandText = "UPDATE dbo.dt SET issue_dt=? WHERE book_id=?"
cmd.Parameters.Append cmd.CreateParameter("pDate", adDBTimeStamp, adParamInput, , CDate(dtt))
cmd.Parameters.Append cmd.CreateParameter("pBook", adInteger, adParamInput, , 5)
cmd.Execute
Set cmd = Nothing
Else
Response.Write "Please enter a valid date and time."
End If
End If
%>When redisplaying the current value in a form control, HTML-encode it before placing it in markup.
CREATE TABLE dbo.dt (
book_id INT IDENTITY(1,1) NOT NULL CONSTRAINT PK_dt PRIMARY KEY,
issue_dt DATETIME2(0) NOT NULL,
return_dt DATETIME2(0) NOT NULL
);Sample rows:
3,2007-08-23 00:00:00,2007-09-25 00:00:00
4,2007-12-15 00:00:00,2008-01-02 00:00:00
5,2007-07-05 19:59:56,2007-07-20 19:54:56Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.