alter the store procedure Programtically using c# [duplicate] - c#

I could not find the solution which gives first and last day of previous month with timestamp.
Here is the solution.
SELECT DATEADD(month, DATEDIFF(month, -1, getdate()) - 2, 0) as FirtDayPreviousMonthWithTimeStamp,
DATEADD(ss, -1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0)) as LastDayPreviousMonthWithTimeStamp
This will return the following if currentdate = '2012-7-31'
result: 2012-06-01 00:00:00.000 2012-06-30 23:59:59.000
This will return the following if currentdate = '2012-1-1'
result: 2011-12-01 00:00:00.000 2011-12-31 23:59:59.000

select DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) --First day of previous month
select DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1) --Last Day of previous month

First Day Of Current Week.
select CONVERT(varchar,dateadd(week,datediff(week,0,getdate()),0),106)
Last Day Of Current Week.
select CONVERT(varchar,dateadd(week,datediff(week,0,getdate()),6),106)
First Day Of Last week.
select CONVERT(varchar,DATEADD(week,datediff(week,7,getdate()),0),106)
Last Day Of Last Week.
select CONVERT(varchar,dateadd(week,datediff(week,7,getdate()),6),106)
First Day Of Next Week.
select CONVERT(varchar,dateadd(week,datediff(week,0,getdate()),7),106)
Last Day Of Next Week.
select CONVERT(varchar,dateadd(week,datediff(week,0,getdate()),13),106)
First Day Of Current Month.
select CONVERT(varchar,dateadd(d,-(day(getdate()-1)),getdate()),106)
Last Day Of Current Month.
select CONVERT(varchar,dateadd(d,-(day(dateadd(m,1,getdate()))),dateadd(m,1,getdate())),106)
In this Example Works on Only date is 31. and remaining days are not.
First Day Of Last Month.
select CONVERT(varchar,dateadd(d,-(day(dateadd(m,-1,getdate()-2))),dateadd(m,-1,getdate()-1)),106)
Last Day Of Last Month.
select CONVERT(varchar,dateadd(d,-(day(getdate())),getdate()),106)
First Day Of Next Month.
select CONVERT(varchar,dateadd(d,-(day(dateadd(m,1,getdate()-1))),dateadd(m,1,getdate())),106)
Last Day Of Next Month.
select CONVERT(varchar,dateadd(d,-(day(dateadd(m,2,getdate()))),DATEADD(m,2,getdate())),106)
First Day Of Current Year.
select CONVERT(varchar,dateadd(year,datediff(year,0,getdate()),0),106)
Last Day Of Current Year.
select CONVERT(varchar,dateadd(ms,-2,dateadd(year,0,dateadd(year,datediff(year,0,getdate())+1,0))),106)
First Day of Last Year.
select CONVERT(varchar,dateadd(year,datediff(year,0,getdate())-1,0),106)
Last Day Of Last Year.
select CONVERT(varchar,dateadd(ms,-2,dateadd(year,0,dateadd(year,datediff(year,0,getdate()),0))),106)
First Day Of Next Year.
select CONVERT(varchar,dateadd(YEAR,DATEDIFF(year,0,getdate())+1,0),106)
Last Day Of Next Year.
select CONVERT(varchar,dateadd(ms,-2,dateadd(year,0,dateadd(year,datediff(year,0,getdate())+2,0))),106)

SELECT CONVERT(DATE,DATEADD(MM, DATEDIFF(MM, 0, GETDATE())-1, 0)) AS FirstDayOfPrevMonth
SELECT CONVERT(DATE,DATEADD(MS, -3, DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) , 0))) AS LastDayOfPrevMonth

Solution
The date format that you requested is called ODBC format (code 120).
To actually calculate the values that you requested, include the following in your SQL.
Copy, paste...
DECLARE
#FirstDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, -1, GETDATE() - 2)) ), DATEADD(m, -1, GETDATE() - 1)))
, #LastDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(GETDATE()) ), GETDATE()))
...and use in your code:
#FirstDayOfLastMonth
#LastDayOfLastMonth
Be aware that it has to be pasted earlier than any statements that reference the parameters, but from that point on you can reference #FirstDayOfLastMonth and #LastDayOfLastMonth in your code.
Example
Let's see some code in action:
DECLARE
#FirstDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, -1, GETDATE() - 2)) ), DATEADD(m, -1, GETDATE() - 1)))
, #LastDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(GETDATE()) ), GETDATE()))
SELECT
'First day of last month' AS Title, CONVERT(VARCHAR, #FirstDayOfLastMonth , 120) AS [ODBC]
UNION
SELECT
'Last day of last month' AS Title, CONVERT(VARCHAR, #LastDayOfLastMonth , 120) AS [ODBC]
Run the above code to produce the following output:
Note: Bear in mind that today's date for me is 12th September, 2016.
More (for completeness' sake)
Common date parameters
Are you left wanting more?
To set up a more comprehensive range of handy date related parameters, include the following in your SQL:
DECLARE
#FirstDayOfCurrentWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 0))
, #LastDayOfCurrentWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 6))
, #FirstDayOfLastWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 7, GETDATE()), 0))
, #LastDayOfLastWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 7, GETDATE()), 6))
, #FirstDayOfNextWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 7))
, #LastDayOfNextWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 13))
, #FirstDayOfCurrentMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(GETDATE() - 1) ), GETDATE()))
, #LastDayOfCurrentMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, 1, GETDATE())) ), DATEADD(m, 1, GETDATE())))
, #FirstDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, -1, GETDATE() - 2)) ), DATEADD(m, -1, GETDATE() - 1)))
, #LastDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(GETDATE()) ), GETDATE()))
, #FirstDayOfNextMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, 1, GETDATE() - 1)) ), DATEADD(m, 1, GETDATE())))
, #LastDayOfNextMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, 2, GETDATE())) ), DATEADD(m, 2, GETDATE())))
, #FirstDayOfCurrentYear DATETIME = CONVERT(DATE, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0))
, #LastDayOfCurrentYear DATETIME = CONVERT(DATE, DATEADD(ms, -2, DATEADD(YEAR, 0, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0))))
, #FirstDayOfLastYear DATETIME = CONVERT(DATE, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) - 1, 0))
, #LastDayOfLastYear DATETIME = CONVERT(DATE, DATEADD(ms, -2, DATEADD(YEAR, 0, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0))))
, #FirstDayOfNextYear DATETIME = CONVERT(DATE, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0))
, #LastDayOfNextYear DATETIME = CONVERT(DATE, DATEADD(ms, -2, DATEADD(YEAR, 0, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 2, 0))))
It would make most sense to include it earlier on, preferably at the top of your procedure or SQL query.
Once declared, the parameters can be referenced anywhere in your code, as many times as you need them.
Example
Let's see some code in action:
DECLARE
#FirstDayOfCurrentWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 0))
, #LastDayOfCurrentWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 6))
, #FirstDayOfLastWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 7, GETDATE()), 0))
, #LastDayOfLastWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 7, GETDATE()), 6))
, #FirstDayOfNextWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 7))
, #LastDayOfNextWeek DATETIME = CONVERT(DATE, DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 13))
, #FirstDayOfCurrentMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(GETDATE() - 1) ), GETDATE()))
, #LastDayOfCurrentMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, 1, GETDATE())) ), DATEADD(m, 1, GETDATE())))
, #FirstDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, -1, GETDATE() - 2)) ), DATEADD(m, -1, GETDATE() - 1)))
, #LastDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(GETDATE()) ), GETDATE()))
, #FirstDayOfNextMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, 1, GETDATE() - 1)) ), DATEADD(m, 1, GETDATE())))
, #LastDayOfNextMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, 2, GETDATE())) ), DATEADD(m, 2, GETDATE())))
, #FirstDayOfCurrentYear DATETIME = CONVERT(DATE, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0))
, #LastDayOfCurrentYear DATETIME = CONVERT(DATE, DATEADD(ms, -2, DATEADD(YEAR, 0, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0))))
, #FirstDayOfLastYear DATETIME = CONVERT(DATE, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) - 1, 0))
, #LastDayOfLastYear DATETIME = CONVERT(DATE, DATEADD(ms, -2, DATEADD(YEAR, 0, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0))))
, #FirstDayOfNextYear DATETIME = CONVERT(DATE, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0))
, #LastDayOfNextYear DATETIME = CONVERT(DATE, DATEADD(ms, -2, DATEADD(YEAR, 0, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 2, 0))))
SELECT
'a) FirstDayOfCurrentWeek.' AS [Title] ,
#FirstDayOfCurrentWeek AS [DATE (Server default)] ,
CONVERT(VARCHAR, #FirstDayOfCurrentWeek, 127) AS [ISO8601] ,
CONVERT(VARCHAR, #FirstDayOfCurrentWeek, 103) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [British&French] ,
CONVERT(VARCHAR, #FirstDayOfCurrentWeek, 104) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [German] ,
CONVERT(VARCHAR, #FirstDayOfCurrentWeek, 105) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Italian] ,
CONVERT(VARCHAR, #FirstDayOfCurrentWeek, 111) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Japan] ,
CONVERT(VARCHAR, #FirstDayOfCurrentWeek, 100) AS [U.S.] ,
CONVERT(VARCHAR, #FirstDayOfCurrentWeek, 120) AS [ODBC]
UNION
SELECT
'b) LastDayOfCurrentWeek.' AS [Title] ,
#LastDayOfCurrentWeek AS [DATE (Server default)] ,
CONVERT(VARCHAR, #LastDayOfCurrentWeek, 127) AS [ISO8601] ,
CONVERT(VARCHAR, #LastDayOfCurrentWeek, 103) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [British&French] ,
CONVERT(VARCHAR, #LastDayOfCurrentWeek, 104) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [German] ,
CONVERT(VARCHAR, #LastDayOfCurrentWeek, 105) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Italian] ,
CONVERT(VARCHAR, #LastDayOfCurrentWeek, 111) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Japan] ,
CONVERT(VARCHAR, #LastDayOfCurrentWeek, 100) AS [U.S.] ,
CONVERT(VARCHAR, #LastDayOfCurrentWeek, 120) AS [ODBC]
UNION
SELECT
'c) FirstDayOfLastWeek.' AS [Title] ,
#FirstDayOfLastWeek AS [DATE (Server default)] ,
CONVERT(VARCHAR, #FirstDayOfLastWeek, 127) AS [ISO8601] ,
CONVERT(VARCHAR, #FirstDayOfLastWeek, 103) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [British&French] ,
CONVERT(VARCHAR, #FirstDayOfLastWeek, 104) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [German] ,
CONVERT(VARCHAR, #FirstDayOfLastWeek, 105) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Italian] ,
CONVERT(VARCHAR, #FirstDayOfLastWeek, 111) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Japan] ,
CONVERT(VARCHAR, #FirstDayOfLastWeek, 100) AS [U.S.] ,
CONVERT(VARCHAR, #FirstDayOfLastWeek, 120) AS [ODBC]
UNION
SELECT
'd) LastDayOfLastWeek.' AS [Title] ,
#LastDayOfLastWeek AS [DATE (Server default)] ,
CONVERT(VARCHAR, #LastDayOfLastWeek, 127) AS [ISO8601] ,
CONVERT(VARCHAR, #LastDayOfLastWeek, 103) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [British&French] ,
CONVERT(VARCHAR, #LastDayOfLastWeek, 104) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [German] ,
CONVERT(VARCHAR, #LastDayOfLastWeek, 105) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Italian] ,
CONVERT(VARCHAR, #LastDayOfLastWeek, 111) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Japan] ,
CONVERT(VARCHAR, #LastDayOfLastWeek, 100) AS [U.S.] ,
CONVERT(VARCHAR, #LastDayOfLastWeek, 120) AS [ODBC]
UNION
SELECT
'e) FirstDayOfNextWeek.' AS [Title] ,
#FirstDayOfNextWeek AS [DATE (Server default)] ,
CONVERT(VARCHAR, #FirstDayOfNextWeek, 127) AS [ISO8601] ,
CONVERT(VARCHAR, #FirstDayOfNextWeek, 103) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [British&French] ,
CONVERT(VARCHAR, #FirstDayOfNextWeek, 104) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [German] ,
CONVERT(VARCHAR, #FirstDayOfNextWeek, 105) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Italian] ,
CONVERT(VARCHAR, #FirstDayOfNextWeek, 111) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Japan] ,
CONVERT(VARCHAR, #FirstDayOfNextWeek, 100) AS [U.S.] ,
CONVERT(VARCHAR, #FirstDayOfNextWeek, 120) AS [ODBC]
UNION
SELECT
'f) LastDayOfNextWeek.' AS [Title] ,
#LastDayOfNextWeek AS [DATE (Server default)] ,
CONVERT(VARCHAR, #LastDayOfNextWeek, 127) AS [ISO8601] ,
CONVERT(VARCHAR, #LastDayOfNextWeek, 103) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [British&French] ,
CONVERT(VARCHAR, #LastDayOfNextWeek, 104) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [German] ,
CONVERT(VARCHAR, #LastDayOfNextWeek, 105) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Italian] ,
CONVERT(VARCHAR, #LastDayOfNextWeek, 111) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Japan] ,
CONVERT(VARCHAR, #LastDayOfNextWeek, 100) AS [U.S.] ,
CONVERT(VARCHAR, #LastDayOfNextWeek, 120) AS [ODBC]
UNION
SELECT
'g) FirstDayOfCurrentMonth.' AS [Title] ,
#FirstDayOfCurrentMonth AS [DATE (Server default)] ,
CONVERT(VARCHAR, #FirstDayOfCurrentMonth, 127) AS [ISO8601] ,
CONVERT(VARCHAR, #FirstDayOfCurrentMonth, 103) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [British&French] ,
CONVERT(VARCHAR, #FirstDayOfCurrentMonth, 104) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [German] ,
CONVERT(VARCHAR, #FirstDayOfCurrentMonth, 105) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Italian] ,
CONVERT(VARCHAR, #FirstDayOfCurrentMonth, 111) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Japan] ,
CONVERT(VARCHAR, #FirstDayOfCurrentMonth, 100) AS [U.S.] ,
CONVERT(VARCHAR, #FirstDayOfCurrentMonth, 120) AS [ODBC]
UNION
SELECT
'h) LastDayOfCurrentMonth.' AS [Title] ,
#LastDayOfCurrentMonth AS [DATE (Server default)] ,
CONVERT(VARCHAR, #LastDayOfCurrentMonth, 127) AS [ISO8601] ,
CONVERT(VARCHAR, #LastDayOfCurrentMonth, 103) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [British&French] ,
CONVERT(VARCHAR, #LastDayOfCurrentMonth, 104) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [German] ,
CONVERT(VARCHAR, #LastDayOfCurrentMonth, 105) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Italian] ,
CONVERT(VARCHAR, #LastDayOfCurrentMonth, 111) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Japan] ,
CONVERT(VARCHAR, #LastDayOfCurrentMonth, 100) AS [U.S.] ,
CONVERT(VARCHAR, #LastDayOfCurrentMonth, 120) AS [ODBC]
UNION
SELECT
'i) FirstDayOfLastMonth.' AS [Title] ,
#FirstDayOfLastMonth AS [DATE (Server default)] ,
CONVERT(VARCHAR, #FirstDayOfLastMonth, 127) AS [ISO8601] ,
CONVERT(VARCHAR, #FirstDayOfLastMonth, 103) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [British&French] ,
CONVERT(VARCHAR, #FirstDayOfLastMonth, 104) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [German] ,
CONVERT(VARCHAR, #FirstDayOfLastMonth, 105) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Italian] ,
CONVERT(VARCHAR, #FirstDayOfLastMonth, 111) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Japan] ,
CONVERT(VARCHAR, #FirstDayOfLastMonth, 100) AS [U.S.] ,
CONVERT(VARCHAR, #FirstDayOfLastMonth, 120) AS [ODBC]
UNION
SELECT
'j) LastDayOfLastMonth.' AS [Title] ,
#LastDayOfLastMonth AS [DATE (Server default)] ,
CONVERT(VARCHAR, #LastDayOfLastMonth, 127) AS [ISO8601] ,
CONVERT(VARCHAR, #LastDayOfLastMonth, 103) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [British&French] ,
CONVERT(VARCHAR, #LastDayOfLastMonth, 104) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [German] ,
CONVERT(VARCHAR, #LastDayOfLastMonth, 105) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Italian] ,
CONVERT(VARCHAR, #LastDayOfLastMonth, 111) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Japan] ,
CONVERT(VARCHAR, #LastDayOfLastMonth, 100) AS [U.S.] ,
CONVERT(VARCHAR, #LastDayOfLastMonth, 120) AS [ODBC]
UNION
SELECT
'k) FirstDayOfNextMonth.' AS [Title] ,
#FirstDayOfNextMonth AS [DATE (Server default)] ,
CONVERT(VARCHAR, #FirstDayOfNextMonth, 127) AS [ISO8601] ,
CONVERT(VARCHAR, #FirstDayOfNextMonth, 103) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [British&French] ,
CONVERT(VARCHAR, #FirstDayOfNextMonth, 104) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [German] ,
CONVERT(VARCHAR, #FirstDayOfNextMonth, 105) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Italian] ,
CONVERT(VARCHAR, #FirstDayOfNextMonth, 111) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Japan] ,
CONVERT(VARCHAR, #FirstDayOfNextMonth, 100) AS [U.S.] ,
CONVERT(VARCHAR, #FirstDayOfNextMonth, 120) AS [ODBC]
UNION
SELECT
'l) LastDayOfNextMonth.' AS [Title] ,
#LastDayOfNextMonth AS [DATE (Server default)] ,
CONVERT(VARCHAR, #LastDayOfNextMonth, 127) AS [ISO8601] ,
CONVERT(VARCHAR, #LastDayOfNextMonth, 103) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [British&French] ,
CONVERT(VARCHAR, #LastDayOfNextMonth, 104) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [German] ,
CONVERT(VARCHAR, #LastDayOfNextMonth, 105) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Italian] ,
CONVERT(VARCHAR, #LastDayOfNextMonth, 111) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Japan] ,
CONVERT(VARCHAR, #LastDayOfNextMonth, 100) AS [U.S.] ,
CONVERT(VARCHAR, #LastDayOfNextMonth, 120) AS [ODBC]
UNION
SELECT
'm) FirstDayOfCurrentYear.' AS [Title] ,
#FirstDayOfCurrentYear AS [DATE (Server default)] ,
CONVERT(VARCHAR, #FirstDayOfCurrentYear, 127) AS [ISO8601] ,
CONVERT(VARCHAR, #FirstDayOfCurrentYear, 103) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [British&French] ,
CONVERT(VARCHAR, #FirstDayOfCurrentYear, 104) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [German] ,
CONVERT(VARCHAR, #FirstDayOfCurrentYear, 105) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Italian] ,
CONVERT(VARCHAR, #FirstDayOfCurrentYear, 111) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Japan] ,
CONVERT(VARCHAR, #FirstDayOfCurrentYear, 100) AS [U.S.] ,
CONVERT(VARCHAR, #FirstDayOfCurrentYear, 120) AS [ODBC]
UNION
SELECT
'n) LastDayOfCurrentYear.' AS [Title] ,
#LastDayOfCurrentYear AS [DATE (Server default)] ,
CONVERT(VARCHAR, #LastDayOfCurrentYear, 127) AS [ISO8601] ,
CONVERT(VARCHAR, #LastDayOfCurrentYear, 103) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [British&French] ,
CONVERT(VARCHAR, #LastDayOfCurrentYear, 104) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [German] ,
CONVERT(VARCHAR, #LastDayOfCurrentYear, 105) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Italian] ,
CONVERT(VARCHAR, #LastDayOfCurrentYear, 111) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Japan] ,
CONVERT(VARCHAR, #LastDayOfCurrentYear, 100) AS [U.S.] ,
CONVERT(VARCHAR, #LastDayOfCurrentYear, 120) AS [ODBC]
UNION
SELECT
'o) FirstDayOfLastYear.' AS [Title] ,
#FirstDayOfLastYear AS [DATE (Server default)] ,
CONVERT(VARCHAR, #FirstDayOfLastYear, 127) AS [ISO8601] ,
CONVERT(VARCHAR, #FirstDayOfLastYear, 103) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [British&French] ,
CONVERT(VARCHAR, #FirstDayOfLastYear, 104) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [German] ,
CONVERT(VARCHAR, #FirstDayOfLastYear, 105) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Italian] ,
CONVERT(VARCHAR, #FirstDayOfLastYear, 111) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Japan] ,
CONVERT(VARCHAR, #FirstDayOfLastYear, 100) AS [U.S.] ,
CONVERT(VARCHAR, #FirstDayOfLastYear, 120) AS [ODBC]
UNION
SELECT
'p) LastDayOfLastYear.' AS [Title] ,
#LastDayOfLastYear AS [DATE (Server default)] ,
CONVERT(VARCHAR, #LastDayOfLastYear, 127) AS [ISO8601] ,
CONVERT(VARCHAR, #LastDayOfLastYear, 103) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [British&French] ,
CONVERT(VARCHAR, #LastDayOfLastYear, 104) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [German] ,
CONVERT(VARCHAR, #LastDayOfLastYear, 105) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Italian] ,
CONVERT(VARCHAR, #LastDayOfLastYear, 111) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Japan] ,
CONVERT(VARCHAR, #LastDayOfLastYear, 100) AS [U.S.] ,
CONVERT(VARCHAR, #LastDayOfLastYear, 120) AS [ODBC]
UNION
SELECT
'q) FirstDayOfNextYear.' AS [Title] ,
#FirstDayOfNextYear AS [DATE (Server default)] ,
CONVERT(VARCHAR, #FirstDayOfNextYear, 127) AS [ISO8601] ,
CONVERT(VARCHAR, #FirstDayOfNextYear, 103) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [British&French] ,
CONVERT(VARCHAR, #FirstDayOfNextYear, 104) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [German] ,
CONVERT(VARCHAR, #FirstDayOfNextYear, 105) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Italian] ,
CONVERT(VARCHAR, #FirstDayOfNextYear, 111) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Japan] ,
CONVERT(VARCHAR, #FirstDayOfNextYear, 100) AS [U.S.] ,
CONVERT(VARCHAR, #FirstDayOfNextYear, 120) AS [ODBC]
UNION
SELECT
'r) LastDayOfNextYear.' AS [Title] ,
#LastDayOfNextYear AS [DATE (Server default)] ,
CONVERT(VARCHAR, #LastDayOfNextYear, 127) AS [ISO8601] ,
CONVERT(VARCHAR, #LastDayOfNextYear, 103) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [British&French] ,
CONVERT(VARCHAR, #LastDayOfNextYear, 104) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [German] ,
CONVERT(VARCHAR, #LastDayOfNextYear, 105) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Italian] ,
CONVERT(VARCHAR, #LastDayOfNextYear, 111) + ' ' + CONVERT(CHAR(5), #FirstDayOfCurrentWeek, 108) AS [Japan] ,
CONVERT(VARCHAR, #LastDayOfNextYear, 100) AS [U.S.] ,
CONVERT(VARCHAR, #LastDayOfNextYear, 120) AS [ODBC];
Run the above code to produce the following output:
If your country is missing, then it is because I don't know the code for it. It would be most helpful and appreciated if you could please edit this answer and add a new column for your country.
Thanks in advance.
Note: Bear in mind that today's date for me is 12th September, 2016.
References
For further reading on the ISO8601 international date standard, follow this link:
https://en.wikipedia.org/wiki/ISO_8601
For further reading on the ODBC international date standard, follow this link:
https://technet.microsoft.com/en-us/library/ms190234(v=sql.90).aspx
To view the list of date formats I worked from, follow this link:
https://msdn.microsoft.com/en-GB/library/ms187928.aspx
For further reading on the DATETIME data type, follow this link:
https://msdn.microsoft.com/en-GB/library/ms187819.aspx

From SQL2012, there is a new function introduced called EOMONTH. Using this function the first and last day of the last month can be easily found.
select DATEADD(DD,1,EOMONTH(Getdate(),-2)) firstdayoflastmonth, EOMONTH(Getdate(), -1) lastdayoflastmonth

SELECT DATEADD(m,DATEDIFF(m,0,GETDATE())-1,0) AS PreviousMonthStart
SELECT DATEADD(ms,-2,DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)) AS PreviousMonthEnd

To get last month's first date:
select DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) LastMonthFirstDate
To get last month's last date:
select DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1) LastMonthEndDate

I have used the following logic in SSRS reports.
BUS_DATE = 17-09-2013
X=DATEADD(MONTH,-1,BUS_DATE) = 17-08-2013
Y=DAY(BUS_DATE)=17
first_date = DATEADD(DAY,-Y+1,X)=01-08-2013
last_date = DATEADD(DAY,-Y,BUS_DATE)=31-08-2013

You can get first and last day of previous month (with timestamp) in SQL Server
by executing
--select dateadd(dd,-datepart(dd,getdate())+1,dateadd(mm,-1,getdate())) --first day of previous month
--select dateadd(dd,-datepart(dd,getdate()),getdate()) -- last day of previous month**

Take some base date which is the 31st of some month e.g. '20011231'. Then use the
following procedure (I have given 3 identical examples below, only the #dt value differs).
declare #dt datetime;
set #dt = '20140312'
SELECT DATEADD(month, DATEDIFF(month, '20011231', #dt), '20011231');
set #dt = '20140208'
SELECT DATEADD(month, DATEDIFF(month, '20011231', #dt), '20011231');
set #dt = '20140405'
SELECT DATEADD(month, DATEDIFF(month, '20011231', #dt), '20011231');

Here's a fairly straight forward and dynamic way. For last month's last day, subtract the current numeric day from today's date. For first day of last month, use the same code, just repeat subtracting the numeric day from the prior result and add 1.
declare #PriorEOM as datetime
declare #PriorFOM as datetime
Set #PriorEOM = dateadd(day,-day(getdate()),getdate())
Set #PriorFOM = dateadd(day,-day(#PriorEOM) + 1,#PriorEOM)
Print #PriorEOM
Print #PriorFOM

I've not seen this solution presented yet; this is my preference for its simpler readability:
select dateadd(month,-1,format(getutcdate(),'yyyy-MM-01'))

Related

Get sum of time column in sql server

What I tried is:
select cast(sum(datediff(second,0,totalhr))/3600 as varchar(12)) + ':' +
right('0' + cast(sum(datediff(second,0,totalhr))/60%60 as varchar(2)),2) +
':' + right('0' + cast(sum(datediff(second,0,totalhr))%60 as varchar(2)),2) as total
FROM checkinout where YEAR(date)=2019 and MONTH(date)=09 and userid=5
It giving wrong output 112:53:04 the right answer should be 116:30:04
If the totalhr column type is time and the format is hh:mm:ss,
For SQL Server: DATEADD (datepart , number , date )
SELECT DATEADD (ms, SUM (DATEDIFF (ms, '00:00:00.000', totalhr)), '00:00:00.000') AS total
FROM checkinout
WHERE
YEAR(date) = 2019 AND
MONTH(date) = 9 AND
userid = 5;
This function adds a specified number value (as a signed integer) to a specified datepart of an input date value, and then returns that modified value.
the above condition will return the calculated Date and Time but if you want just the hh:mm:ss in two digit format you can use:
SELECT FORMAT(hrs, '0#') + ':' + FORMAT(mins, '0#') + ':' + FORMAT(secs, '0#') as total FROM
(SELECT hrs + (((((mins * 60) + (secs - (secs % 60))) / 60) - ((((mins * 60) + (secs - (secs % 60))) / 60) % 60)) / 60) AS hrs,
(((mins * 60) + (secs - (secs % 60))) / 60) % 60 AS mins,
secs % 60 AS secs
FROM (
SELECT SUM(116) AS hrs, // you have to replace the number with your column
SUM(30) AS mins, // you have to replace the number with your column
SUM(04) AS secs // you have to replace the number with your column
) AS dateSplit) AS total
try running below query
SELECT cast(sum(DATEPART(HOUR,totalhr)) as varchar(max)) + ':' +
cast(sum(DATEPART(MINUTE,totalhr)) as varchar(max)) + ':' +
cast(sum(DATEPART(SECOND,totalhr)) as varchar(max)) FROM checkinout

SQL Server : searching for occurrences in different months

I am trying to do a project which uses Visifire to display graphed data. It takes in a single DataTable. I need to create an SQL query that searches a date column for a particular month (format is: Friday, 03 November 2017) and then stores how many times that month is repeated. It has to do this for all 12 months. This is done in C# in visual studio. Currently, it displays no data on the line graph.
public DataTable ReadDataMonthlyBooked()
{
// Declare references (for table, reader and command)
DataTable monthlyBookedTable = new DataTable();
SqlDataReader reader;
SqlCommand command;
string selectString = "SELECT COUNT (BookingNum) AS Bookings "
+ "FROM Booking "
+ "WHERE Booking.EndDate LIKE 'January' "
+ "AND Booking.EndDate LIKE 'February' "
+ "AND Booking.EndDate LIKE 'March' "
+ "AND Booking.EndDate LIKE 'April' "
+ "AND Booking.EndDate LIKE 'May' "
+ "AND Booking.EndDate LIKE 'June' "
+ "AND Booking.EndDate LIKE 'July' "
+ "AND Booking.EndDate LIKE 'August' "
+ "AND Booking.EndDate LIKE 'September' "
+ "AND Booking.EndDate LIKE 'October' "
+ "AND Booking.EndDate LIKE 'November' "
+ "AND Booking.EndDate LIKE 'December'";
try
{
// Create a new command
command = new SqlCommand(selectString, cnMain);
// open the connection
cnMain.Open();
command.CommandType = CommandType.Text;
reader = command.ExecuteReader();
// read data from readerObject and load in table
monthlyBookedTable.Load(reader);
reader.Close(); //close the reader
cnMain.Close(); //close the connection
return monthlyBookedTable;
}
catch (Exception ex)
{
return (null);
}
}
This is the current SQL query I have and I hope its hopelessly wrong with logic errors but I do not know SQL well enough.
select 'Friday, 03 November 2017'
select cast('03 November 2017' as date)
select SUBSTRING('Friday, 03 November 2017', CHARINDEX(',', 'Friday, 03 November 2017') + 2, 100)
select cast(SUBSTRING('Friday, 03 November 2017', CHARINDEX(',', 'Friday, 03 November 2017') + 2, 100) as date)
select datepart(month, cast(SUBSTRING('Friday, 03 November 2017', CHARINDEX(',', 'Friday, 03 November 2017') + 2, 100) as date))
select datepart(month, cast(Booking.EndDate, CHARINDEX(',', Booking.EndDate) + 2, 100) as date))
, COUNT (BookingNum) AS Bookings "
from booking
group by datepart(month, cast(Booking.EndDate, CHARINDEX(',', Booking.EndDate) + 2, 100) as date))
You'll need a group by, like this
SELECT COUNT(BookingNum) as Bookings, MONTH(EndDate) as Month
FROM Booking
GROUP BY MONTH(EndDate)
But as your EndDate is a varchar, you'll have to extract the month
SELECT COUNT(BookingNum) as Bookings
, SUBSTRING(EndDate,
CHARINDEX(',',EndDate) + 4,
CHARINDEX(' ',EndDate, CHARINDEX(',',EndDate) + 5) - CHARINDEX(',',EndDate) + 4)
FROM Booking
GROUP BY SUBSTRING(EndDate,
CHARINDEX(',',EndDate) + 4,
CHARINDEX(' ',EndDate, CHARINDEX(',',EndDate) + 5) - CHARINDEX(',',EndDate) + 4)
There may be a better way to do this (cursors maybe?) but basically you want to get the location of the first ',' and add 4 to get the month (assuming the date is always 2 digits like in your example) and then use that as a start index for a search of the first blank (that'll be the end of the month name) and substract the first index we found to get the length fo the month.
So, basically, just change the date to a DATE field
or cast it
SELECT COUNT(BookingNum) as Bookings, MONTH(CONVERT(DATE, EndDate)) as Month
FROM Booking
GROUP BY MONTH(CONVERT(DATE, EndDate))
But I don't know if the format you are using will be interpreted correctly by the engine.
The error is in your SQL statement.
SELECT
COUNT(BookingNum) AS Bookings
FROM
Booking
WHERE
Booking.EndDate LIKE '%January%',
AND Booking.EndDate LIKE '%February%',
AND Booking.EndDate LIKE '%March%',
AND Booking.EndDate LIKE '%April%',
AND Booking.EndDate LIKE '%May%',
AND Booking.EndDate LIKE '%June%',
AND Booking.EndDate LIKE '%July%',
AND Booking.EndDate LIKE '%August%',
AND Booking.EndDate LIKE '%September%',
AND Booking.EndDate LIKE '%October%',
AND Booking.EndDate LIKE '%November%',
AND Booking.EndDate LIKE '%December%',
You try to get a record where the month is every month in the year at the same time.
If I'm right, you try to get the number of bookings per month.
So it should look something much more like
SELECT
SUM(CASE WHEN Booking.EndDate LIKE '%January%' THEN 1 ELSE 0 END) AS JanuaryBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%February%' THEN 1 ELSE 0 END) AS FebruaryBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%March%' THEN 1 ELSE 0 END) AS MarchBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%April%' THEN 1 ELSE 0 END) AS AprilBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%May%' THEN 1 ELSE 0 END) AS MayBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%June%' THEN 1 ELSE 0 END) AS JuneBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%July%' THEN 1 ELSE 0 END) AS JulyBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%August%' THEN 1 ELSE 0 END) AS AugustBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%September%' THEN 1 ELSE 0 END) AS SeptemberBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%October%' THEN 1 ELSE 0 END) AS OctoberBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%November%' THEN 1 ELSE 0 END) AS NovemberBookings,
SUM(CASE WHEN Booking.EndDate LIKE '%December%' THEN 1 ELSE 0 END) AS DecemberBookings
FROM
Booking

How to convert Int to Varchar

I tried to convert Int to Varchar but I'm unable to get output. Can anyone suggest any other way to do so?
I have below query and need to cast StoreNo (int) to a varchar:
ALTER PROCEDURE [dbo].[getrevenue]
#date1 DATE,
#date2 DATE,
#StoreNo NVARCHAR(max)
AS
BEGIN
DECLARE #sql_test NVARCHAR(max)
--SET #StoreNo='68,78,104'
SET #sql_test = 'SELECT t1.transtoreno AS StoreNO
,t3.NAME AS NAME
,t1.Dealdate AS DATE
,t1.UKEI AS UKEI
,t2.SubTotal AS SubTotal
,ISNULL(t2.SubTotalDiscount, 0) AS SubToatlDiscount
,ISNULL(t1.twoeyesSubtotalDiscount, 0) AS TwoeyeSubTotalDiscount
,t2.ValueInquiries AS TotalDiscount
,t2.NetSale AS Netsale
,t2.TotalSale AS ToatlSale
,t2.Cash AS Cash
,ISNULL(t2.GiftVochuer, 0) AS GiftVochuer
,ISNULL(t2.Card, 0) AS Card
,ISNULl(t2.Suica, 0) AS Suica
,t2.WONPOINT AS WAONPOINT
,t1.TaxExemption AS TAXExemption
,t2.TaxTotal AS TaxTotal
,t2.Returngoods AS Returngoods
,t2.Regiminus AS RegiMinus
,t2.PrintRecipt AS printrecipt
,ISNULL(t1.Visitorcount, 0) AS VisitorCount
FROM (
SELECT CAST(StoreNo AS NVARCHAR) AS transtoreno
,(DealDate) AS Dealdate
,SUM(SalePrice) AS UKEI
,SUM(TansuNebikiPrice) AS twoeyesdicount
,SUM(SubTotalNebiki2Price) AS twoeyesSubtotalDiscount
,SUM(TotalSalePrice - Si1Tax - RegiMinusNo) AS Netsale
,SUM(SpecialConsumptionTaxPrice) AS TaxExemption
,Sum(RegiMinusNo) AS Receiptissue
,SUM(VisitorCount) AS Visitorcount
FROM POS_TtlTran
GROUP BY StoreNo
,DealDate
) t1
LEFT OUTER JOIN (
SELECT DATE AS D
,cast(StoreNo AS NVARCHAR) AS s
,SUM(CASE
WHEN SerialNo LIKE 23
THEN DayTotalAmt
ELSE 0
END) AS Cash
,SUM(CASE
WHEN SerialNo LIKE 31
THEN DayTotalAmt
ELSE 0
END) AS Card
,SUM(CASE
WHEN SerialNo LIKE 30
THEN DayTotalAmt
ELSE 0
END) AS GiftVochuer
,SUM(CASE
WHEN SerialNo LIKE 138
THEN DayTotalAmt
ELSE 0
END) AS Returngoods
,SUM(CASE
WHEN SerialNo LIKE 160
THEN DayTotalAmt
ELSE 0
END) AS PrintRecipt
,SUM(CASE
WHEN SerialNo LIKE 304
THEN DayTotalAmt
ELSE 0
END) AS Suica
,SUM(CASE
WHEN SerialNo LIKE 26
THEN DayTotalAmt
ELSE 0
END) AS WONPOINT
,SUM(CASE
WHEN SerialNo LIKE 139
THEN DayTotalAmt
ELSE 0
END) AS Regiminus
,SUM(CASE
WHEN SerialNo LIKE 4
THEN DayTotalAmt
ELSE 0
END) AS SubToTal
,SUM(CASE
WHEN SerialNo LIKE 7
THEN DayTotalAmt
ELSE 0
END) AS SubTotalDiscount
,SUM(CASE
WHEN SerialNo LIKE 8
THEN DayTotalAmt
ELSE 0
END) AS TwoeyesubTotalDiscount
,SUM(CASE
WHEN SerialNo LIKE 18
THEN DayTotalAmt
ELSE 0
END) AS ValueInquiries
,SUM(CASE
WHEN SerialNo LIKE 22
THEN DayTotalAmt
ELSE 0
END) AS TotalSale
,SUM(CASE
WHEN SerialNo LIKE 114
THEN DayTotalAmt
ELSE 0
END) AS TaxTotal
,SUM(CASE
WHEN SerialNo LIKE 2
THEN DayTotalAmt
ELSE 0
END) AS NetSale
FROM POS_FinTtl
GROUP BY StoreNo
,DATE
) t2 ON t1.transtoreno = t2.s
AND t1.Dealdate = t2.D
LEFT OUTER JOIN (
SELECT StoreNo AS No
,StoreName AS NAME
FROM Store
) t3 ON t2.s = t3.No
WHERE (
t1.transtoreno IN ('''
+ CAST(#StoreNo AS NVARCHAR(max)) + ''')
AND (t1.Dealdate between ''' + CAST(#date1 AS VARCHAR(30)) + '''
AND ''' + CAST(#date2 AS VARCHAR(30)) + '''))'
END
Hope this one will work
ALTER PROCEDURE [dbo].[getrevenue] #date1 DATE
,#date2 DATE
,#StoreNo NVARCHAR(max) `
AS
BEGIN
DECLARE #sql_test NVARCHAR(max)
--SET #StoreNo='68,78,104'
SET #sql_test ='SELECT t1.transtoreno AS StoreNO
,t3.NAME AS NAME
,t1.Dealdate AS DATE
,t1.UKEI AS UKEI
,t2.SubTotal AS SubTotal
,ISNULL(t2.SubTotalDiscount, 0) AS SubToatlDiscount
,ISNULL(t1.twoeyesSubtotalDiscount, 0) AS TwoeyeSubTotalDiscount
,t2.ValueInquiries AS TotalDiscount
,t2.NetSale AS Netsale
,t2.TotalSale AS ToatlSale
,t2.Cash AS Cash
,ISNULL(t2.GiftVochuer, 0) AS GiftVochuer
,ISNULL(t2.Card, 0) AS Card
,ISNULl(t2.Suica, 0) AS Suica
,t2.WONPOINT AS WAONPOINT
,t1.TaxExemption AS TAXExemption
,t2.TaxTotal AS TaxTotal
,t2.Returngoods AS Returngoods
,t2.Regiminus AS RegiMinus
,t2.PrintRecipt AS printrecipt
,ISNULL(t1.Visitorcount, 0) AS VisitorCount
FROM (
SELECT CAST(StoreNo AS NVARCHAR) AS transtoreno
,(DealDate) AS Dealdate
,SUM(SalePrice) AS UKEI
,SUM(TansuNebikiPrice) AS twoeyesdicount
,SUM(SubTotalNebiki2Price) AS twoeyesSubtotalDiscount
,SUM(TotalSalePrice - Si1Tax - RegiMinusNo) AS Netsale
,SUM(SpecialConsumptionTaxPrice) AS TaxExemption
,Sum(RegiMinusNo) AS Receiptissue
,SUM(VisitorCount) AS Visitorcount
FROM POS_TtlTran
GROUP BY StoreNo
,DealDate
) t1
LEFT OUTER JOIN (
SELECT DATE AS D
,cast(StoreNo AS NVARCHAR) AS s
,SUM(CASE
WHEN SerialNo LIKE 23
THEN DayTotalAmt
ELSE 0
END) AS Cash
,SUM(CASE
WHEN SerialNo LIKE 31
THEN DayTotalAmt
ELSE 0
END) AS Card
,SUM(CASE
WHEN SerialNo LIKE 30
THEN DayTotalAmt
ELSE 0
END) AS GiftVochuer
,SUM(CASE
WHEN SerialNo LIKE 138
THEN DayTotalAmt
ELSE 0
END) AS Returngoods
,SUM(CASE
WHEN SerialNo LIKE 160
THEN DayTotalAmt
ELSE 0
END) AS PrintRecipt
,SUM(CASE
WHEN SerialNo LIKE 304
THEN DayTotalAmt
ELSE 0
END) AS Suica
,SUM(CASE
WHEN SerialNo LIKE 26
THEN DayTotalAmt
ELSE 0
END) AS WONPOINT
,SUM(CASE
WHEN SerialNo LIKE 139
THEN DayTotalAmt
ELSE 0
END) AS Regiminus
,SUM(CASE
WHEN SerialNo LIKE 4
THEN DayTotalAmt
ELSE 0
END) AS SubToTal
,SUM(CASE
WHEN SerialNo LIKE 7
THEN DayTotalAmt
ELSE 0
END) AS SubTotalDiscount
,SUM(CASE
WHEN SerialNo LIKE 8
THEN DayTotalAmt
ELSE 0
END) AS TwoeyesubTotalDiscount
,SUM(CASE
WHEN SerialNo LIKE 18
THEN DayTotalAmt
ELSE 0
END) AS ValueInquiries
,SUM(CASE
WHEN SerialNo LIKE 22
THEN DayTotalAmt
ELSE 0
END) AS TotalSale
,SUM(CASE
WHEN SerialNo LIKE 114
THEN DayTotalAmt
ELSE 0
END) AS TaxTotal
,SUM(CASE
WHEN SerialNo LIKE 2
THEN DayTotalAmt
ELSE 0
END) AS NetSale
FROM POS_FinTtl
GROUP BY StoreNo
,DATE
) t2 ON t1.transtoreno = t2.s
AND t1.Dealdate = t2.D
LEFT OUTER JOIN (
SELECT StoreNo AS No
,StoreName AS NAME
FROM Store
) t3 ON t2.s = t3.No
WHERE (
t1.transtoreno IN ('''
+ CONVERT(varchar(MAX), #StoreNo) + ''')
AND (t1.Dealdate between ''' + CAST(#date1 AS VARCHAR(30)) + '''
AND ''' + CAST(#date2 AS VARCHAR(30)) + '''))'
END
where (t1.transtoreno IN ('''
+ CAST(#StoreNo AS nvarchar(max)) + ''')
Should be
where (t1.transtoreno IN ('''
+ replace(CAST(#StoreNo AS nvarchar(max)),',',''',''') + ''')

TimeScaleData on C#

I am migrating a macro code into VSTO. I am having problems with TimeScaleData. the macro code is like this.
For Each tsk In ActiveProject.Tasks
If tsk.Summary = False And tsk.Duration <> 0 Then
With tsk
Set tsvs = .TimeScaleData(StartDate:=ActiveProject.ProjectStart, EndDate:=ActiveProject.ProjectFinish, Type:=pjTaskTimescaledBaselineWork, timescaleunit:=pjTimescaleWeeks, Count:=1)
End With
For Each tsv In tsvs
differencedate = 0
If tsk.BaselineStart <> "NA" And tsk.BaselineFinish <> "NA" Then
If tsk.BaselineStart >= tsv.StartDate And tsk.BaselineStart <= tsv.StartDate + 7 And tsk.BaselineFinish <= tsv.StartDate + 7 Then
differencedate = Application.DateDifference(tsk.BaselineStart, tsk.BaselineFinish, ActiveProject.Calendar) / 60
ElseIf tsk.BaselineStart >= tsv.StartDate And tsk.BaselineStart <= tsv.StartDate + 7 And tsk.BaselineFinish >= tsv.StartDate + 7 Then
differencedate = (Application.DateDifference(tsk.BaselineStart, tsv.StartDate + 7, ActiveProject.Calendar)) / 60
ElseIf tsk.BaselineStart <= tsv.StartDate And tsk.BaselineFinish >= tsv.StartDate And tsk.BaselineFinish <= tsv.StartDate + 7 Then
differencedate = (Application.DateDifference(tsv.StartDate, tsk.BaselineFinish, ActiveProject.Calendar)) / 60
ElseIf tsk.BaselineStart <= tsv.StartDate And tsk.BaselineFinish >= tsv.StartDate + 7 Then
differencedate = Application.DateDifference(tsv.StartDate, tsv.StartDate + 7, ActiveProject.Calendar) / 60
End If
End If
Next
End if
Next
I need to translate this to C#, anyone who can help?
What code do you have so far? Any exceptions in the code?
Anyway, you can use automatic code converters to get started quickly. Then you can correct the code manually.
Take a look at the sample code listed in the Microsoft Project: TimeScaleData Method from .NET without GC.Collect article.

UDF in C# that updates a table (dividing all data with result of query)

Is there a way to divide each data of a table (all columns and rows) by
select count(*) from table;
so for example
table
a1 a2 a3 a4
-------------------
438 498 3625 3645
500 291 5000 2351
233 263 1298 1687
198 117 1744 5932
438 498 3625 3648
500 291 5000 2637
and then divide each of them by 6 which is the number of rows
a1 a2 a3 a4
------------------------
73.0 83.0 604.16 607.5
83.33 48.5 833.33 391.83
...
...
...
...
One problem is that my data are INT, but after transform they would be double...
In c# I was doing:
int N = 0;
string sql = #"select count(*) from 'ExampleTable'";
N = (int)cmd.ExecuteScalar();
int Columns = 0;
sql = #"select count(*) from information_schema.COLUMNS WHERE TABLE_NAME = 'ExampleTable'";
Columns = (int)cmd.ExecuteScalar();
cmdLoad.CommandText = "SELECT * FROM [ExampleTable]";
int i,j;
double[,] newTable = new double[N, Columns];
using (SqlDataReader reader = cmdLoad.ExecuteReader())
{
while (reader.Read())
{
for ( i = 1; i <= N; j++)
{
for ( j = 1; j <= Columns; j++)
{
newTable[i,j] = reader.GetDouble((i*j) - 1) / N ;
} //j
} //i
}//reader
}
but I am not sure...
If you know the columns in advance, it's pretty easy:
SELECT
a1 / Rows As a1,
a2 / Rows As a2,
a3 / Rows As a3,
a4 / Rows As a4
FROM
(
SELECT *, CAST(COUNT(*) OVER (PARTITION BY NULL) as float) As Rows
FROM ExampleTable
) Data
If not, you'll need to do effectively what you're doing in C# - loop through the columns and build a dynamic query
DECLARE #TableName varchar(100)
DECLARE #Query varchar(max)
SELECT #TableName = 'ExampleTable'
SELECT #Query = 'SELECT '
SELECT #Query = #Query + '[' + COLUMN_NAME + '] / Rows as [' + COLUMN_NAME + '], '
FROM information_schema.COLUMNS col WHERE TABLE_NAME = #TableName
SELECT #Query = #Query + 'Rows FROM ( SELECT *, CAST(COUNT(*) OVER (PARTITION BY NULL) as float) As Rows FROM [' + #TableName + ']) Data'
EXECUTE(#Query)

Categories