+ Reply to Thread
Results 1 to 8 of 8

Thread: Command Object - Parameters

  1. #1
    Barn Enthusiast Flam is an unknown quantity at this point Flam's Avatar
    Join Date
    Dec 2008
    Posts
    249
    Rep Power
    4

    Command Object - Parameters

    I'll do the best I can to explain my dilemma. I'm using Crystal XI and have a report where I use several Command Objects and link them in the report.

    Each command object is a SQL statement for a day of the week.

    EX: Command1 = Sun
    Command2 = Mon
    Etc

    Within the SQL statement, I set up a parameter on the "Where" clause of each Command object that looks like this:

    Work.Date={?Sun}
    Work.Date={?Mon} etc

    The Work.Date field is an int datatype, not an actual date field. In most of my raw SQL statements, I do a conversion such as this:

    Cast(
    Substring(Convert(Varchar,Work.Date),5,2)+'-'+
    Substring(Convert(Varchar,Work.Date),7,2)+'-'+
    Substring(Convert(Varchar,Work.Date),1,4) as Datetime)

    This will typically get the format that I need. When I do this in the Select statement of the Command Object, it returns the data in the Crystal Report in the correct format.

    However, what I'd like to do is something like this:

    WHERE
    Cast(
    Substring(Convert(Varchar,Work.Date),5,2)+'-'+
    Substring(Convert(Varchar,Work.Date),7,2)+'-'+
    Substring(Convert(Varchar,Work.Date),1,4) as Datetime)={?Sun}

    and set the parameter to datatype Date or Datetime in the Create Parameters box so I can use the calendar to select dates on prompt. However, it's throwing an error each time I run that. Does anyone have any suggestions?

    Thanks!

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    What's the error you get?
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  3. #3
    Barn Enthusiast Flam is an unknown quantity at this point Flam's Avatar
    Join Date
    Dec 2008
    Posts
    249
    Rep Power
    4

    Error

    Hi J,

    Here's a sample command object query that I used:
    SELECT *

    FROM WORK

    WHERE
    Cast(
    Substring(Convert(Varchar,Date),5,2)+'-'+
    Substring(Convert(Varchar,Date),7,2)+'-'+
    Substring(Convert(Varchar,Date),1,4) as Datetime)={?Test}

    (Set datatype in parameter list to both Date and Datetime)

    Here's the error:

    Failed to retrieve data from the database.
    Details: 22007:[Microsoft][ODBC SQL Server Driver][SQL Server]Conversion failed when converting datetime from character string. [Database Vendor Code: 241]

    Not sure if that helps

  4. #4
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    My guess is that one or more of the values in the following aren't forming valid dates for the Convert function to work properly:
    Code:
    Substring(Convert(Varchar,Work.Date),5,2)+'-'+
    Substring(Convert(Varchar,Work.Date),7,2)+'-'+
    Substring(Convert(Varchar,Work.Date),1,4)
    
    What datatype is Work.Date? Why do you need to build a date string in the WHERE clause like that?
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  5. #5
    Barn Enthusiast Flam is an unknown quantity at this point Flam's Avatar
    Join Date
    Dec 2008
    Posts
    249
    Rep Power
    4

    *error*

    Hi J,

    Thanks for the reply.

    the Work.Date field is data type INT. It's an integer
    stored in an 8 digit number in yyyymmdd format but it
    is only date field in name, not in type.

    The conversion is necessary in order to convert it from data
    type INT to data type Datetime. My supposition for the parameter
    list was that I'd need to get it converted to a datetime data type
    in order to use the calendar.

    I hope that makes sense!

  6. #6
    I like Data Cubes too... Lauramc has a spectacular aura about Lauramc has a spectacular aura about Lauramc's Avatar
    Join Date
    Mar 2008
    Location
    Far Far Away
    Posts
    387
    Real Name
    Laura
    Rep Power
    5

    I think jmh is likely right... the datetime conversion must not be working properly. Do you have SQL server? Can you test the conversion by running only that part of the process? See if you can find out what numbers each part of the conversion is returning. That should give you a good idea as to what is failing. Just a thought!

    Good luck!
    "The Enrichment Center is required to remind you that first you will be baked, then there will be cake." - GLaDOS

  7. #7
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Yup, I would tend to agree that perhaps you have a value in the database that is not a valid date in the integer format. Try running the following query to see if there are any dates that may be of an invalid format:
    Code:
     Select work.date 
    From Work
    Where len(convert(varchar, work.date)) <> 8 
       or work.date < 19000101
    
    See if that weeds any out.

    You could also try working the WHERE clause the other way around by converting the input date parameter to the integer format. A benefit from this would be that you would need do the conversion only once rather than on each record in the database.
    Last edited by Wolffy; June 2nd, 2009 at 04:16 PM.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  8. #8
    Barn Enthusiast Flam is an unknown quantity at this point Flam's Avatar
    Join Date
    Dec 2008
    Posts
    249
    Rep Power
    4

    Fixed

    Great ideas guys! I hadn't even thought of that. Yes, there will always be dates=0 because the field is not nullable. And there are mistakes in old data that I'm not allowed to change so we'll have some values from before the 20th century.

    Sooo...my work around was adding the following filters in the where clause:

    And Work.Date>0
    AND LEN(Work.Date)=8

    Thank you so much, that helps immensely!

+ Reply to Thread

Similar Threads

  1. Too few parameters
    By todd2006 in forum ASP Development
    Replies: 8
    Last Post: March 11th, 2009, 06:37 AM
  2. No value given for one or more required parameters Error
    By PhloooooIsFlo in forum ASP Development
    Replies: 6
    Last Post: August 13th, 2008, 12:13 PM
  3. too few parameters error
    By todd2006 in forum SQL Development
    Replies: 3
    Last Post: August 4th, 2008, 12:16 PM
  4. Object reference not set to an instance of an object
    By Shem in forum .NET Development
    Replies: 4
    Last Post: July 22nd, 2008, 05:59 AM
  5. Object reference not set to an instance of an object
    By jmurrayhead in forum .NET Development
    Replies: 1
    Last Post: May 29th, 2008, 11:16 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO