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.
Related
I am working on a website in asp.net. I am getting a date from a web page and then depending on the user input I want to get results from SQL Server database (using stored procedures).
Problem is that I am getting date only from UI in this format 2016-10-08 which is of type string. But in the database, I have a column which is of type datetime in this format 2016-10-08 17:38:00.000.
I am using this query to search but it does not work.
select *
from table
where acceptedDate like #sDate+ '%';
where sDate is input parameter for stored procedure. Please help. thanks
Don't pass dates as strings. Pass them as DateTime.
The .Net DateTime maps directly to SQL Server's DateTime. All you have to do is parse the string to a DateTime struct in your .Net code and pass it as a parameter to your stored procedure.
To search for a specific date and ignore the Time portion of the DateTime, better use >= and < in your sql:
select *
from table
where acceptedDate >= #Date
AND acceptedDate < DATEADD(DAY, 1, #Date);
If you only want compare with day level and ignoring the hours part, you can use DateDiff function.
Pass d or DAY to interval parameter of DateDiff
For example:
DECLARE #sDate VARCHAR(100)='2016-10-08'
IF ISDATE(#sDate)=1
BEGIN
select *
from table
where datediff(d,acceptedDate,#sDate)=0 --same day
END
ELSE
PRINT 'Invalid date format!'
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 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 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))
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