Displaying SQL Data For Same Day - sql-server-2008

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

Related

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

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))"

How to do condition in SQL Server stored procedure

I have a stored procedure in which I need to implement a conditional WHERE clause.
I have a #name parameter, #startdate and #enddate parameters.
How can I write conditions:
if #name is null and #startdate & #enddate is given then return data for date range.
if #name is given and #startdate & #enddate is null then return data for the name
if #name and #startdate & #enddate is given then return data where name and date range are evaluated
Here is my current procedure
alter procedure dbo.sp_emp
#name varchar(50),
#startdate date,
#enddate date
as
begin
select *
from employee
where
Name = #name
and dob between #startdate and #enddate
end
I think COALESCE solves your problem.
alter procedure dbo.sp_emp
#name varchar(50),
#startdate date,
#enddate date
as
begin
select *
from employee
where Name = (COALESCE(#name, name))
AND dob between COALESCE(#startdate, dob) and COALESCE(#enddate, dob)
end
ISNULL version
alter procedure dbo.sp_emp
#name varchar(50),
#startdate date,
#enddate date
as
begin
select *
FROM employee
WHERE Name = ISNULL(#name, name)
AND dob between ISNULL(#startdate, dob) AND ISNULL(#enddate, dob)
end
You can do it this way:
DECLARE #query NVARCHAR(2048);
SET #query = 'select * from employee where Name = ''' + #name + '''';
IF #startdate IS NOT NULL AND #enddate IS NOT NULL
SET #query += ' AND dob between ''' + #startdate + ''' and ''' + #enddate '''';
EXECUTE(#query);
You should take care yours vars about SQL Injection.

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