Click here to Skip to main content
15,910,886 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to pull data from tables called INVOICE and CUSTOMER. The code is

SQL
Create Function MonthOrders (@InvoiceDate datetime)
Returns
    @MonthOrders Table
(
    InvoiceNumber varchar(20),
    InvoiceDate varchar(50),
    CustomerCode varchar(20),
    Customer varchar(30),
    CustomerPhone varchar(12)
)
As
Begin
    While DatePart(mm,'INV_DATE')+DatePart(yyyy,'INV_DATE') = DatePart(mm,@InvoiceDate)+DatePart(yyyy,@InvoiceDate)
        Insert Into @MonthOrders
            Select INV_NUMBER
                ,DatePart(dw,INV_DATE)+ ',' + DatePart(mm,INV_DATE) + DatePart(d,INV_DATE) + ',' + DatePart(yyyy,INV_DATE)
                ,CUSTOMER.CUS_CODE
                ,dbo.FullName(CUS_FNAME, CUS_INITIAL,CUS_LNAME)
                ,CUS_PHONE + '-' + CUS_PHONE
            From INVOICE Left Join CUSTOMER on INVOICE.CUS_CODE = CUSTOMER.CUS_CODE

    Return
End

When I provide the following...
SQL
Select 'Orders for January 2006' as Test, *
From dbo.MonthOrders('1-1-2006') ;

I get a date conversion error message...
Conversion failed when converting date and/or time from character string.

What am I doing wrong???
Posted
Updated 13-Nov-10 4:28am
v2

I don't do much with dates in SQL so this is just a guess.

MonthOrders is expecting a datetime, so why not give it one?

SQL
Select 'Orders for January 2006' as Test, *
From dbo.MonthOrders(convert(datetime, '1-1-2006')) ;
 
Share this answer
 
SQL Server prefers dates in year-month-day order. Try

SQL
Select 'Orders for January 2006' as Test, *
From dbo.MonthOrders('2006-1-1') ;
 
Share this answer
 
I am stuck with the input as is.
 
Share this answer
 
Changed @InvoiceDate to date from datetime, same results.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900