I am working on a project in which i am using Jquery Datepicker to select to and from date to filter records,
I have DateTime field in my Database table and on selecting date from datepicker i am doing ajax call,
the problem is arising is that in my database i have records with time(eg: 2009-08-08 13:12:23.143)
and from datepicker i am having record without time(ie 2009-08-08), I have converted it to DateTime but still i am having same record, Because of this difference my query returns null result, Can any one suggest how to handle this?
Regards
Use only the date part of DateTime object (if you do not want to consider time) for filter... in your where clause of query, do like this
where (#pDate is null or CONVERT(VARCHAR(10), Date,111) between CONVERT(VARCHAR(10), #pStartDate,111) and CONVERT(VARCHAR(10), #pEndDate,111))
Related
I am trying to update a datetime column in a SQL Server 2012 table by using
newItem.DateSaved = DateTime.Today;
I want to save only the Date part of DateTime, but when I am checking in the table I can see that it saves also the time part: 2018-07-27 00:00:00.000 .
How I can make it to store only date part? 2018-07-27 ?
Edit
Because my column is datetime, I see that I can't store only the date part. But how I can show to the user only the date part without time? Here is the select part of the Linq-to-SQL query:
select new { Date = smv.DateSaved.Value.Date }).Distinct();
A datetime column always has a time part - just, in your case it will all be zeros. A datatime is just a number under the hood; it doesn't have any choice about what it has / doesn't have; similarly, it doesn't have any notion of formatting until your code tries to display it, and then it is the dispalying code that chooses the format.
If you don't want a time part, use date instead of datetime for your column.
Change column type in SQL Server from datetime to date (https://learn.microsoft.com/en-us/sql/t-sql/data-types/date-transact-sql?view=sql-server-2017)
If you want to store only the date without time in SQL Server database you can set the column type to date
ALTER TABLE table_name
ALTER COLUMN DateSaved date;
Reference: https://learn.microsoft.com/en-us/sql/t-sql/data-types/date-transact-sql?view=sql-server-2017
If you have a DateTime property in your model and you want to show only the date part just format it in your control in the View. For example:
$"{newItem.DateSaved:yyyy-MM-dd}"
or
newItem.DateSaved.ToString("yyyy-MM-dd")
You can also use "d" or "D" (short and long version) instead of specific format like "yyyy-MM-dd" if you want to have default formatting.
If your view is written in HTML or XAML do it there using the same formatting technique.
It is because your field on the database is DateTime. If you want to store just date, you should have a field with date data type. In this link, you can see the difference between the date data types.
I have a table called Jobs which has several columns, One of the column being ProofDate Column. Now the Column shows the date and as well as time.. I want to display the entire table in a tab of windows form application along with the ProofDate..But The proof Date must show only the Date and not the time..Can anyone help me with it??
Select CONVERT(DATE, ColumnName) From Table
Would give you just the date of a column which is datetime
Applications and databases work best when you leave DateTime as complete DateTime objects, and the best time to change that format is when it is time to display the information
Leave the SQL query alone, use one of the DateTime.ToString methods to just the Date portion. There are plenty of canned methods as well; such as ToShortDateString().
if your ProofDate column is datetime as datatype then you can use below query.
select col1, col2, col3, convert(date,ProofDate ) as colname from table
I am trying to get date from a column which has its datatype set as date and in SQL Server the column also contains only date but when i run query from my application C#:
resultData[i][3] = sqlRdr["addedOn"].ToString();
It gives me a default time signature attached to it
select addedOn from sitrep;
I also tried using below cast but still same results:
select Convert(Date, Convert(datetime, addedOn)) as addedOn from sitrep;
If your saving it to a string i would try.
Convert.ToDateTime(sqlRdr["addedOn"]).ToShortDateString();
You can modify your query as:
resultData[i][3] = sqlRdr["addedOn"].ToString("M/d/yyyy");
I have a website project which selects dates from database. the database stores the date in this format "2013-01-02 00:00:00.000". but when I select the date, I want to use the format "11/06/2013" - "dd/mm/yyyy"
here is my select
-- I am trying to pass "dd/mm/yyyy"
select date from Currencies where date = '11/06/2013'
but this doesnt work. if I change my where clause as below, it works...
-- if I pass "mm/dd/yyyy" , it works
select date from Currencies where date = '06/11/2013'
but I must pass "dd/mm/yyyy", how can I do that?
Try this one
SELECT date FROM Currencies WHERE date = convert(datetime, '11/06/2013 00:00:00', 103)
You should avoid handling variable syntax inside your sql statements and use parameterized queries instead
check this out.
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