Results 1 to 2 of 2

Thread: Using ' on Stored Procedure Problem

  1. #1

    Thread Starter
    Addicted Member charmedcharmer's Avatar
    Join Date
    Sep 2003
    Posts
    211

    Using ' on Stored Procedure Problem

    Guys Im very very sorry. I just need a quick answer. Here is my stored procedure, Im having an error i think on the bold part.

    VB Code:
    1. CREATE PROCEDURE sp_DFListing_ByDate
    2. @datetime1 datetime, @datetime2 datetime, @username varchar(15)
    3. AS
    4.  
    5. declare @TableName varchar(30)
    6. declare @CreateTable varchar(1000)
    7. declare @InsertScript varchar(1000)
    8. declare @QueryScript varchar(500)
    9.  
    10. select @TableName = '##tempDFListing_' + @username + ''
    11. select @CreateTable = 'create table ' + @TableName + '
    12. (charge_id uniqueidentifier, employee_nr char(10), caregiver_lname char(30), caregiver_fname char(30), caregiver_job_type char(30),
    13. upi char(8), lname char(30), fname char(30), mname char(30), admission_date datetime, admission_type char(3), visit_type char(30),
    14. item_code char(20), item_desc char(200), quantity int, uom char(20), unit_price money, total_amt money, charge_date datetime, invoiced_date datetime,
    15. paid char(1))'
    16.  
    17. select @InsertScript = 'insert into ' + @TableName + ' select a.charge_id, a.employee_nr, a.caregiver_lname, a.caregiver_fname, a.caregiver_job_type,
    18. a.upi, a.lname, a.fname, a.mname, a.admission_date, a.admission_type, a.visit_type, b.item_code, b.item_desc, b.quantity, b.uom, b.unit_price, b.total_amt, b.charge_date, b.invoiced_date, b.paid
    19. from charge_caregiver a, charge_detail b
    20. where a.charge_id = b.charge_id and b.charge_date between  [B]' + @datetime1 + ' and ' + @datetime2 + '[/B] order by a.caregiver_lname, a.caregiver_fname, a.caregiver_job_type'
    21.  
    22. exec(@CreateTable)
    23. exec(@InsertScript)
    24. GO

    The error I'm receiving is

    VB Code:
    1. Server: Msg 241, Level 16, State 1, Procedure sp_DFListing_ByDate, Line 17
    2. Syntax error converting datetime from character string.

    Thank you very much. Again, Im very sorry to ask this. I just need a quick answer. Thanks
    C++ Programming is overwhelming.

    Dont let it overwhelm you or you'll fall into the oblivion of its perfection

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Using ' on Stored Procedure Problem

    You need to use some kind of CONVERT style to make the DATETIME variables become string variables.

    Try:

    Code:
    between  ' + Convert(varchar(10),@datetime1,101)
                + ' and ' + Convert(varchar(10),@datetime2,101) + ' order
    style 101 is MM/DD/YYYY - this should work - otherwise check BOOKS ONLINE for other style values.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

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



Click Here to Expand Forum to Full Width