Problem Comparing Dates In Store Procedure

Mar 17, 2004

Hi,





Below is my store procedure. I am trying to capture all orders between a startdate and a enddate. When I use the Where #1(see below- I have 2 where clauses) it allows me to go from one year to another. For example, startdate of 2/18/2003 - enddate of 2/19/2004 will return all orders in my table except for 2/19/2004. Probably because of the time portion.


When I use the Where #2, it doesn't go back a full year. The startdate is reacting like 2/18/2004 and not 2003. The records I get are only for dates 2/18/2004-2/19/2004.





How do I create a where clause which will go back a year or more and include the enddate orders? Any help will be very much appreciated.





Thanks,





poc1010








alter Procedure GetOrdersBetweenDates





@startdatesmalldatetime=null,


@enddatesmalldatetime=null





as





select o.orderid,(convert(varchar(12),o.orderdate,101)) as Date


,o.orderamt,o.shipamt,o.njtaxamt,o.totalamt


,c.bfirstname+' '+c.blastname as BName,c.baddress1,c.baddress2


,c.bcity,c.bstate,c.bzip


,c.sfirstname+' '+c.slastname as SName,c.saddress1,c.saddress2


,c.scity,c.sstate,c.szip


from orders o inner join customers c on o.orderid=c.orderid


1.)where o.orderdate between @startdate and @enddate


2.)where convert(varchar(12),o.orderdate,101) between


convert(varchar(12),@startdate,101) and convert(varchar(12),@enddate,101)


order by o.orderid





exec getordersbetweendates '2/18/2003','02/19/2004'

View 1 Replies


ADVERTISEMENT

Comparing Dates With Today Dates

Jun 21, 2005

I want to know if there is a way to compare dates in the sql statement with dates that I input into a database and todays date.  the datatype that I'm using is smalldatetime.The statement I used is:Select Date from Table where Date > 'Today.now'I get an errorCould this be done or is there another approach?

View 1 Replies View Related

Comparing Dates

Jun 26, 2006

Hello,
 
I am trying to retrieve the data that are not more than 3 months. How do I do this? The closest thing that I can do is...
 
(dbo.CLASSIFIEDADS.PostDate > CONVERT(DATETIME, '2006-03-26 00:00:00', 102))
I want to be able to put 3 months in there somehow...
Thanks in Advance!

View 2 Replies View Related

Comparing Dates In SQL

Jan 28, 2006

I am wondering how I would create a SELECT that will select the most recent date from one of two tables. For example, table1 has a field called LastUpdate and table 2 has a field called LastUpdate.  I need to grab only the most recent date.   I tried this using an inner join...but that didn't work because it only picks the lastupdate form one table only.  talbe1 and table2 are tied by table2.table1id. 
Can anyone help?
 

View 7 Replies View Related

Comparing Dates.

May 23, 2006

Hi All,
I have a database field (datestamp) which returns the date the records were inserted into the database. The datestamp was created with the now(); function in .net and is in the following format: 5/23/2006 2:27:45 AM
I basically want to return all records that were inputted more than 28 days ago. I have had alook though some other posts and below is the closest query that i could find but unfortunately it does not work for me.
SELECT id, datestampFROM table WHERE datestamp > DateAdd(d, 28, GetDate())
Thanks in advance,
Jake

View 2 Replies View Related

Comparing Dates

Jul 10, 2001

How do i compare 2 dates without comparing their Time portions. I have a Datetime column in my table which for example has values :-

6/6/2001 7:23:42 PM
6/6/2001 8:01:08 PM and so on....

I have a Stored Procedure in which i am trying to fetch values from this table. The SP has 2 DateTime parameters @DateFrom and @Dateto. This SP is called from a Web page. On the Web Page the user just enters mm/dd/yyyy values for the 2 Date parameters. Therefore the Time portions defaults to 12:00 AM. So when the user passes 6/6/2001 as Date From and 6/6/2001 as Date To values, he doesn't get back the 2 records mentioned above since the Time portion makes the 2 records fall out of the date range.

Is there a way to just do a comparison on the Date portions i.e. give me all the records for 6/6/2001 irrespective of the time. I don't wan't to do individual comparisons for Day then Month and then Year.

Thanks
Sanjay.

View 5 Replies View Related

Comparing Dates

Dec 10, 2004

i am using sql server ver 7

i am trying to do

select *
from independent_investment_certificate_temp

where independent_investment_certificate_temp.ticker_key = 31

and convert(varchar(40), cast(independent_investment_certificate_temp.maturity_date AS datetime),107) >
convert(varchar(40), cast('2006-12-10 10:29:50.733' AS datetime),107)

independent_investment_certificate_temp.maturity_date value is '2005-01-21 00:00:00.000'
the query above returns a value even though '2005-01-21 00:00:00.000' is clearly < '2006-12-10 10:29:50.733'
but why can anyone plz explain.. how can i compare dates stripping tyhem of the time so that all dates on the same day will be
equivalent regardless of the time

View 1 Replies View Related

Comparing Two Dates - Need Help!

Aug 25, 2005

Hi, i got this query below, which I'd like to use to compare two sets of date...


select count(*) as rec, convert(char(6),dateEntered,13) as dateEntered from EmailTracker where chtype = '1' AND convert(char(12),dateEntered,13) >= '"& stdate &"' AND convert(char(12),dateEntered,13) <= '"& endate &"' group by convert(char(6),dateEntered,13)


the format of the stdate and dateEntered (a datetime datatype) looks like - 25 Aug 2005

right now, the query simply uses the first 2 letters of the date to fetch the record. That would not do the job as i want the thing to compare the whole lot! not just the 1st two letters...

Any help pls?

View 5 Replies View Related

Comparing Dates

Dec 16, 2004

Does anyone know of a quick and easy to compare 2 datetime values based only on the month and year.

For example,

FromDate = 11/30/2004
ToDate = 12/30/2004

I just need to compare the 11/2004 to 12/2004 using function like <=, =, >=, etc.

Any suggestions?

Thanks

View 5 Replies View Related

Comparing 12-Hr Dates

Apr 4, 2008

I will try to explain my problem as best as possible without going into too much detail.

Basically I have two tables,
AgentSchedule
AgentPunchData

These tables both contain agent names.

The schedule table has a column called "SHEND" which is a DATETIME datatype which identifies when an agent shift should end.

The entries all have a date attached which means nothing, for example "12/30/1899 9:00:00 PM"

The PunchData table has a column called "LOGOUT" which is also a DATETIME, the entries will have the real date and look something like this: "3/28/2008 9:23:00 PM"

I need to strip the date out of these functions so I can compare the times. I need to query the databases and return any instances where the LOGOUT time is > the shift end time (SHEND).

So far nothing I've tried has been successful. I'm completely stumped, I've tried so many different things and nothing compares the two times correctly. SQL DATETIME is the biggest pain in the ass, I wish we had SQL 2008 so I could just use TIME.

View 6 Replies View Related

Comparing Dates In SQL

Apr 9, 2008

hi, i have a table with persons(babies) and their date of birth. I want to delete the persons above 6 months old can u plse tell me the query ?

delete
from babies
where ......

View 7 Replies View Related

Comparing Dates

Jun 3, 2008

Hello,

I have some code that is trying to first find the first date of service that a person visited. Using the min function is not working. Is there another function? Right now I am still getting multiple rows for each person instead of just the first visit.

Then I want to be able to compare the first date of service to the date of birth and determine if the patient first visited before they were 2 years old. I can't figure out how to do this.

select distinct b.person_id,b.date_of_birth,min(a.enc_timestamp)
from patient_encounter a
join person b on a.person_id = b.person_id
where b.date_of_birth >= '20060301' and b.date_of_birth <= '20060531'
group by b.person_id,b.date_of_birth,a.enc_timestamp

View 4 Replies View Related

SQL QUERY Help Please.. (Comparing Dates)

Dec 1, 1998

hi,

i would like to create an SQL query, part of which involves comparing dates.

i have something like

Select ID, CONVERT(varchar(10), StartDate, 101), Name from Table WHERE StartDate .....

how can i compare the StartDate to something i have in the form of a string? for example, i have
Date = 12/12/1998. and i would like to select where the StartDate in the DB = '12/12/1998'.

i'd appreciate all the help i can get.

thanks,
sb

View 1 Replies View Related

Comparing Dates (older Than)

Feb 21, 2007

Hi Guys,

I am really stuck on exactly how I can do this. I can sudo it in my mind but can't figure out how I would code it.

What i want to do is select all records from a table that the log_date is older than 7 days.

If I was comparing a date i would imagine i would do something like

log_date < NOW()-7

I have looked at:

SELECT player_name, player_last_logon_date,DATEDIFF(day, player_last_logon_date, GETDATE()) AS NumberOfDays from player

select player_name, player_last_logon_date, datepart(dd,player_last_logon_date) from player

but I dont think either is really right. any and all advice would be great!

Thanks

View 2 Replies View Related

Problem Comparing Dates

Nov 16, 2005

Basically, Im trying to return some data from an Access library database into a Java app using an SQL statement. Heres the code:


PHP Code:




 Statement st = conn.createStatement();
            String sql = "SELECT b.BorrowerNo, b.FirstName, b.LastName, COUNT(*) " +
            "FROM Borrowers b, Loans l WHERE l.BorrowerNo = b.BorrowerNo " +
            "GROUP BY b.BorrowerNo, b.FirstName, b.LastName ORDER BY COUNT(*) DESC";

            ResultSet rs = st.executeQuery(sql); 






This works fine, returns the BorrowerNo, FirstName, LastName, and COUNT is how many books they have loaned.

What I need it to do though, is just return loans that are late. I have a column in the Loans table called DateDueBack, so I think I need to compare this date to the current date in the WHERE statement.

I tried add this to my WHERE after l.BorrowerNo = b.BorrowerNo: AND l.DateDueBack > 16/11/2005

But this didn't work, it returned all the loans, regardless of whether they were before or after the date in the query.

Can anyone please help with this statement? I've never tried to compare dates before.

View 2 Replies View Related

Comparing Dates Of Different Lengths

Apr 9, 2008

Hi All,

Is there a way to compare two dates fields that are different in length? For example, I have table1.datefield with a date of '040808' and table2.datefield with a date of '04082008'. Just comparing the two fields is not coming up equal. Has anyone had this situation before?

Thanks.
Frank

View 1 Replies View Related

Transact SQL :: Comparing Two Dates

Aug 4, 2015

I have a date comparison situation in which I will have a column with a date and will have another value containing a GETDATE() minus two weeks. I want to be able to compare both dates and get the MIN date between the two. I'm not sure how to use the MIN(Date) in this scenario since the comparison won't be between two different columns, but between one column and a random date generated by the GETDATE() minus two weeks.

View 6 Replies View Related

Comparing The Dates In Different Rows

May 10, 2008



Hi,

I have a table that holds pay rate changes with a field for the rate start date and a field for the rate end date. When an employee gets given a new pay rate, the existing rate is given an end date and a new row is added with the rate start date being the day following the end date of the old pay rate.

I need to identify the staff who have had a rate change within the past month, therefore an end date on one row that is within one month of the current month, and a start date on another row that is one day after an end date on a separate row and within one month of the current month.

Is someone able to help me out please?

Thanks

View 8 Replies View Related

Comparing Dates In MS SQL 2000 Query

Feb 8, 2008

Hi,
I have a table that temporarily stores information. Before I insert new data in the table, I want to delete the previous day's data.

This is the query I'm using
Code:

q="DELETE FROM SearchTemp WHERE theDate < "&Date()
set RS2 = DB.Execute(q, ra, adCmdText)

I should say I'm using ASP & VBScript.

Problem is, this isn't deleting the previous day's dates.

The dates are stored in this format: m/d/yyyy.

I've searched through old posts but none of the solutions offered have worked for me so far. Anyone have any suggestions?

Thanks!

View 4 Replies View Related

Comparing Dates(Minutes, Hours Etc)

Jan 30, 2006

guys - is this a decent query to pull all columns (dateCreate)
that have a timestamp less than five minutes?
i know its simple, but i've never done a date compare with minutes or hours
in sql server
thanks
rik:o



select top 10 * from ptpuritm
where datediff(MINUTE,dateCreate,getdate()) <=5



select top 10 * from ptpuritm
where datediff(MINUTE,dateCreate,current_timestamp) <=5

View 3 Replies View Related

Function Using Comparing Dates Not Working Right

Sep 12, 2006

Hi,

I'm trying to write a function to return all notes with date. Sample data for 1 record=187189 as follows:
iincidentid,iWorkNoteId,iSeqnum, dtEntryDate, workNoteAll
1871893440 1 2006-04-24 note1
1871893545 1 2006-06-22 note2
1871893547 1 2006-06-22 note3
1871893653 1 2006-08-10 note4
1871893653 2 2006-08-10 note5

funtion will return = 2006_08-10 note4 note5 for iincidentid=187189
-----------------------------------------------------
CREATE FUNCTION dbo.getIncidentNotesRev(@iIncidentID int)
RETURNS varchar(8000)
AS
BEGIN
declare @incidentId int
declare @worknoteid int
declare @worknotesaveid int
declare @seqnum int
declare @dtEntryDate smalldatetime
declare @worknoteall varchar(8000)
declare@allnotes varchar(8000)
declare @currentWEDate smalldatetime
declare @beginWEDate smalldatetime

select @allnotes=''
select @currentWEDate=currentweekEndDate from csCurrentweekEndDate --get the current week end date
select @beginWEDate = DATEADD(d, - 28, @currentWEDate)--get the last 4 weeks

declare CursorIncident CURSOR
LOCAL FOR SELECT iIncidentId, iWorkNoteID, iSeqNum, dtEntryDate,worknoteall FROM dbo.rpt_weekly_prospect_status_vw
where iIncidentId=@iIncidentID order by iWorkNoteId

OPEN CursorIncident
FETCH NEXT FROM CursorIncident INTO @incidentId,@worknoteid,@seqnum,@dtEntryDate,@work noteall

--store 1st record of cursor
select @worknotesaveid =@worknoteid
WHILE (@@FETCH_STATUS=0)
BEGIN
if @dtEntryDate >=@beginWEDate AND @dtEntryDate <= @currentWEDate
Begin
if @worknotesaveid <> @worknoteid
Begin
Select @allnotes = @allnotes + @dtEntryDate + @worknoteall
End
else
BEgin
select @allnotes = @allnotes + @worknoteall
End

select @worknotesaveid = @worknoteid --save next worknoteId
End
else
Begin
select @allnotes=''
End
FETCH NEXT FROM CursorIncident INTO @incidentId,@worknoteid,@seqnum,@dtEntryDate,@work noteall
END --WHILE (@@FETCH_STATUS=0)

CLOSE CursorIncident
DEALLOCATE CursorIncident

return @allnotes
END

----------
Function not working right. I appreciate any help.
Thanks in advance.

View 3 Replies View Related

Creating A Boolean EvalExpression Comparing Dates

Aug 21, 2007

To the experts in the field:
There is probably a very simple solution that is avoiding my grasp.

I have a For Loop which I want to execute as long as a variable called BeforeRunDt = CurrentDate. Both are DateTime data types and I am using the following expression:

@BeforeRunDt==@CurrenDate

I get an error stating "Cannot convert expression value to propeerty type"

I understand that the result of the expression should be a boolean value but am just struggling on how to create it.

Thanks!

View 6 Replies View Related

DateTime Dates Store Wrong

Dec 14, 2007

I have a stored procedure to add a poeersons DOB to a table. The date is being stored as Dec 12 1980

Instead of as 12/12/1980

The table uses a datetime data type.
The data type of the date being passed into the stored procedure is also set to datetime. (@DOB datetime) Even though the value passed into the procedure is 12/12/1980 It saves the date as Dec 12 1980

What do I need to do to fix this?





Miranda

View 2 Replies View Related

I Can't Store Dates In The DD/MM/YYYY Format.

Oct 18, 2007

ok so this is my table structure
[odcCode] [int] NOT NULL ,
[insCode] [varchar] (11) COLLATE Modern_Spanish_CI_AS NOT NULL ,
[odcQty] [int] NOT NULL ,
[odcCostoUnitario] [numeric](18, 4) NOT NULL ,
[odcISV] [bit] NULL ,
[odcStatusInsumo] [varchar] (3) COLLATE Modern_Spanish_CI_AS NULL ,
[odcInsArrivalDate] [datetime]

INSERT INTO OrdenCompraDetalle
(odcCode, insCode, odcQty, odcCostoUnitario, odcISV, odcStatusInsumo, odcInsArrivalDate)
VALUES
(697,'MIN-000028',1,22.3250,0,'PEN', '18/10/2007 12:00:00 a.m.')

and when I run, the insert command I get the next error:
Syntax error converting datetime from character string.


The date is taken from a standar DateTime Picker control from Visual Studio 2003 and I can't change the regional settings on the computers (that's not an option)

What can I do to fix the problem and be able to insert the date in any format?

View 3 Replies View Related

Problem Getting Recoreds From Store Proedure Between Dates...

Aug 23, 2007

i have a gridView and i want to get the recoreds between specific dates.. so i put two calendars to select the dates...
and they will filter the recoreds..(i seleced dates and it returns non recors and theres recored betwenn those dates and in a SQL View it works )
What can i do? Thanks
Store Proceure Function:
 CREATE PROCEDURE sp_FindTnoaGrid
@SearchTnoa nvarchar(14),
@beginDate as nvarchar(50),
@endDate as nvarchar(50)
AS
-- order by dbo.V_tnuot.t_erech desc--



if (@beginDate='%') or (@endDate='%')
SELECT dbo.V_tnuot.kod_lakoha, dbo.V_tnuot.t_erech, dbo.V_tnuot.t_peula, dbo.V_tnuot.scum_peula as sum,
dbo.V_tnuot.strHpoalimSugKodTnua, dbo.V_tnuot.DescSugPeula, cast(year(cast(@beginDate as datetime)) as nvarchar)+'-'+cast(month(cast(@beginDate as datetime)) as nvarchar)+'-'+cast(day(cast(@beginDate as datetime)) as nvarchar) as try,
dbo.V_tnuot.Hpoalim_HodeshSahar
FROM dbo.V_tnuot LEFT OUTER JOIN
dbo.tblCategTnuot_KodPeula ON dbo.V_tnuot.strHpoalimSugKodTnua = dbo.tblCategTnuot_KodPeula.idKodTnua

WHERE ( kod_lakoha = @SearchTnoa)

if (@beginDate<>'%') and (@endDate<>'%')
SELECT dbo.V_tnuot.kod_lakoha, dbo.V_tnuot.t_erech, dbo.V_tnuot.t_peula, dbo.V_tnuot.scum_peula as sum,
dbo.V_tnuot.strHpoalimSugKodTnua, dbo.V_tnuot.DescSugPeula,
dbo.V_tnuot.Hpoalim_HodeshSahar
FROM dbo.V_tnuot LEFT OUTER JOIN
dbo.tblCategTnuot_KodPeula ON dbo.V_tnuot.strHpoalimSugKodTnua = dbo.tblCategTnuot_KodPeula.idKodTnua

WHERE ( kod_lakoha = @SearchTnoa) and (dbo.V_tnuot.t_peula BETWEEN cast(@beginDate as datetime) and cast(@endDate as datetime) )
GO 

View 1 Replies View Related

Call Store Procedure From Another Store Procedure

Nov 13, 2006

I know I can call store procedure from store procedure but i want to take the value that the store procedure returned and to use it:

I want to create a table and to insert the result of the store procedure to it.

This is the code: Pay attention to the underlined sentence!

ALTER PROCEDURE [dbo].[test]



AS

BEGIN

SET NOCOUNT ON;



DROP TABLE tbl1

CREATE TABLE tbl1 (first_name int ,last_name nvarchar(10) )

INSERT INTO tbl1 (first_name,last_name)

VALUES (exec total_cash '8/12/2006 12:00:00 AM' '8/12/2006 12:00:00 AM' 'gilad' ,'cohen')

END

PLEASE HELP!!!! and God will repay you in kind!

Thanks!

View 7 Replies View Related

T-Sql Stored Procedure Comparing String

Feb 24, 2008

HiI have a problem trying to compare a string value in a WHERE statement. Below is the sql statement.  ALTER PROCEDURE dbo.StoredProcedure1(@oby char,@Area char,@Startrow INT,@Maxrow INT, @Minp INT,@Maxp INT,@Bed INT
)

ASSELECT * FROM
(
SELECT row_number() OVER (ORDER BY @oby DESC) AS rownum,Ref,Price,Area,Town,BedFROM [Houses] WHERE ([Price] >= @Minp) AND ([Price] <= @Maxp) AND ([Bed] >= @Bed) AND ([Area] = @Area)) AS AWHERE A.rownum BETWEEN (@Startrow) AND (@Startrow + @Maxrow)  The problem is the Area variable if i enter it manually it works if i try and pass the variable in it doesn't work. If i change ([Area] = @Area) to ([Area] = 'The First Area') it is fine. The only problem i see is that the @Area has spaces, but i even tried passing 'The First Area' with the quotes and it still didnt work.Please help, its got to be something simple.Thanks In Advance 

View 2 Replies View Related

Comparing Two Strings Inside A Stored Procedure

Aug 14, 2006

Basically I have two strings.  Both strings will contain similar data because the 2nd string is the first string after an update of the first string takes place.  Both strings are returned in my Stored Procedure
For example:String1 = "Here is some data.  lets type some more data"String2 = "Here's some data. Lets type some data here"I would want to change string2 (inside my Stored Procedure) to show the changed/added text highlighted and the deleted text with a strike though.
So I would want string2 to look like thisstring2 = "Here<font color = "#00FF00">'s</font> <strike>is</strike> some data. <font color = "#00FF00">L</font>ets type some <strike>more</strike> data <font color = "#00FF00">here</font>"
Is there an way to accomplish this inside a stored procedure?

View 2 Replies View Related

Utilities For Comparing Stored Procedure Results?

Dec 11, 2007

I have large stored procedures in SQL Server 2005 that often get updated. Sometimes it is very difficult to recognize how one change could impact the overall results. I would like to have some sample input that I could test during changes and see it compare the data results before & after my changes. This would help me quickly identify if the results are fine to pass through.

So basically I would like an easy way to compare the results of 2 stored procedures. Any suggestions or utilites that would help me do this?

View 3 Replies View Related

Compare Utilities For Comparing Stored Procedure Results?

Dec 10, 2007

I have large stored procedures in SQL Server 2005 that often get updated. Sometimes it is very difficult to recognize how one change could impact the overall results. I would like to have some sample input that I could test during changes and see it compare the data results before & after my changes. This would help me quickly identify if the results are fine to pass through.

So basically I would like an easy way to compare the results of 2 stored procedures. Any suggestions or utilites that would help me do this?

View 6 Replies View Related

Store Procedure Not Store

Nov 20, 2013

My store Procedure is not save in Strore Procedure folder at the time of saving it give me option to save in Project folder and file name default is SQLQuery6.sql when i save it after saving when i run my store procedure

exec [dbo].[SP_GetOrdersForCustomer] 'ALFKI'

I am getting below error :

Msg 2812, Level 16, State 62, Line 1
Could not find stored procedure 'dbo.SP_GetOrdersForCustomer'.

View 13 Replies View Related

SQL Stored Procedure With Dates

May 31, 2004

A table in my database has a field of type smalldatetime called "PeriodEnd". I want to write a simple stored procedure that gets records based on the 'PeriodEnd' field. When I run a query such as

Select * from mytable where PeriodEnd='30/06/1989'

I get an errror saying "The conversion of char data type to a smalldatetime data type resulted in an out-of-range smalldatetime value"

Do I have to use CONVERT in some way to alter my input value?

cheers

View 2 Replies View Related

Passing Dates To A Procedure

Mar 6, 2006

im throwing up some errors

Syntax error converting datetime from character string.

im sending 2 asp:Text fields to the procedure

in the format dd/mm/yyyy

i.e. 06/03/2006

the table im inserting it into is set to datetime

within the procedure i have tried both
@Date1 DateTime,
@Date2 DateTime

@Date1 Nvarchar(50),
@Date2 Nvarchar(50)

and it still throws up Syntax error converting datetime from character string.

now im not sure if i should pass it to the Procedure as Nvarchar then Convert it within the procedure ..

or change the table type to nvarchar and just insert into a non date format table type.

View 11 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved