SQL Date Search without time - asp.net

I have a query that searches by date. the dates in the database include the time. How do I search on just the date only.
select *
from weblogs.dbo.vwlogs
where Log_time between #BeginDate and #EndDAte
and (client_user=#UserName or #UserName Is null)
order by Log_time desc
cmd.Parameters.AddWithValue("#BeginDate", txtBeginDate.Text);
cmd.Parameters.AddWithValue("#EndDAte", txtEndDate.Text);

Leave your sql mostly as is and just fix your parameters:
cmd.Parameters.Add("#BeginDate", SqlDbType.DateTime).Value =
DateTime.Parse(txtBeginDate.Text).Date;
cmd.Parameters.Add("#EndDAte", SqlDbType.DateTime).Value =
// add one to make search inclusive
DateTime.Parse(txtEndDate.Text).Date.AddDays(1);
You also want to check to make sure your textboxes are valid datetimes first, but you should get the idea.
The only caveat here is that due to a quirk with the BETWEEN operator it will match the first instant of the next day. So, to fix that we write the query like this:
SELECT *
FROM vwlogs
WHERE Log_time >= #BeginDate AND Log_Time < #EndDate
AND (client_user=#UserName OR #UserName IS NULL)
ORDER BY Log_time DESC
Pay special attention to the comparision operators around the date.

The first thing to do is to remove the times from the dates. If you want to do this in the sql server code you can use something like the code below. I have this as a function on all the databases I work on
cast(floor(cast(#fromdate as float)) as datetime)
The next thing to worry about is the where criteria. You need to make sure you select everything from the start of the from date to the end of the to date. You also need to make sure queries for one day will work which you can do with a date add like this
Where LogTime >= #fromdate and LogTime < DateAdd(dd, 1, #todate)

In SQL round the start and end date to Whole Dates and use >= #BeginDate and very specifically < #EndDAte. The "rounding" process is not very elegant I'm afraid
e.g.
SELECT #BeginDate = DATEADD(Day, DATEDIFF(Day, 0, #BeginDate), 0),
#EndDAte = DATEADD(Day, DATEDIFF(Day, 0, #EndDAte) + 1, 0)
select *
from weblogs.dbo.vwlogs
where Log_time >= #BeginDate
and Log_time < #EndDAte
and (#UserName Is null OR client_user=#UserName)
order by Log_time desc
Note that I've moved "#UserName Is null" first, as there is some evidence that this test will easily pass/fail, and will cause the second more CPU intensive test (client_user=#UserName) to be ignored if the first test is TRUE (may be TommyRot of course ...)
Also, for best performance, you should explicitly name all the columns you need, and not use "SELECT *" (but that may just have been for the purpose of this question)

If you want to change the sql instead,
TRUNC(Log_Time) will reduce every datetime to to that date at midnight.
Make sure that you build your index on the column as TRUNC(Log_TIME) so it's usable.

Another gotcha - truncating your end date will NOT include that date! Consider:
WHERE Log_Time >= #BeginDate AND Log_Time < #EndDate
If #EndDate is truncated it will be midnight and not match anything on that day. You'll need to add a day!

Clean up the dates by adding the following line before your query...
select
#begindate=dateadd(day,datediff(day,0,#begindate),0),
#enddate=dateadd(ms,-3,dateadd(day,datediff(day,0,#enddate),1))
This will floor your begin date to the lowest possible time (00:00:00.000), and ceiling your end date to the highest possible (23:59:59.997). You can then keep your BETWEEN query exactly as it was written.
select *
from weblogs.dbo.vwlogs
where Log_time between #BeginDate and #EndDAte
and (client_user=#UserName or #UserName Is null)
order by Log_time desc
Hope this helps.

Related

I want to combine date and time and compare with utc date

When I write this query
SELECT Convert(datetime,Convert(varchar,CAST(GETUTCDATE() AS DATE))+' '+
CONVERT(varchar, cast(meas_pain.datetime AS time))) FROM meas_pain
it works for me but when I use the same part in WHERE clause it gives error 'Conversion failed when converting date and/or time from character string.'
SELECT schedules.id
FROM meas_pain LEFT JOIN schedules ON schedules.id=meas_pain.schd_id
WHERE meas_pain.schd_id=9150 AND
Convert(datetime,(Convert(varchar,CAST(GETUTCDATE() AS DATE))+' '+
CONVERT(varchar, cast(meas_pain.datetime AS time)))) <
CONVERT(datetime,DATEADD(Minute,0,getutcdate()))
can anybody explain??
I am not sure why this error does not appear in your select statement, since I can reproduce the error using just
SET DATEFORMAT DMY;
SELECT CONVERT(DATETIME, CONVERT(VARCHAR,CAST(GETUTCDATE() AS DATE)))
Example of Error
You are relying on localised conversion settings, you should use explicit conversion, e.g.
SET DATEFORMAT DMY;
SELECT CONVERT(DATETIME, CONVERT(VARCHAR, CAST(GETUTCDATE() AS DATE), 111), 111)
By explicitly defining the date format to convert both to varchar and from varchar (111) you can avoid any implied conversions.
However, If your dates/times are stored as such there should be no need for all the conversion to and from varchar, this is just more chance for things to go wrong, and unnecessary work, you can simply add a time to a datetime. e.g.
DECLARE #Date1 DATETIME = DATEADD(HOUR, 1, GETUTCDATE()),
#Date2 DATETIME = DATEADD(DAY, 1, GETUTCDATE());
SELECT [Date1] = #Date1,
[Date2] = #Date2,
[Date1/Time2] = CAST(CAST(#Date1 AS DATE) AS DATETIME) +
CAST(#Date2 AS TIME);
From what I can gather from your query you are just trying to get results where the time of meas_pain.datetime is less that the current UTC time, regardless of date. So you should be able to simplify your query to just:
SELECT schedules.id
FROM meas_pain
LEFT JOIN schedules
ON schedules.id = meas_pain.schd_id
WHERE meas_pain.schd_id = 9150
AND CAST(meas_pain.[DateTime] AS TIME) < CAST(GETUTCDATE() AS TIME);
And remove further redundant conversions.
Simplified example on SQL Fiddle
ADENDUM
Apparently this time comparison is not what you are after (although it is what the query you have posted is doing), so I am assuming GETUTCDATE() is just for demonstration.
The conversion you are trying to perform is equivalent to this:
CAST(CAST(GETUTCDATE() AS DATE) AS DATETIME) + CAST(meas_pain.[DateTime] AS TIME)
Another example on SQL Fiddle using the above conversion

SQLite, how to use expression in datetime modifier

In short: I need to calculate the ending datetime given starting time and length in minutes.
I have a table with columns StartTime (type datetime) and LengthMinutes (type int). To calculate the ending time I would need some sql like this:
select datetime(StartTime, '+LengthMinutes minutes') from my_table;
How do I refer to the column LengthMinutes from within the modifier?
Edited: solved using dan04's suggestion. Thanks!
SELECT datetime(StartTime, '+' || LengthMinutes || ' minutes') FROM my_table;
select datetime(strftime('%s',StartTime)+lengthMinutes*60,'unixepoch') from my_table;

ASP.Net SQL Where Date is after Today

Hi all I have the following Query made using the Query Builder in Visual Studio.
SELECT Schd_ID, Schd_Date, Schd_Avaliable, Schd_Nights, Schd_Price, Accom_ID
FROM Schedule
WHERE (Schd_Avaliable = 'Yes') AND (Accom_ID = Accom_ID)
I want to add another WHERE statement which adds where Schd_Date is after todays date, any ideas?
Assuming SQL Server, the GETDATE() function returns the date and time the statement was run at:
WHERE Schd_Date > GETDATE()
Use the following for finding dates greater than the current date at midnight:
WHERE Schd_Date > DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
However, CURRENT_TIMESTAMP is the ANSI means of getting the current date and time in a database. Beyond that, date functionality is not consistent between databases so you'd have to tell us what you are dealing with for better answers.
If this is SQL Server you could use the GETDATE() function to return the current date and compare against it:
SELECT
Schd_ID, Schd_Date, Schd_Avaliable, Schd_Nights, Schd_Price, Accom_ID
FROM
Schedule
WHERE
(Schd_Avaliable = 'Yes')
AND
(Accom_ID = Accom_ID)
AND
(Schd_Date > GETDATE())
SELECT Schd_ID, Schd_Date, Schd_Avaliable, Schd_Nights, Schd_Price, Accom_ID
FROM Schedule
WHERE (Schd_Avaliable = 'Yes') AND (Accom_ID = Accom_ID) AND (GETDATE() < Schd_Date)
This should work
I guess this expression can be used as a date representing the current date on the format yyyy-mm-dd (without the hours,minutes and seconds).
(Datepart('yyyy',getdate())+ '-' +Datepart('m',getdate())+ '-' + Datepart('d',getdate()))
therefore
SELECT Schd_ID, Schd_Date, Schd_Avaliable, Schd_Nights, Schd_Price, Accom_ID
FROM Schedule
WHERE (Schd_Avaliable = 'Yes') AND (Accom_ID = Accom_ID) AND
(Schd_Date >= (Datepart('yyyy',getdate())+ '-' +
Datepart('m',getdate())+ '-' +
Datepart('d',getdate())))

Query to check Date in Sql

My query is simple yet bit twisted for me. Actually I am working on registration module of an app, where the registration expires 31st March every year. The registration is valid from April 1 to March 31. So whenever a user is registered in between the date, I want his status to be expired if march 31 is crossed.
Let me make more clear to you.
Say I have registered my self in 15Nov2010, then on 31st March 2011, my subscription will get expired. I want to check it automatically as the years will go on. I need a query that will automatically query the created date with expiration date. I am already having a select query and i need to embed this condition and i want to check the creation date with current system date. If Current system date is not 31 march midnight 12, the status must be active else expired.
This can be used for MS SQL to determine whether it has expired or not.
create table #t
(
CreateDate datetime
)
Insert Into #t
select GETDATE() union all
Select DateAdd(month,4, getdate())
Select Case When CreateDate < getdate() And
Getdate() < Cast(str(DatePart(year, getdate())) + '-03-31' as datetime) Then
'Active' Else 'Expired' end as [Status],
CreateDate
From #t
drop table #t
To filter your query you would simply move the case statement to a where clause
e.g.
Where Case When CreateDate < getdate() And
Getdate() < Cast(str(DatePart(year, getdate())) + '-03-31' as datetime) Then
'Active' Else 'Expired' end = 'Expired'
What you need is to schedule a job (i think they're called events on MySql) to run every year on March 31 11:59 and update set the status of all your accounts to expired. (remember to make dstinction on admin accounts) :)
Take a look at this.
for MySQL
http://dev.mysql.com/tech-resources/articles/mysql-events.html#1
for SqlServer
http://msdn.microsoft.com/en-us/library/ms191439.aspx
http://msdn.microsoft.com/en-us/library/ms190268.aspx
I am considering a table YourTable and it has a column Date of type datetime
You can use this query -
select [Date], dbo.GetStatus([Date]) as 'status' from YourTable
And, the function GetStatus -
CREATE FUNCTION [dbo].[GetStatus](#Date datetime)
RETURNS varchar(10)
AS
BEGIN
DECLARE #Return varchar(10)
DECLARE #Year int
SELECT #Year = DATEPART(YYYY,GETDATE())
IF GETDATE() >= CONVERT(datetime,'01-APR-' + CONVERT(varchar,#Year))
SET #Year = #Year + 1
IF #Date BETWEEN CONVERT(datetime,'01-APR-' + CONVERT(varchar,#Year-1)) AND CONVERT(datetime,'31-MAR-' + CONVERT(varchar,#Year))
set #return = 'active'
ELSE
set #return = 'inactive'
Return #return
END;
I'd probably have a "last renewed" column (It would initially store the creation date) then write:
SELECT CASE WHEN
YEAR(DATEADD(mm, -3, LastRenewed)) < (YEAR(GETDATE()) - 1)
THEN 'Active'
ELSE 'Expired' END
AS Status
FROM TableName
I don't see what the problem with #Barry's answer is though, to be honest. If you need to use this logic in several places you can avoid repeating yourself using a view eg:
CREATE VIEW ActiveOrNot AS
SELECT Account, CASE WHEN
YEAR(DATEADD(mm, -3, LastRenewed)) < (YEAR(GETDATE()) - 1)
THEN 'Active'
ELSE 'Expired' END
AS Status
FROM TableName
You could then select only active accounts using:
SELECT Account
FROM ActiveOrNot
WHERE Status = 'Active'

Search by parameter only if user enters a value

I want to rewrite this query so that if #UserName gets passed in a null value then it leaves the Client_User out of the search criteria. I only want it to search by name if the user enters a name in the username textbox on the webform. I'm not sure how to do this.
select * from weblogs.dbo.vwlogs
where Log_time between #BeginDate and #EndDAte
and client_user=#UserName
select * from weblogs.dbo.vwlogs where Log_time between #BeginDate and #EndDAte
and
(#UserName IS NULL OR client_user=#UserName)
select *
from weblogs.dbo.vwlogs
where Log_time between #BeginDate and #EndDAte
and (client_user=#UserName or #UserName IS null)
The solution from Kristen will work, but if you need performance, don't do it because the plan will be cached only for the first condition.
So, if your procedure gets called with the NULL parameter first, that query will be cached.
If you need higher performance, use an IF statement and create two distinct queries.
In more complicated queries event sp_execsql will be faster.
The best solution is to utilize sp_execute_sql. For example:
--BEGIN SQL
declare #sql nvarchar(4000)
set #sql =
'select * from weblogs.dbo.vwlogs
where Log_time between #BeginDate and #EndDate'
+ case when #UserName is null then '' else 'and client_user = #UserName' end
sp_execute_sql
#sql
, #params = '#UserName varchar(50)'
, #UserName = #UserName
--END SQL
As muerte mentioned, this will have a performance benefit. According to BOL:
sp_executesql can be used instead of stored procedures to execute a Transact-SQL statement a number of times when the change in parameter values to the statement is the only variation. Because the Transact-SQL statement itself remains constant and only the parameter values change, the Microsoft® SQL Server™ query optimizer is likely to reuse the execution plan it generates for the first execution.

Resources