SELECT * FROM MyTable WHERE [Date] = ([Date] + 8 hours) - sql

My problem starts with the fact that my server is in a totally different country, so we have a difference of 8 hours (GMT).
So when I'm trying to do something like this:
CreateNewDataSet("SELECT * FROM tblArchives WHERE [Date] =" & DateAndTime.Today.Date.ToString("MM/dd/yyyy") & "'; & , "tblArchives")
It won't get all the results because it happened today in my country but yesterday where the server is based.
I tried unsuccessfully to use the DATEADD function.
Is there any way to do something like:
SELECT FROM WHERE [DATE+8 hours] =DateAndTime.Today.Date.ToString("MM/dd/yyyy")

Why don't you subtract the hours from the other side of the condition statement:
CreateNewDataSet("SELECT * FROM tblArchives WHERE [Date] =" & DateAdd(DateInterval.Hour, -8, DateAndTime.Today.Date).ToString("MM/dd/yyyy") & "'; & , "tblArchives")

You could move the date calculation in to the SQL so that you get the servers date something like
SELECT * FROM tblArchives WHERE Date = DateAdd(HH, +8,getdate())
try to run DateAdd(HH, +8,getdate()) to see the actual date and time it uses in your query. I have similar needs but I always set the time to be 1 millisecond after midnight to one millisecond before midnight. This will make sure you get all the data for a particular day.

you have to use DateTime.AddHours method
"SELECT * FROM tblArchives WHERE [Date] =" &
DateAndTime.Today.AddHours(-8).ToString("MM/dd/yyyy")
Instead of adding +8 hours to [Date] Column, add -8 to the current server date

Related

How to extract records from SQL table falling within certain date range? [duplicate]

I have a SQL statement to display data between two dates. I almost got it but there's a problem.
If I input March 1,2012 to March 7, 2012.. it should show data with dates between the two.. but it also show all of the dates under March 2012.. but whenever I input March 10, 2012 to March 30, 2012 the SQL works perfectly.. any help will be appreciated. thanks
SELECT
agentname, noofcalls, qualified, booking, resched,
actualbooking, sales, remarks,
concat(month,' ',day,',',year) as 'date'
FROM
tblagents
WHERE
(month between '" & cbosmonth.Text & "' AND '" & cboemonth.Text & "')
AND (day between '" & cbosday.Text & "' AND '" & cboeday.Text & "')
AND (year between '" & cbosyear.Text & "' AND '" & cboeyear.Text & "')"
you are doing string comparisons in each of your 'between'. Every number starting with a 1, a 2 or a 3, regardless of what follows it, i.e. 21, or 26, or 31, they are all lower than 7 if you look at them as strings. 1 to 30 works because you're only leaving 31 behind, and 30 < 31 as a String as well.
Do the concatenation first and then the between:
WHERE concat(month,' ',day,',',year)
BETWEEN concat(cbosmonth.Text,' ', cbosday.Text,' ',cbosyear.Text)
AND concat(cboemonth.Text,' ', cboeday.Text,' ',cboeyear.Text)
(check out for correct syntax, I'm just copy pasting from your question, not tried it)
BTW, unless you have a reason to, you probably should be storing the entire date in a single column with the right data time (datetime, timestamp, ...) and not three separated columns.
That is the incorrect way to store a date in database.
Change the date column to datatype date.
You could then do:
SELECT *
FROM table
WHERE bookingDate between to_date ('2012/03/01', 'yyyy/mm/dd')
AND to_date ('2012/03/07', 'yyyy/mm/dd');
This approach is wrong.
In order for a date to be between two interval dates it does not have to have a day number between the two dates, e.g. (pseudocode)
date = May-25-2012; startDate = March-15-2012, endDate = June-01-2012
date is clearly between startDate and endDate, yet
day(date) is 25, which is not between day(startDate) = 15 and day(endDate) = 1
more, as 15 is larger than 1, there are no numbers between them, so that condition will always be false
Similar example can be made for the month part of the date (e.g. date = May-25-2012; startDate = September-15-2010, endDate = Match-01-2015)
You need to take the values for day, month, year, and construct a Date either in the application or on the server and use that to compare the values against.
To make a date from text fields in VB
Dim startDate = new DateTime(
Convert.ToInt32(cbosyear.Text),
Convert.ToInt32(cbosmonth.Text),
Convert.ToInt32(cbosday.Text))
Note that this will fail if the user enters, e.g. "some text" for the year value. You'll need to add some data validations to achieve that.
To make a datetime from parts in SQL Server, take a look here, there are quite a few techniques explained.
Also, you should always avoid just pasting values into the sql string, that's asking for sql injection problems. You should do something like this:
Dim command = new SqlCommand()
command.CommandText = "SELECT .... FROM tblagents where DATEFROMPARTS(year, month, day) between #startDate AND #endDate"
command.Parameters.AddWithValue("#startDate", startDate)
command.Parameters.AddWithValue("#endDate", endDate)

How to convert Access statement to SQL to determine date range

I have an Access statement that I am trying to convert to be used in SQL Server 2012. Here is the statement:
>=DateAdd("m",-1,DateAdd("m",-12,Month(Now()) & "/" & [FiscalYear])) And <DateAdd("m",-1,DateAdd("m",-12,Month(Now()) & "/" & [FiscalYear]))+364
It is basically getting a rolling 12 month period. How can I use this in SQL or is there a better way to write this altogether?
Here's how you would get the past 12 months worth of data in SQL:
SELECT *
FROM Table
WHERE DateField > DATEADD(MONTH, -12, GETDATE())

VB.net, how to select date between 2 date

How to select date between 2 date using datetimepicker.
I am using
SELECT *
FROM data BETWEEN '"& datetimepicker1.value &"' AND '"& datetimepicker2.value &"'
It is very bad practice to concatenate parameters in SQL query. Use special parameter syntax.
Back to you question:
Query should look like:
SELECT * from data WHERE someField BETWEEN #Low AND #High
And code:
command.Parameters.Add("#Low", SqlDbType.Int); //set correct type
command.Parameters["#Low"].Value = low;
command.Parameters.Add("#High", SqlDbType.Int);//set correct type
command.Parameters["#High"].Value = high;
Complementing the above answers:
1) You must have the date in the same format of SQL, including seconds (or not). The string must be the same. You may also try 00:00:00 to starttime and 23:59:59 to end-time.
2) You must utilize a conversion from string to date, since your fields are DATE in SQL.
Try:
SELECT * from data WHERE someField BETWEEN Convert(datetime, "01/01/1980 00:00:00", 120) AND Convert(datetime, "03/10/2015 23:59:59", 120)
Look the '120' above: it´s the SQL format date/time. Check the available formats to convert date and time in your SQL version.

BETWEEN clause in SQL

I have a SQL statement to display data between two dates. I almost got it but there's a problem.
If I input March 1,2012 to March 7, 2012.. it should show data with dates between the two.. but it also show all of the dates under March 2012.. but whenever I input March 10, 2012 to March 30, 2012 the SQL works perfectly.. any help will be appreciated. thanks
SELECT
agentname, noofcalls, qualified, booking, resched,
actualbooking, sales, remarks,
concat(month,' ',day,',',year) as 'date'
FROM
tblagents
WHERE
(month between '" & cbosmonth.Text & "' AND '" & cboemonth.Text & "')
AND (day between '" & cbosday.Text & "' AND '" & cboeday.Text & "')
AND (year between '" & cbosyear.Text & "' AND '" & cboeyear.Text & "')"
you are doing string comparisons in each of your 'between'. Every number starting with a 1, a 2 or a 3, regardless of what follows it, i.e. 21, or 26, or 31, they are all lower than 7 if you look at them as strings. 1 to 30 works because you're only leaving 31 behind, and 30 < 31 as a String as well.
Do the concatenation first and then the between:
WHERE concat(month,' ',day,',',year)
BETWEEN concat(cbosmonth.Text,' ', cbosday.Text,' ',cbosyear.Text)
AND concat(cboemonth.Text,' ', cboeday.Text,' ',cboeyear.Text)
(check out for correct syntax, I'm just copy pasting from your question, not tried it)
BTW, unless you have a reason to, you probably should be storing the entire date in a single column with the right data time (datetime, timestamp, ...) and not three separated columns.
That is the incorrect way to store a date in database.
Change the date column to datatype date.
You could then do:
SELECT *
FROM table
WHERE bookingDate between to_date ('2012/03/01', 'yyyy/mm/dd')
AND to_date ('2012/03/07', 'yyyy/mm/dd');
This approach is wrong.
In order for a date to be between two interval dates it does not have to have a day number between the two dates, e.g. (pseudocode)
date = May-25-2012; startDate = March-15-2012, endDate = June-01-2012
date is clearly between startDate and endDate, yet
day(date) is 25, which is not between day(startDate) = 15 and day(endDate) = 1
more, as 15 is larger than 1, there are no numbers between them, so that condition will always be false
Similar example can be made for the month part of the date (e.g. date = May-25-2012; startDate = September-15-2010, endDate = Match-01-2015)
You need to take the values for day, month, year, and construct a Date either in the application or on the server and use that to compare the values against.
To make a date from text fields in VB
Dim startDate = new DateTime(
Convert.ToInt32(cbosyear.Text),
Convert.ToInt32(cbosmonth.Text),
Convert.ToInt32(cbosday.Text))
Note that this will fail if the user enters, e.g. "some text" for the year value. You'll need to add some data validations to achieve that.
To make a datetime from parts in SQL Server, take a look here, there are quite a few techniques explained.
Also, you should always avoid just pasting values into the sql string, that's asking for sql injection problems. You should do something like this:
Dim command = new SqlCommand()
command.CommandText = "SELECT .... FROM tblagents where DATEFROMPARTS(year, month, day) between #startDate AND #endDate"
command.Parameters.AddWithValue("#startDate", startDate)
command.Parameters.AddWithValue("#endDate", endDate)

SQL Server date between LIKE

Please help me how to insert LIKE % in date between. Example is:
SELECT *
FROM table
WHERE Date BETWEEN '" & startDate & "%'" AND '" & endDate & "%'"
So in this code where i should put LIKE so that data will appear?
example if i set like this
SELECT *
FROM table
WHERE Date LIKE '" & startDate & "%'"
it's working..LIKE meant read either startdate or %..for starting it will read %
Try this :
"Select (listOfFields)
FROM TABLE
where CONVERT(VARCHAR(25), Your_DATE, 126) BETWEEN 'Start_date%' AND 'EndDate%'";
Try something like this
SELECT * from table
WHERE CONVERT(VARCHAR, DateField, 120) BETWEEN '2010%' AND '2012%'
If the dates are of type string, you can't use BETWEEN*.
If the dates are of type date or datetime, you can't use LIKE.
*Actually between might work with text because b is between a and c but it will not return correct results with date strings.