Syntax error converting datetime from character string while calling a function

  • Hi Friends,

    I am trying to call a function (which returns month value when passing date as parameter ) in another query but it is resulting into

    Syntax error converting datetime from character string.

    select

    (CASE WHEN (sht.mode='SI' Or sht.mode='AI') THEN (y3.[M_NAD_IND])

    ELSE (y1.[M_NAD_IND])

    end)as [Maxdate_Nad_IND],

    max(b.shpdate) as [MAX_shpDate],

    count(shipment)as Shp_Count

    into #maxDate_swift

    fromallstat b

    Left Join nad_new y1 on y1.adrno=b.Shipper

    Left Join nad_new y3 on y3.adrno=b.consignee

    left join shipmenttype sht on b.[type]=sht.[type]

    where b.shpdate between (DATEADD(m, -6, @year + '-' + + '-01'))

    and (DATEADD(dd, -1, @year + '-' + 'select * from mon_allstat('+ b.shpdate + ')' + '-01'))

    group by (CASE WHEN (sht.mode='SI' Or sht.mode='AI') THEN (y3.[M_NAD_IND])

    ELSE (y1.[M_NAD_IND])

    end)

    --@month

    GO

    SET QUOTED_IDENTIFIER OFF

    GO

    SET ANSI_NULLS ON

    GO

    Pls. help 🙁


    Kindest Regards,

    Neetu Sharma

  • DATEADD(dd, -1, @year + '-' + 'select * from mon_allstat('+ b.shpdate + ')' + '-01')

    This isn't valid syntax.

    Can you post the structure of function mon_allstat()?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Hi

    create FUNCTION mon_allstat(@sdate datetime)

    RETURNS TABLE

    AS

    RETURN

    (

    SELECT distinct month(shpdate)as ddate from allstat where

    [allstat].shpDate=@sdate

    )


    Kindest Regards,

    Neetu Sharma

  • Thanks. Resolving the expensive iTVF results in this:

    DATEADD(dd, -1, @year + '-' + MONTH(b.shpdate) + '-01')

    Can you post the code for populating the variable @year?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply