SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Syntax error converting datetime from character string while calling a function


Syntax error converting datetime from character string while calling a function

Author
Message
Neetu Sharma
Neetu Sharma
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 10
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
from allstat 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 Sad


Kindest Regards,

Neetu Sharma
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40112 Visits: 20000
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
Exploring Recursive CTEs by Example Dwain Camps
Neetu Sharma
Neetu Sharma
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 10
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
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40112 Visits: 20000
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
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search