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.
Related
I want to get data betwenn 2 dates and these dates are supplied by the user.
what i did is as follows
CREATE PROCEDURE getattndncchart
(
#date1 varchar(50),
#date2 varchar(50)
)
AS
SELECT * FROM Tbl_Date WHERE Date BETWEEN #date1 AND #date2
but am not getting the result. pls help me out.
is your Date should be different type instead of varchar(50), ?
I am never used procedures, so maybe I am wrong. Maybe something like this?
CREATE PROCEDURE getattndncchart
#date1 date,
#date2 date
AS
BEGIN
SELECT * FROM Tbl_Date WHERE [Date] BETWEEN #date1 AND #date2
END;
Also, Date in your WHERE clause needs to be surrounded by [ ] (since it is a reserved word).
It looks like you have several errors here:
1) As Andrew points out, you can't compare dates that are not the correct type. So you should parse the values as a date or datetime into the stored procedure from your application.
2) In order to do a comparison of dates, you must have another date type to compare it to. For example:
CREATE PROCEDURE getattndncchart
(
#date1 date,
#date2 date
)
AS
DECLARE #curdate date = CURRENT_TIMESTAMP
SELECT *
FROM Tbl_Date
WHERE #curdate BETWEEN #date1 AND #date2
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 have a DateTime column on a table in SQL Server 2008 R2 database. My c# front end is inserting a row in this table with a datetime parameter.
DateTime T = DateTime.Now;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "cargridsnapshot";
cmd.Parameters.AddWithValue("#t", T);
cmd.ExecuteNonQuery();
The datetime column in the row in the table is this:
2013-09-04 16:21:23.450
but
select * from table
where TIMECOLUMN = '2013-09-04 16:21:23.450'
returns no results.
SQL Server can represent datetime in a variety of ways when comparing a string to your database value. Some have the month before the day, some day before month. The localization of your SQL Server is causing your issue; you can read more about it here. Specifically, look for SET DATEFORMAT.
SQL Server only stores time to approximately 1/300th of a second-you are probably using a value that cannot accurately be represented with the available bits.
Edit
You can check this behavior with the following code:
select CONVERT(DATETIME, '2013-09-04 16:21:23.450') -- returns 2013-09-04 16:21:23.450
select CONVERT(DATETIME, '2013-09-04 16:21:23.451') -- returns 2013-09-04 16:21:23.450
select CONVERT(DATETIME, '2013-09-04 16:21:23.452') -- returns 2013-09-04 16:21:23.453
select CONVERT(DATETIME, '2013-09-04 16:21:23.453') -- returns 2013-09-04 16:21:23.453
select CONVERT(DATETIME, '2013-09-04 16:21:23.454') -- returns 2013-09-04 16:21:23.453
select CONVERT(DATETIME, '2013-09-04 16:21:23.455') -- returns 2013-09-04 16:21:23.457
Bottom line
Don't try to identify rows with a datetime...
What if you use this
Select * from table where convert(date,yourcolumn)=convert(date,your_datetime)
but kindly note it will compare on date only
The SQL Server datetime type is accurate only only to increments of .000, .003, or .007 seconds.
So when you save a value with .450, that will be fine. But if you saved with .451, that would get rounded back down to .450, and then it wouldn't match up.
You can avoid this by using a datetime2 column type.
Also - you probably shouldn't be storing DateTime.Now in your database. Read here.
I am using SQL Server CE database and C# language. I have a table with a datetime type column. I want to use a SELECT statement like
SELECT * FROM Data WHERE Date = #Date
But I need only date part of the datetime value. So I can add parameter like
SQLCmd.Parameters.Add("#Date", date.Date);
My problem is with SELECT statement I think.
Instead of that you can use DateDIFF
SELECT * FROM Data WHERE DATEDIFF(day, Date, #Date)=0
SELECT * FROM Data WHERE DAY(Date) = DAY(#Date)
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