How can I user GETDATE() to retrieve rows - asp.net

I am a new developer in asp.net, I want to create query that retrieve row based on comparing between DateTime (column in my table) with the current date, I used a lot of things like:
Select * from employee where DateTime = dateadd(dd,0, datediff(dd,0, getDate()))
Select * from employee where DateTime=Convert(date, getdate())
Select * from employee where DateTime =getDate()
The first one, worked correctly but suddenly did not work!
what do you think is the problem?

Try your query like this
Select * from employee where convert(date,yourfield) = convert(date,dateadd(dd,0, datediff(dd,0, getDate())))

You can use CASTto trim time part from the date for the sake of searching.
SELECT * FROM Employee WHERE CAST(DateTime AS DATE) = CAST (GETDATE() AS DATE)

Related

string_agg function with IN operator not working in PostgreSQL Query

here is my query
select *
from table
where id in ( select string_agg(CAST(id as varchar), '","') FROM table)
string_agg() is completely useless and unnecessary for that:
select *
from table_one
where id in (select id FROM other_table)
I assume you are doing that for two different tables, otherwise that would be a very expensive way of writing: select * from table where id is not null

SQL query for finding all the records that exist on a particular date within some date range

I have a date= 2016-12-25 at hand, and i want to find all the records of this particular date. The records in the database are stored as a range of startDate and endDate. Example
|_____Name___|___Class_____|___StartDate_____|___EndDate______|
|_____Maths___|____Lecture___|___2016-12-10____|___2016-12-30____|
so now using the sql query i want to display this class with the user selected date 2016-12-25 where I am unaware os StartDate and EndDate (All i have in hand in the user specified date, the StartDate and EndDate could differ for each subjects). Can anybody please help me out?
will it be something like this?
query: select all records where 2016-12-25 between all StartDate and endDate
This works
CREATE TABLE "classes" (
`Name` TEXT,
`Class` TEXT,
`StartDate` TEXT,
`EndDate` TEXT
);
Insert into Classes(`Name`,`Class`,`StartDate`,`EndDate`)
values ("Maths","Lecture","2016-12-10","2016-12-30");
select *
from Classes
where "2016-12-20" between StartDate and EndDate;
Do not forget to check the input strings are representing valid dates in the choosen format ('YYYY-mm-DD')
Try below query
SELECT *
FROM `TaleName`
WHERE
StartDate >='2016-12-25'
AND EndDate <='2016-12-25 '

Return records from a specific date

In my table I have a column date which is DATETIME type where I save my data like 2015-10-22. How can I select those rows which their date is older than a specific date, eg 2015-10-22. I tried
SELECT * FROM MYTABLE where date > '2015-10-09';
but it doesn't return anything.. my DB is built with sqlite.
Try this:
SELECT * FROM MYTABLE where date > date('2015-10-09');

How to select latest Row from table without using ORDER BY

How can I select latest row from by table without sorting it?
It is because it follow by the ID AUTO INCREMENT...
I'm using c# asp.net to select... I did try using LIMIT 5 but it give me an error page..
rSQL = "select COUNT(*) from chatLog_db where sessionid='" + grpID + "' LIMIT 5";
Is there any better way to solve this matter?
I'd appreciate any help please.
You have an id column which is autoincremented, right? Then you can do it like this..
select * from tablename where id=(select MAX(rid) from tablename)
On MSSQL simply use the top 1 instead of limit
select top(1) * from mytable order by some_column
http://msdn.microsoft.com/en-us/library/ms189463.aspx
if the latest means the max id
select * from chatLog_db
where id = (select max(id) from chatLog_db);
EDIT
select 5 records
select * from chatLog_db
where id > (select max(id) - 5 from chatLog_db);
You can try
SELECT * FROM chatLog_db WHERE sessionid > (SELECT MAX(sessionid) - 1 FROM chatLog_db);
You may also try for
SELECT * FROM chatLog_db WHERE sessionid > (SELECT MAX(sessionid) - 5 FROM chatLog_db);
You may use max as well like
select * from chatLog_db where sessionid = (select max(sessionid) from chatLog_db);
Something like that.
If you are not using order by into your query because you are thinking that it will change the order of your dsplay data then i will tell you that there is one trick as well to sort your data as per your need
you can also sort your data as per your need even if you are using
order by into your query,put the result into DataView and sort it
according to your need because DataView allow us sorting facility as
well.
Latest by using Order By like
select * from tablename order by columnname desc LIMIT 5;
Hope it works for you.

SQL Date Search without time

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.

Resources