473,378 Members | 1,078 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Getting current date as part of an Stored Procedure

347 100+
I have the following SP:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     moncallAdd.FirstListing,
  3.     DATEADD(MINUTE, mOnCallAdd.StartOnCallTime,
  4.             DATEADD(DAY, mOnCallAdd.StartOnCallDate, '12/31/1899')) AS OnCallStart,
  5.     DATEADD(MINUTE, mOnCallAdd.duration,
  6.             DATEADD(MINUTE, mOnCallAdd.StartOnCallTime,
  7.                     DATEADD(DAY, mOnCallAdd.StartOnCallDate, '12/31/1899'))) AS OnCallEnd
  8. FROM
  9.     mdr.dbo.mOnCallAdd
  10.     where DATEADD(DAY, mOnCallAdd.StartOnCallDate, '12/31/1899')= DATEADD(ms , 0 - DATEPART(ms , GETDATE()) ,
  11.             DATEADD(s , 0 - DATEPART(s , GETDATE()) , GETDATE())) and
  12.     mOnCallAdd.SchedName = 'arc im' 
  13.  
  14. UNION    
  15. SELECT
  16.     moncallDelete.FirstListing,
  17.     DATEADD(MINUTE, mOnCallDelete.StartOnCallTime,
  18.             DATEADD(DAY, mOnCallDelete.StartOnCallDate, '12/31/1899')) AS OnCallStart,
  19.     DATEADD(MINUTE, mOnCallDelete.duration,
  20.             DATEADD(MINUTE, mOnCallDelete.StartOnCallTime,
  21.                     DATEADD(DAY, mOnCallDelete.StartOnCallDate, '12/31/1899'))) AS OnCallEnd
  22. FROM
  23.     mdr.dbo.mOnCallDelete
  24. where DATEADD(DAY, mOnCallDelete.StartOnCallDate, '12/31/1899')= DATEADD(ms , 0 - DATEPART(ms , GETDATE()) ,
  25.             DATEADD(s , 0 - DATEPART(s , GETDATE()) , GETDATE())) and
  26.     mOnCallDelete.SchedName = 'arc im'
  27.  
and what I'm trying to do is as follows:

If I have a schedule that runs from 9 a.m. today to 5 p.m. today, and I run my SP anytime during that time frame, I'd like to see that schedule as part of my dataset. I can make this work for date alone, but can't figure out the time portion. Can anyone offer any assistance on this?

Thank you

Doug
Aug 12 '11 #1
10 4603
Rabbit
12,516 Expert Mod 8TB
I'm not sure what you want because you're already using getdate which will return the current date and time.
Aug 12 '11 #2
dougancil
347 100+
Rabbit,

It's not producing any results. For example, if I run this query by date alone, then it'll work . But because my schedules have start times and end times and what I'm looking to do is something with a BETWEEN statement, but I just don't know what. It would be something like between midnight and getdate ... I just don't know how to write it.

For example ... when I run this with date alone ... here's what I get:

SETON NW - 2011-08-12 19:00:00.000 2011-08-13 07:00:00.000
HEART HOSPITAL - 2011-08-12 07:00:00.000 2011-08-12 13:00:00.000
SETON MAIN - 2011-08-12 07:00:00.000 2011-08-12 17:00:00.000
SETON MAIN - 2011-08-12 17:00:00.000 2011-08-12 23:00:00.000


and you can see that it shows me both values before the current time and after the current time. What I need to be able to do is to see up to the current time but no later.
Aug 12 '11 #3
Rabbit
12,516 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. DateVar BETWEEN Convert(VARCHAR(10), GetDate(), 101) AND GetDate()
Aug 12 '11 #4
dougancil
347 100+
I'm not sure what you mean as the DateVar. Are you saying that I need to leave in
Expand|Select|Wrap|Line Numbers
  1.    WHERE DATEADD(DAY, mOnCalldelete.AddDate, '12/31/1899')
  2.  
because when I leave that in and run the query, it gives me too many results.

SETON MAIN ADMITS - 2011-08-17 23:00:00.000 2011-08-18 07:00:00.000
SETON MAIN ADMITS - 2011-08-18 07:00:00.000 2011-08-18 15:00:00.000
SETON MAIN ADMITS - 2011-08-19 15:00:00.000 2011-08-19 23:00:00.000
SETON MAIN ADMITS - 2011-08-13 19:00:00.000 2011-08-14 07:00:00.000
SETON MAIN ADMITS - 2011-08-16 15:00:00.000 2011-08-16 23:00:00.000
SETON MAIN ADMITS - 2011-08-17 15:00:00.000 2011-08-17 23:00:00.000
SETON NW - 2011-08-12 07:00:00.000 2011-08-12 19:00:00.000
Aug 12 '11 #5
Rabbit
12,516 Expert Mod 8TB
DateVar is the name of whatever date variable/field you're trying to filter on. Basically, what I posted will give you all records where DateVar is between '08/12/2011 00:00:00.000' and '08/12/2011 13:21:52.158' or whatever time it is currently.
Aug 12 '11 #6
dougancil
347 100+
Rabbit,

As you can see from my query, the only date/variable that I have in this is where I have this line:
where DATEADD(DAY, mOnCallDelete.StartOnCallDate, '12/31/1899')

because i have to convert the table that I'm pulling from to a readable date, there is another field for time. So I have start time and start date as two fields. So I guess, I'll need to have one query for the date, which I guess I'll have to do a datepart of the getdate to provide me with just a date and then another query for just the time.

So then my query will need to look something more like this:

WHERE DATEADD(MINUTE, mOnCallDelete.StartOnCallTime,
DATEADD(DAY, mOnCallDelete.StartOnCallDate, '12/31/1899') BETWEEN Convert(VARCHAR(10), GetDate(), 101) AND GetDate() and

and then the datepart will have to be done for both parts. Any suggestions on how to do that?
Aug 12 '11 #7
Rabbit
12,516 Expert Mod 8TB
There's no need to separate them. And there's no need for DatePart(). I don't know why they are separated in the first place, because they are readable as a regular datetime data type. All you need to do is change DateVar in my example to the datetime value you need. In this case, combining your date field and your time field and converting it to a datetime.
Aug 12 '11 #8
dougancil
347 100+
They are seperated in the database. It wasnt by choice that it was done this way. The date is always going to be the current date (whatever that is) and the current time and will need to match both the start date, and be before or up to the current date/time. But as the values in the table are split as one field for date, and one for time, thats' why I assumed that it would have to be split to find those values. I won't be using a variable for the current date, I've got to find a way to get that as one value and the time as one value and match them both.

(from the table in the database)
This is the value for startdate
38490
this is the value for starttime
1020
Aug 12 '11 #9
Rabbit
12,516 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. DATEADD(MINUTE, mOnCallDelete.StartOnCallTime,
  2.    DATEADD(DAY, mOnCallDelete.StartOnCallDate, '12/31/1899')) 
  3.    BETWEEN Convert(VARCHAR(10), GetDate(), 101) AND GetDate()
Aug 12 '11 #10
dougancil
347 100+
Rabbit, that was it. Thank you very much.
Aug 15 '11 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Vipul Pathak | last post by:
Hello Friends ! I have the Following Code, that Executes a Stored Procedure and Attempt to read a Returned Integer Value from the StoredProc. But It gives Error ... ADODB.Command (0x800A0BB9)...
1
by: Robin Tucker | last post by:
As I had real problems working my head around sp_spaceused, I've written an SP to do it (I also noted a lot of questions about this when "searching"). Pass in a database name and it will return the...
4
by: Richard G | last post by:
I'm a database guy, so go easy on me here. :) How can I get the rowcount of the affected rows of a SQL statement from a stored procedure call? I know that "set nocount on" does not return the...
7
by: Scamjunk | last post by:
Hi, I am basically a linux person being made to work on windows. and i have been given a limited version of the windows i guess - for it doesn't provide a proper help option :(. I sorely miss...
2
by: manontheedge | last post by:
I'm working on a program in VB6 and I need to be able to have the program display the current date ( actually, i just need the day number ), but I have no idea how to do this is VB...is there a built...
2
cassbiz
by: cassbiz | last post by:
I am using strtotime and I have read up on some examples and am getting the wrong output, it jumps by several days instead of one day at a time. Ultimately what I am trying to accomplish is to set...
2
by: jed | last post by:
I have created a stored procedure in SQLExpress management.I need to retrieve a numeric value that the stored procedure creates and use it in a C# application.Please help thanks. USE GO /******...
0
by: rameshonweb | last post by:
Can some one please tell how to get the table name from a procedure in C#. Thanks in advance
4
code green
by: code green | last post by:
Within a stored procedure I am creating a table from an Excel spreadsheet /*Create tmp table from XLS*/ SELECT * INTO tmp_cons FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.