Wednesday, March 7, 2012

BETWEEN DATES (TIME) HELP

Hi,

I need to display the employees from a current work shift. These are the following work shift:

4 AM – 12 PM

12 PM – 8 PM

8 PM – 4 AM

I’m having problems in third one “8 PM – 4 AM”. The next code is the one that I’m working on. If somebody knows a better way to do this let me know or if somebody can help me with this go ahead.

begin

declare @.from_time varchar(25);

set @.from_time = '8:00:00.000 PM';

declare @.to_time varchar(25);

set @.to_time = '4:00:00.000 AM';

declare @.current_date VARCHAR(25);

set @.current_date = getdate();

--select convert(smalldatetime, substring(@.current_date, 0, 13) + @.from_time),

--convert(smalldatetime, substring(@.current_date, 0, 13) + @.to_time),

--getdate();

if getdate() BETWEEN convert(smalldatetime, substring(@.current_date, 0, 13) + @.from_time) AND

convert(smalldatetime, substring(@.current_date, 0, 13) + @.to_time)

print 'OK';

else

print 'NO';

end

That's because of midnight crossover - the dates are changed also. Try this, it's far more simple and reliable:

Code Snippet

declare @.From int, @.To int, @.DT int

select @.From = 20, @.To = 4, @.DT = datepart(hh, getdate())

if @.DT >= @.From or @.DT < @.To
print 'OK'
else
print 'No'

|||Yeah. Thanks.

between dates

in sql2000
"select * from tbl where tbl.date between date1 and date2" - results with
nothing
"select * from tbl where tbl.date >= date1 " - gives result
"select * from tbl where tbl.date >= date1 and tbl.date<=date2" - results
with nothing
how to select between 2 dates?
thanksHow about providing us with real specs, e.g. are the columns really
datetime/smalldatetime? What data is in them? What values are you using
for date1 and date2? What format are they in?
Please see http://www.aspfaq.com/5006 ... otherwise vague questions will
only get vague answers.
"Sam" <focus10@.zahav.net.il> wrote in message
news:ey3GNgirFHA.248@.TK2MSFTNGP14.phx.gbl...
> in sql2000
> "select * from tbl where tbl.date between date1 and date2" - results with
> nothing
> "select * from tbl where tbl.date >= date1 " - gives result
> "select * from tbl where tbl.date >= date1 and tbl.date<=date2" - results
> with nothing
> how to select between 2 dates?
> thanks
>
>|||column date is datetime type
the values are simple:
"select * from tbl where tbl.date between 01/01/2002 and 01/05/2005" -
gives NO RESULT
"select * from tbl where tbl.date >= 01/01/2002" - gives GOO DRESULT
"select * from tbl where tbl.date >=01/01/2002 and <=01/05/2005" - gives NO
RESULT
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:OQO7bjirFHA.3440@.TK2MSFTNGP10.phx.gbl...
> How about providing us with real specs, e.g. are the columns really
> datetime/smalldatetime? What data is in them? What values are you using
> for date1 and date2? What format are they in?
> Please see http://www.aspfaq.com/5006 ... otherwise vague questions will
> only get vague answers.
>
> "Sam" <focus10@.zahav.net.il> wrote in message
> news:ey3GNgirFHA.248@.TK2MSFTNGP14.phx.gbl...
>|||See point number 3 in the first link.
Should I use BETWEEN in my database queries?
http://www.aspfaq.com/show.asp?id=2280
The ultimate guide to the datetime datatypes
http://www.karaszi.com/SQLServer/info_datetime.asp
AMB
"Sam" wrote:

> column date is datetime type
> the values are simple:
> "select * from tbl where tbl.date between 01/01/2002 and 01/05/2005" -
> gives NO RESULT
> "select * from tbl where tbl.date >= 01/01/2002" - gives GOO DRESULT
> "select * from tbl where tbl.date >=01/01/2002 and <=01/05/2005" - gives
NO
> RESULT
>
> "Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in messag
e
> news:OQO7bjirFHA.3440@.TK2MSFTNGP10.phx.gbl...
>
>|||If u r using date1 and date2 as parameters, in ur case date2 must be null.
Even if one of the dates is null, BETWEEN or simple comparison will fail as
happening in ur cases.
pls chk and let me know in case of any dbts
Rakesh
"Sam" wrote:

> in sql2000
> "select * from tbl where tbl.date between date1 and date2" - results with
> nothing
> "select * from tbl where tbl.date >= date1 " - gives result
> "select * from tbl where tbl.date >= date1 and tbl.date<=date2" - results
> with nothing
> how to select between 2 dates?
> thanks
>
>|||(a) you need to delimit datetime values with '
(b) never use ambiguous and silly formats like m/d/y or d/m/y. Is
01/05/2005 January 5th or May 1st? Who knows?
Try:
WHERE date >= '20020101' AND date <= '20050105'
"Sam" <focus10@.zahav.net.il> wrote in message
news:ehA02sirFHA.2592@.TK2MSFTNGP09.phx.gbl...
> column date is datetime type
> the values are simple:
> "select * from tbl where tbl.date between 01/01/2002 and 01/05/2005" -
> gives NO RESULT
> "select * from tbl where tbl.date >= 01/01/2002" - gives GOO DRESULT
> "select * from tbl where tbl.date >=01/01/2002 and <=01/05/2005" - gives
> NO RESULT
>
> "Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in
> message news:OQO7bjirFHA.3440@.TK2MSFTNGP10.phx.gbl...
>|||
> "select * from tbl where tbl.date >= 01/01/2002" - gives GOO DRESULT
What are the date values for these results? Are they all later than
"01/05/2005"?
Perayu|||i found the solution:
@.date1 nvarchar(12), @.date2 nvarchar(12),
'BETWEEN CONVERT(DATETIME, ' + char(39) + @.date1 + char(39) + ', 102) AND
CONVERT(DATETIME, '+ char(39) + @.date2 + char(39) +', 102)'
"Rakesh" <Rakesh@.discussions.microsoft.com> wrote in message
news:790C5553-6B88-45F8-A1D6-4F32CF001010@.microsoft.com...
> If u r using date1 and date2 as parameters, in ur case date2 must be null.
> Even if one of the dates is null, BETWEEN or simple comparison will fail
> as
> happening in ur cases.
> pls chk and let me know in case of any dbts
> Rakesh
> "Sam" wrote:
>

Between date selection

Hi,

I have the following situation.

I want to create reports with a start date parameter and an end date parameter

In my query i use "where date between @.startdate and @.enddate"

i configure my parameters so i get a nice calendar when i generate the report.

the problem is when i select for example a date starting 1 april 2007 and enddate 10 april 2007,

the result in my report gives me only the data from 1 until 9 april and not until 10.

in my database the date is stored as a date time (10/04/2007 17:25:30).

Any suggestion how i can solve my problem?

Greetings

Vinnie

Hello Vinnie,

The problem is that your database stores the time with your date and when you select a date in the date picker, it doesn't have a tme, so anything after midnight gets excluded. Try this as your where clause:

where date >= @.startdate and date < dateadd(d, 1, @.enddate)

Hope this helps.

Jarret

|||

Hi Vinnie,

When I come across this I normally use a conversion in my query:

where convert(varchar(25),date,101) between @.startdate and @.enddate

The conversion eliminates the time from the database field and will pull back all rows with the date portion between the start and end dates.

Simone

|||

Do you have a date picker control on your reporting website, so that instead of the user having to manually input the date in the correct format, they are given a control to pick the date ranges?

(Like datetimepicker control in C#)

Please let me know as that is what I need to do.

|||

Yes I do. You need to change the data type of the parameter in the Report to datetime. Go to "Report" then "Report Parameters" Select the parameter from the list on the left and change the data type to DateTime. This should automatically give the date control.

Simone

Between Date Query

How would I make a query that checks for all dates using a feature that
automatically selects todays date and then 14 days in the future. Meaning
all records between today and 14 days in the future.
Thanks.The SQL should read something like:
SELECT *
FROM MyTable
WHERE MyDateField BETWEEN GETDATE() AND DATEADD(Day, 14, GETDATE())
HTH,
Magendo_man
"Samson" wrote:
> How would I make a query that checks for all dates using a feature that
> automatically selects todays date and then 14 days in the future. Meaning
> all records between today and 14 days in the future.
> Thanks.|||thank you. it worked
"magendo_man" wrote:
> The SQL should read something like:
> SELECT *
> FROM MyTable
> WHERE MyDateField BETWEEN GETDATE() AND DATEADD(Day, 14, GETDATE())
> HTH,
> Magendo_man
> "Samson" wrote:
> > How would I make a query that checks for all dates using a feature that
> > automatically selects todays date and then 14 days in the future. Meaning
> > all records between today and 14 days in the future.
> >
> > Thanks.

Between date problem

I have a SP that gets two dates passed to it. Those dates are then used to
pull records created between those dates. The problem is that the created
date is a timestamp and so when I pass 3/15/2006 and 3/15/2006 no rows are
returned. If I want today's records I have to pass 3/15/2006 and 3/16/2006.
This is my where clause WHERE CONVERT(VARCHAR(10),Created_TS,112) BETWEEN
@.strFromDate AND @.strToDate How can I fix this? Thanks.Don't use BETWEEN. And don't pass in a string in that format, use YYYYMMDD
and SMALLDATETIME parameters. Don't do any string conversions of the value
in the table. This will basically destroy performance and doesn't do a
whole lot for readability of the query, either. Finally, pass in the day
*after* the end of the range. So, for 3/15 -> 3/15, pass in 3/15 and 3/16.
Then, say:
CREATE PROCEDURE dbo.foo
@.rangeStart SMALLDATETIME,
@.rangeEnd SMALLDATETIME
AS
BEGIN
SET NOCOUNT ON;
SELECT ...
WHERE Created_TS >= @.rangeStart
AND Created_TS < @.rangeEnd;
END
GO
e.g.
EXEC dbo.foo @.rangeStart = '20060315', @.rangeEnd = '20060316';
"Phill" <Phill@.discussions.microsoft.com> wrote in message
news:2A9229F5-B2D5-4830-99D1-2C57FAF75D3C@.microsoft.com...
>I have a SP that gets two dates passed to it. Those dates are then used to
> pull records created between those dates. The problem is that the created
> date is a timestamp and so when I pass 3/15/2006 and 3/15/2006 no rows are
> returned. If I want today's records I have to pass 3/15/2006 and
> 3/16/2006.
> This is my where clause WHERE CONVERT(VARCHAR(10),Created_TS,112) BETWEEN
> @.strFromDate AND @.strToDate How can I fix this? Thanks.

Between database servers

Hi all,
I like to access a production database server from my development database,
I need to count how fast one table in the production table grows, is there
any way to do it without actually getting on the production server? linked
server?
Thanks.Yes you can create a linked server
http://sqlservercode.blogspot.com/
"Matthew Z" wrote:
> Hi all,
> I like to access a production database server from my development database,
> I need to count how fast one table in the production table grows, is there
> any way to do it without actually getting on the production server? linked
> server?
> Thanks.|||Thanks. Because the company security reseans, I can't use the linked server,
is there a way to work around?
"SQL" wrote:
> Yes you can create a linked server
> http://sqlservercode.blogspot.com/
>
> "Matthew Z" wrote:
> > Hi all,
> > I like to access a production database server from my development database,
> > I need to count how fast one table in the production table grows, is there
> > any way to do it without actually getting on the production server? linked
> > server?
> >
> > Thanks.|||look up OPENROWSET and OPENDATASOURCE in Books on line
http://sqlservercode.blogspot.com/
"Matthew Z" wrote:
> Thanks. Because the company security reseans, I can't use the linked server,
> is there a way to work around?
>
> "SQL" wrote:
> > Yes you can create a linked server
> >
> > http://sqlservercode.blogspot.com/
> >
> >
> > "Matthew Z" wrote:
> >
> > > Hi all,
> > > I like to access a production database server from my development database,
> > > I need to count how fast one table in the production table grows, is there
> > > any way to do it without actually getting on the production server? linked
> > > server?
> > >
> > > Thanks.|||Thanks for reply.
Even I have sa permission to all sql servers, I still can't have have access
from one server to a different server.
"SQL" wrote:
> look up OPENROWSET and OPENDATASOURCE in Books on line
> http://sqlservercode.blogspot.com/
>
> "Matthew Z" wrote:
> > Thanks. Because the company security reseans, I can't use the linked server,
> > is there a way to work around?
> >
> >
> > "SQL" wrote:
> >
> > > Yes you can create a linked server
> > >
> > > http://sqlservercode.blogspot.com/
> > >
> > >
> > > "Matthew Z" wrote:
> > >
> > > > Hi all,
> > > > I like to access a production database server from my development database,
> > > > I need to count how fast one table in the production table grows, is there
> > > > any way to do it without actually getting on the production server? linked
> > > > server?
> > > >
> > > > Thanks.|||Matthew,
as SQL said:
1. Linked Server
2. OPENROWSET or OPENDATASOURCE - neither requires a linked server to be
created
3. Use EM, QA, OSQL or some other application to connect directly to your
other server.
4. Job or DTS to get the value required to your local db
HTH
Jerry
"Matthew Z" <MatthewZ@.discussions.microsoft.com> wrote in message
news:46E45877-8585-4709-AEA8-BDD7886E7FC5@.microsoft.com...
> Thanks for reply.
> Even I have sa permission to all sql servers, I still can't have have
> access
> from one server to a different server.
> "SQL" wrote:
>> look up OPENROWSET and OPENDATASOURCE in Books on line
>> http://sqlservercode.blogspot.com/
>>
>> "Matthew Z" wrote:
>> > Thanks. Because the company security reseans, I can't use the linked
>> > server,
>> > is there a way to work around?
>> >
>> >
>> > "SQL" wrote:
>> >
>> > > Yes you can create a linked server
>> > >
>> > > http://sqlservercode.blogspot.com/
>> > >
>> > >
>> > > "Matthew Z" wrote:
>> > >
>> > > > Hi all,
>> > > > I like to access a production database server from my development
>> > > > database,
>> > > > I need to count how fast one table in the production table grows,
>> > > > is there
>> > > > any way to do it without actually getting on the production server?
>> > > > linked
>> > > > server?
>> > > >
>> > > > Thanks.|||Another option is to create an xp_sendmail proc job on the production DB
that will mail the result to yourself whenever you execute this job
"Matthew Z" wrote:
> Thanks for reply.
> Even I have sa permission to all sql servers, I still can't have have access
> from one server to a different server.
> "SQL" wrote:
> > look up OPENROWSET and OPENDATASOURCE in Books on line
> >
> > http://sqlservercode.blogspot.com/
> >
> >
> >
> > "Matthew Z" wrote:
> >
> > > Thanks. Because the company security reseans, I can't use the linked server,
> > > is there a way to work around?
> > >
> > >
> > > "SQL" wrote:
> > >
> > > > Yes you can create a linked server
> > > >
> > > > http://sqlservercode.blogspot.com/
> > > >
> > > >
> > > > "Matthew Z" wrote:
> > > >
> > > > > Hi all,
> > > > > I like to access a production database server from my development database,
> > > > > I need to count how fast one table in the production table grows, is there
> > > > > any way to do it without actually getting on the production server? linked
> > > > > server?
> > > > >
> > > > > Thanks.|||Thanks guys, based on your replies,I got a idea, I may try DTS job & send
mail functions. This is not a one time process, it runs every week for
reporting Purpose.
"Matthew Z" wrote:
> Hi all,
> I like to access a production database server from my development database,
> I need to count how fast one table in the production table grows, is there
> any way to do it without actually getting on the production server? linked
> server?
> Thanks.

Between database servers

Hi all,
I like to access a production database server from my development database,
I need to count how fast one table in the production table grows, is there
any way to do it without actually getting on the production server? linked
server?
Thanks.
Yes you can create a linked server
http://sqlservercode.blogspot.com/
"Matthew Z" wrote:

> Hi all,
> I like to access a production database server from my development database,
> I need to count how fast one table in the production table grows, is there
> any way to do it without actually getting on the production server? linked
> server?
> Thanks.
|||Thanks. Because the company security reseans, I can't use the linked server,
is there a way to work around?
"SQL" wrote:
[vbcol=seagreen]
> Yes you can create a linked server
> http://sqlservercode.blogspot.com/
>
> "Matthew Z" wrote:
|||look up OPENROWSET and OPENDATASOURCE in Books on line
http://sqlservercode.blogspot.com/
"Matthew Z" wrote:
[vbcol=seagreen]
> Thanks. Because the company security reseans, I can't use the linked server,
> is there a way to work around?
>
> "SQL" wrote:
|||Thanks for reply.
Even I have sa permission to all sql servers, I still can't have have access
from one server to a different server.
"SQL" wrote:
[vbcol=seagreen]
> look up OPENROWSET and OPENDATASOURCE in Books on line
> http://sqlservercode.blogspot.com/
>
> "Matthew Z" wrote:
|||Matthew,
as SQL said:
1. Linked Server
2. OPENROWSET or OPENDATASOURCE - neither requires a linked server to be
created
3. Use EM, QA, OSQL or some other application to connect directly to your
other server.
4. Job or DTS to get the value required to your local db
HTH
Jerry
"Matthew Z" <MatthewZ@.discussions.microsoft.com> wrote in message
news:46E45877-8585-4709-AEA8-BDD7886E7FC5@.microsoft.com...[vbcol=seagreen]
> Thanks for reply.
> Even I have sa permission to all sql servers, I still can't have have
> access
> from one server to a different server.
> "SQL" wrote:
|||Another option is to create an xp_sendmail proc job on the production DB
that will mail the result to yourself whenever you execute this job
"Matthew Z" wrote:
[vbcol=seagreen]
> Thanks for reply.
> Even I have sa permission to all sql servers, I still can't have have access
> from one server to a different server.
> "SQL" wrote:
|||Thanks guys, based on your replies,I got a idea, I may try DTS job & send
mail functions. This is not a one time process, it runs every week for
reporting Purpose.
"Matthew Z" wrote:

> Hi all,
> I like to access a production database server from my development database,
> I need to count how fast one table in the production table grows, is there
> any way to do it without actually getting on the production server? linked
> server?
> Thanks.