Date Parameters Open Query SQL - ERROR: Unclosed quotation mark after the character string - parameter-passing

Could someone look at my code below and let me know where I am going wrong. Trying to pass date parameters through Open Query & getting error - Unclosed quotation mark after the character string
--#PID varchar(11),
#START datetime,
#END datetime
AS BEGIN
SET NOCOUNT ON;
--DECLARE #PID1 varchar(11) = #PID
DECLARE #START1 datetime = #START
DECLARE #END1 datetime = #END
DECLARE #TSQL varchar(8000)
SET #TSQL = 'SELECT * FROM OPENQUERY ("CWSLIVE", ''SELECT * FROM pricing_base_data
WHERE date >= convert(date, ''''' + convert(varchar, #START1, 23)+ ''''', 23)
and date < convert(date, ''''' + convert(varchar, #END1, 23)+ ''''', 23))'
--PRINT (#TSQL)
EXEC (#TSQL)
END
GO

Use your query like this:- double quotes both side:
"SELECT * FROM OPENQUERY ("CWSLIVE", ''SELECT * FROM pricing_base_data
WHERE date >= convert(date, ''''' + convert(varchar, #START1, 23)+ ''''', 23) and date < convert(date, ''''' + convert(varchar, #END1, 23)+ ''''', 23))"

Related

Displaying SQL Data For Same Day

I need to be able to display information where the search parameters show same day. I thought this query would cut the mustard, however it presents an error of
Msg 241, Level 16, State 1, Line 13
Conversion failed when converting date and/or time from character string.
And this is my syntax, what should I do to remove the error?
DECLARE
#startDate datetime,
#endDate datetime
SET #startDate = '06/11/2015'
SET #endDate = '06/11/2015'
Set #startDate = convert(varchar(100),#startDate, 101) + ' 00:00:00 AM'
Set #endDate = convert(varchar(100),#endDate, 101) + ' 23:59:59 PM'
select 'Artay' As [Bank Name]
,COUNT(Transaction) As LC
from dbo.financialtransactions
where cleardate between ''' + #startDate + ''' and ''' + #endDate + '''
Change the last line to be
where cleardate between #startDate and #endDate
Your entire script should look like below, again Transaction is a reserve word and so make sure to escape it using []
DECLARE
#startDate datetime,
#endDate datetime
SET #startDate = '06/11/2015'
SET #endDate = '06/11/2015'
Set #startDate = convert(varchar(100),#startDate, 101) + ' 00:00:00 AM'
Set #endDate = convert(varchar(100),#endDate, 101) + ' 23:59:59 PM'
select 'Artay' As [Bank Name]
,COUNT([Transaction]) As LC
from dbo.financialtransactions
where cleardate between #startDate and #endDate

SSRS not returning any data with Table-valued parameters

I'm struggling understanding why adding one or more nullable parameters before or after my table-valued parameters will return an empty recordset.
I successfully followed the example here: http://geekswithblogs.net/GruffCode/archive/2012/06/21/using-table-valued-parameters-with-sql-server-reporting-services.aspx
Then I added an extra parameters to mimic my environment and I cannot get any more records unless I replace #accountType=NULL with #accountType=4
Here is the code in the dataset:
exec( ' declare #customerIdList Report.IntegerListTableType ' + #customerIdInserts +
' EXEC rpt_CustomerTransactionSummary
#startDate=''' + #startDate + ''',
#endDate=''' + #endDate + ''',
#customerIds = #customerIdList')
and this is the sql trace (result: empty recordset):
exec sp_executesql N'exec( '' declare #customerIdList Report.IntegerListTableType '' + #customerIdInserts +
'' EXEC rpt_CustomerTransactionSummary
#startDate='''''' + #startDate + '''''',
#endDate='''''' + #endDate + '''''',
#accountType='''''' + #accountType + '''''',
#customerIds = #customerIdList'')',
N'#customerIdInserts nvarchar(40),
#startDate datetime,
#endDate datetime,
#accountType int',
#customerIdInserts=N'INSERT #customerIdList VALUES (304813)',
#startDate='2013-01-01 00:00:00',
#endDate='2014-12-31 00:00:00',
#accountType=NULL
if I replace the NULL with a 4 and run the query in sql management studio I get the expected results:
exec sp_executesql N'exec( '' declare #customerIdList Report.IntegerListTableType '' + #customerIdInserts +
'' EXEC rpt_CustomerTransactionSummary
#startDate='''''' + #startDate + '''''',
#endDate='''''' + #endDate + '''''',
#accountType='''''' + #accountType + '''''',
#customerIds = #customerIdList'')',
N'#customerIdInserts nvarchar(40),
#startDate datetime,
#endDate datetime,
#accountType int',
#customerIdInserts=N'INSERT #customerIdList VALUES (304813)',
#startDate='2013-01-01 00:00:00',
#endDate='2014-12-31 00:00:00',
#accountType=4
Can anyone please set me to the right direction?
Thanks
rick
this is my stored proc:
ALTER PROCEDURE dbo.rpt_CustomerTransactionSummary
(
#startDate datetime,
#endDate datetime,
#customerIds Report.IntegerListTableType READONLY,
#accountType int
)
AS
BEGIN
SET NOCOUNT ON;
SELECT t.PlayerID, t.InsertedDatetime, t.Amount
FROM transactions t
INNER JOIN #customerIds AS c on t.PlayerID = c.Value
WHERE InsertedDatetime BETWEEN #startDate AND #endDate
AND (#accountType IS NULL OR t.AccountType=#accountType)
END
I have modified my answer after you supplied the stored proc. I think accounttype will be 0 instead of null, since it is int datatype.
Try changing this:
AND (accountType= #accountType OR #accountType = 0)
Robert your solution worked however it made us cringe to add that to the already ugly hack. We solved the issue by fixing the "Text" Query type
From
exec( ' declare #customerIdList Report.IntegerListTableType ' + #customerIdInserts +
' EXEC rpt_CustomerTransactionSummary
#startDate=''' + #startDate + ''',
#endDate=''' + #endDate + ''',
#accountType=''' + #accountType + ''',
#customerIds = #customerIdList')
to
DECLARE #vSQL nvarchar(max) = N'
DECLARE #customerIdList Report.IntegerListTableType;
'+#customerIdInserts+';
EXEC rpt_CustomerTransactionSummary
#startDate=#startDate,
#endDate=#endDate,
#accountType=#accountType,
#customerIds = #customerIdList';
exec sp_executesql
#vSQL,
N'#startDate datetime, #endDate datetime, #accountType int',
#startDate,
#endDate,
#accountType;

pulling variable in the select statement

I am trying to Pull #variable in select statement.
ex:
DECLARE #STARTDATE DATE
SELECT #STARTDATE = '8/1/2013'
DECLARE #ENDDATE DATE
SELECT #STARTDATE = '8/31/2013'
SELECT 'Results for DOS Serving' + #STARTDATE + 'to' + #EndDate
Any help please?
Thanks.
You can't just add string data and dates. You have to convert your dates to a string datatype first. (Also, in your code you never set #ENDDATE.
Something like this should get you close.
DECLARE #STARTDATE DATE
SELECT #STARTDATE = '8/1/2013'
DECLARE #ENDDATE DATE
SELECT #ENDDATE = '8/31/2013'
SELECT 'Results for DOS Serving ' + CONVERT(varchar, #STARTDATE, 110) + ' to ' + CONVERT(varchar, #EndDate, 110)
DECLARE #STARTDATE DATE
SET #STARTDATE = '8/1/2013'
DECLARE #ENDDATE DATE
SET #ENDDATE = '8/31/2013'
SELECT 'Results for DOS Serving ' + CONVERT(varchar(20), #STARTDATE, 110) + ' to ' + CONVERT(varchar(20), #EndDate, 110)

Date Wise Report in SQL server 2008

How to get following output ?
[1],[2],[3],[4],[5],[6],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17]
,[18],[19],[20]
,[21],[22],[23],[24],[25],[26],[27],[28],[29],[30]
Need to fetch all dates from given month in sql server
I want to display daily Date Wise report
Example :
If I pass date as '11/01/2012' then it should return above result
and if I pass December than 31 days.
Very unclear question but try
DECLARE #epoch DATETIME = '20130101'
;WITH cte AS
(
SELECT #epoch DateKey
UNION ALL
SELECT DATEADD(D, 1, DateKey)
FROM cte
WHERE MONTH(DATEADD(D, 1, DateKey))=MONTH(#epoch)
)
SELECT * FROM cte
try below code
declare #date datetime = '09/10/2012'
declare #noofdays int
select #noofdays =datediff(day, #date, dateadd(month, 1, #date))
DECLARE #startDate DATETIME=CAST(MONTH(#date) AS VARCHAR) + '/' + '01/' + + CAST(YEAR(#date) AS VARCHAR) -- mm/dd/yyyy
DECLARE #endDate DATETIME= CAST(MONTH(#date) AS VARCHAR) + '/' + cast(#noofdays as varchar) + '/' + CAST(YEAR(#date) AS VARCHAR)
;WITH Calender AS
(
SELECT #startDate AS CalanderDate
UNION ALL
SELECT CalanderDate + 1 FROM Calender
WHERE CalanderDate + 1 <= #endDate
)
SELECT [Date] = CONVERT(VARCHAR(10),CalanderDate,25)
FROM Calender
OPTION (MAXRECURSION 0)
I am able to find out answer my self....
Thanks Buddy for trying to help me...
DECLARE #COLSPIVOT AS NVARCHAR(MAX)=''
declare #MaxDate int
set #MaxDate=(SELECT day(DATEADD(ms,-2,DATEADD(MONTH, DATEDIFF(MONTH,0,'8/1/2013')+1,0)))
AS [Current Month])
declare #i int =1
while (#i<=#MaxDate)
begin
set #COLSPIVOT=#COLSPIVOT+'['+convert(varchar(10),#i)+']'
if(#i!=#MaxDate)
begin
set #COLSPIVOT=#COLSPIVOT+','
end
set #i=#i+1
end
select #COLSPIVOT

Stripping the time out of datetime SQL Server 2005

I think I'm going crazy here.
I am trying to do a SP on SQL Server 2005 which takes a date, looks it up in another table and assigns a period number and week number (for financial calendar purposes).
The output needs to hold the date as a string, rather than datetime.
I simply CANNOT get the date to show without the time behind it.
I have tried a couple of methods:
select cast(floor(cast(#CalcDate as float)) as datetime)
I have made a function which converts it into a string and puts it back out again:
CREATE FUNCTION dbo.DateToVarchar (#DateIn datetime)
RETURNS varchar(10)
AS
BEGIN
declare #DD [varchar] (2)
declare #MM [varchar] (2)
declare #YYYY [varchar] (4)
declare #DateOut [varchar] (10)
declare #DDLen [int]
declare #MMLen [int]
set #DD = datepart(dd,#DateIn)
set #DDLen = len(#DD)
set #DD = (case when #DDLen < 2 then '0' + #DD else #DD end)
set #MM = datepart(mm,#DateIn)
set #MMLen = len(#MM)
set #MM = (case when #MMLen < 2 then '0' + #MM else #MM end)
set #YYYY = datepart(yyyy,#DateIn)
set #DateOut = #YYYY + '-' + #MM + '-' + #DD
return #DateOut
END
When I run the above function outside of the SP I get the date back just fine. When I run it through the SP it comes back as 2012-12-30 00:00:00.000
The variable #CalcDate is declared as a varchar(10)
Any ideas?
Thanks in advance
EDIT
Here is the body of the SP so far:
declare #StartDate [datetime]
, #EndDate [Datetime]
, #CalcDate [datetime] --This number will change in the WHILE loop to reflect the increment of days
, #Week [varchar]
, #Period [varchar]
, #i [int]
, #Year [int]
, #CalcDay [int]
, #CalcMonth [int]
, #CalcYear [int]
, #ConcatDate [char] (10)
set #StartDate = '2012-12-30'
set #EndDate = '2013-01-28'
set #Year = 2013
set #i = -1
-- Going to do a while loop here and instead of Week number and Period Number I'm going to do some calculations
set #Week = (Select WeekNum from aaGreensPeriodListTest Where PeriodNum = 1 and WeekNum = 1)
set #Period = (Select PeriodNum from aaGreensPeriodListTest Where PeriodNum = 1 and WeekNum = 1)
set #CalcDate = #StartDate + #i
set #CalcMonth = datepart(mm,#calcdate)
insert into aaGreensPeriodTest(RealDate,GreensYear,GreensPeriod,GreensWeek)
values (#CalcDate,#Year,#Period,#Week)
select #CalcDate as CalcDate, #CalcMonth as CalcMonth
SQL Server 2005 does not support date type without time part.
If your need to get date as a string you can use convert function. For example:
declare #date datetime;
set #date = getdate();
select convert(varchar, #date, 101) -- that will return date in ‘mm/dd/yyyy’ format
If you need a datetime variable without time part for some calculations, you can use an expression below:
declare #date datetime;
set #date = getdate();
select dateadd(dd, datediff(dd, 0, #date), 0)
As per http://msdn.microsoft.com/en-ca/library/ms187928.aspx, you cannot cast float->date directly. It's simply not supporte. But you can cast a datetime->date, so you'll have to double-cast. Or given your example, triple-cast:
CAST(CAST(CAST(#CalcDate AS float) AS datetime) AS date)