I am writing a query to select data from a table called contest.
each contest has start date and enddate. I want to select all the contest details from the contest table if the current date is between start date and end date.
select * from contest
where getDate() between startdate and enddate
getDate() returns you the current date and time.
select * from contest
where dtBegin <= getDate() and getDate() <= dtEnd
out of interest why is this tagged c# what are you trying to achieve with this?
if your trying to get this into an app some how you could use either a SqlDataReader or LinqtoSQL (plus others)
Related
i have an application in which i am having start_date and due_date . and i am filtering my table on due_date like who are all having due date in couple of days need to show their record eg. today is 25-07-2014 if due_date of particular person is on 27-07-2014 the data should get filtered and m displaying it in gridview . i am using the following query to achive it.
DECLARE #DatePartOnly DATETIME
SEt #DatePartOnly = DATEDIFF(DD, 0, GETDATE())
select *
from users
where DueDate=DATEADD(DAY,2,#DatePartOnly) and TableStatus='Active'
but what the problem is the list wont be shown for next day i.e my query is filtering the list based on current date so next day i wont be seeing the records and i need to display these records in gridview until i update that particular row by clicking select command.
my problem is untill i update my filterd list in gridview i need to display it.
please help me to achive this .
Thank you.
declare #FromDate datetime
declare #ToDate datetime
set #FromDate = DATEADD("d",0,GETDATE())
set #ToDate = DATEADD("d",2,GETDATE())
select * from users
where duedate between #FromDate and #ToDate and TableStatus='Active'
i used the above query to acheive my task.
thank you
I want to get the latest record from my database
Below is my query
select * from tablename order by date desc, time desc
My Issues
My query show uncertain behavior, sometimes it works properly in website but sometimes is does not give the desired result. It happen because of time part
Example
2:00 pm and 2:00 AM how to compare this time?
The exact Query is for getting very latest row
select Top 1 * from tablename order by date desc, time desc
so if you´re datatype is really VARCHAR you can convert it to date and time in the order by clause
begin
declare #time1 time
declare #time2 time
declare #test table(
mytime varchar(max),
mydate varchar(max)
)
insert into #test VALUES('2:00 pm', '10-10-2010')
insert into #test VALUES('2:00 am', '08-05-2009')
SELECT * FROM #test ORDER BY CONVERT(date, mydate) DESC, CONVERT(time, mytime) DESC
end
but i would suggest to change your table-structure, to have only one DateTime Column!
Or at least change the DataType to TIME and DATE!
Unless you have some special requirement, it will be better to use only one column of datetime type to store date and time.
If two columns are must, you can store time in 24-hour format.
Try this. Convert the date and time into a datetime and then order by that.
select *
from tablename
order by cast(date AS DATETIME) + cast(time AS DATETIME)
Select Top 1 ColumnName from dbo.TableName Order by date,time desc
I think u should convert the time in AM/PM to 24-hrs format. Check this out http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
I am trying to check in a stored procedure a date in table if it is equal to Today's date.
Code is
DECLARE #p0 datetime
Set #p0 =GETDATE()
Select * from testtable
where dateCol=#p0
This doesn't work it just gives empty rows. How can I accomplish that? Thanks
If dateCol is just the date, not DATETIME, you can use:
SELECT *
FROM Table
WHERE dateCol = CAST(GETDATE() AS DATE)
You need:
SET #p0 = CONVERT(CHAR(10),GETDATE(),103)
GETDATE() returns the date and time. You probably have records with todays date but not at this exact time.
Also you don't really need to store it in #p0. You could use the expression directly in the WHERE clause.
I am trying to pass a SQL query through c# , currently it is based on just the day month and year with time always set to 0 EG 1997-10-28 00.00.00.000 which is great as the time never changes it easy for me to just Select Where date equals the calendar date.
However with the Start Field , the time is different on each record , eg 1899-12-30 14.14:00.00.000 , 1899-12-30 15.14:30.00.000 . (Seconds downwards are always the same) .
So I need to have a query that will return all the results of the selected date on the "Start" field . How would I do this?.
E.G IF i click the calendar which passes 1997-10-28 00.00.00.000 , I would like the results of every time in that day!. How do I go about that?.
Thanks for any input.
EDIT: Is there a way to format the date that i have in SQL ?. This comes from an old access database!. and as you can see above it is 1899-12-30 ?. not 1998 , I don't know why this has happened!.
WHERE DATEDIFF(dd, your_start_field, #your_param) = 0
You need to select all record between today and tomorrow without including tomorrow's date.
WHERE EventDate >= StartDate AND EventDate < DATEADD(d, 1, StartDate)
Can you not just query the date part. I think this should work...
SELECT * FROM Table1 WHERE StartDate = '1997-10-28'
EDIT: Above may not work but following should cover needs
SELECT * FROM Table1 WHERE StartDate >= '1997-10-28 00:00:00.000' AND StartDate < '1997-10-29 00:00:00.000'
(notice the second date is the following date)
This SQL DATEADD(d,datediff(d,0,startdate),startdate) will convert a date with time to just the date;
eg
Select field1,field2 from mytable where DATEADD(d,datediff(d,0,startdate),startdate)='1997-10-28'
I am a little confused about how or what the best way to determine what the closest date is to DateTime.Now is.
In my table, everything needs to be timestamped. And on a page, I need to be able to retrieve everything from the table only if the date is the closest date to now.
How would I go about this?
I am using DateTime.Now when inserting dates into the Database, and the format is like:
5/07/2011 5:28:57 PM
Here's my suggestion:
declare #DateTimeNow datetime = getdate()
select TOP (1)
RecordId
,MyDateColumn
,abs(datediff(s, MyDateColumn, #DateTimeNow)) as Diff
from
MyTable
order by
abs(datediff(s, MyDateColumn, #DateTimeNow)) asc
Do not forget to use ABS()!
How about
SELECT TOP 1 *
FROM MyTable
ORDER BY TimestampColumn DESC
Consider storing time in UTC - DateTime.UtcNow
In T-SQL you could use DateDiff:
DATEDIFF ( datepart , startdate , enddate )
http://msdn.microsoft.com/en-us/library/ms189794.aspx
or in C# you could use TimeSpan:
http://msdn.microsoft.com/en-us/library/system.timespan.aspx#Y3719
Do you only have past dates, meaning, will you ever have a date that is newer than DateTime.Now? If not, you could get by with a simple Order By on the date column selecting the newest date. Otherwise, you'll need to get a date difference between your DateTime.Now, and order by that result. e.g.
SELECT TOP 1
columnDate
FROM table1
ORDER BY DATEDIFF (ss,#passedInDate,columnDate)
This would essentially look for all future and past dates using your #passedInDate (DateTime.Now) as the qualifier or base date. I'm using seconds as the time interval to compare in my example, but you can change that to whatever makes the most sense for you.
Also, you shouldn't need to pass in DateTime.Now to SQL server, as you can use the built in GetDate() function.
Something like this should work:
SELECT TOP 1 * FROM MyTable ORDER BY ABS(DATEDIFF(DD, getdate(), DATE))
This should sort your rows by the closest date, past or future. If you need it more precise then just days, change DD to something else, as specified here