I'm trying to convert my MS Access query That uses Dateserial, Month, and Weekday functions to work in SQL Server 2008.
With the following values in a record:
[dbo_TBL_TEST].[MFG_YYYY] = "2012"
[dbo_TBL_TEST].[MFG_WW] = "43"
The result from the following MS Access Query expression will be 82.
MFG_Test_INDEX: (Month(DateSerial(Val([dbo_TBL_TEST].[MFG_YYYY]),1,1)-Weekday(DateSerial(Val([dbo_TBL_TEST].[MFG_YYYY]),1,3))+(Val([dbo_TBL_TEST].[MFG_WW])*7))+(Val([dbo_TBL_TEST].[MFG_YYYY])-2006)*12)
Is there a way to do this in SQL Server 2008?
T-SQL has a MONTH() function. It doesn't have a direct DateSerial() equivalent, but you could "glue together" the date string and then use CAST() to convert it to the appropriate date type. And in place of Weekday() you can use DATEPART(dw, datevalue). Details on these and other T-SQL date functions are available here.
I found out how to do it in SQL-Server 2008....
This returns the number of months from the current year,work week (MFG_YYYY,MFG_WW) since Jan 2006 using the ISO 8601 standard.
DATEPART ( Month, Dateadd(weekday,+4,Dateadd(day,-1,DATEADD(DAY, 7 * MFG_WW,1,2) + DATEDIFF(DAY, 4, DATEADD(YEAR, MFG_YYYY - 1900, 7)) / 7 * 7, 1 - 8))))+((DATEPART ( Year, Dateadd(weekday,+4,Dateadd(day,-1,DATEADD(DAY, 7 * MFG_WW + DATEDIFF(DAY, 4, DATEADD(YEAR, MFG_YYYY - 1900, 7)) / 7 * 7, 1 - 8))))-2006)*12) AS MFG_Index
Related
I have an SSRS report (2008R2 3.0) that uses parameter filters for workweek in the format YYYYWW. In 2018 there were 52 work weeks based on the ISO standard (source) .
For default parameter value I select the last WW using the following formula:
DATEPART(DateInterval.Year, DATEADD("d", -7, now())) & FORMAT(DATEPART(DateInterval.WeekOfYear, DATEADD("d", -7, now())), "00")
For some odd reason this returned 201853. From what I understand it should be 201852 given the date when ran was 20180106.
I know in T-SQL I can define return type using ISO_WEEK but is there an equivalent for DateInterval in SSRS?
To simplify:
DATEPART(DateInterval.WeekOfYear, DATEADD("d",-7,now()))
Returns 53 instead of 52 when ran from 201801-201807.
Any idea as to why this is returning the wrong WeekOfYear value?
I think this may be due to your Regional First Day of the Week setting on your server.
If your First Day of the Week is Saturday, there are 53 weeks in the 2018. You can set the first day of the week with the DATEFIRST setting in SQL Server.
Use SELECT ##DATEFIRST to determine your servers setting.
This example code will show the difference.
SET DATEFIRST 7
SELECT ##DATEFIRST
SELECT DATEPART(WEEK, '2018-12-30')
SET DATEFIRST 1
SELECT ##DATEFIRST
SELECT DATEPART(WEEK, '2018-12-30')
For your query, you may need to set the first day of the week to Sunday with SET DATEFIRST 1.
MS Docs: DATEFIRST
Hi I am developing an SSIS package in VS2008 for SQL Server 2008.
To read the data from source, I have added the ADO.NET source editor and written below queries in the SQL command window but getting error.
Select *
From Table
Where CreatedDate Between DATEADD('d', -90, Date()) And Date()
Error for above SQL query:
ERROR [42000] [IBM i [System i Access ODBC Driver] [DB2 for i5 / OS] SQL0170 - Number of arguments for function DATE invalid. (CWBODBC.DLL)
Select *
From Table
Where CreatedDate Between DateAdd('d', -90, GETDATE()) And GETDATE()
Error for this second SQL query:
ERROR [42S02] [IBM] [System i Access ODBC Driver] [DB2 for i5 / OS] SQL0204 - GETDATE of type * N in * LIBL not found. (CWBODBC.DLL)
Is there anyone who can help me to write the SQL command query to get the last 3 month's data?
You appear to be querying a DB2 for IBM i V5 databases, judging by the error message. Indeed there's no GETDATE() function in DB2. There's no DATEADD() either. Instead, you substitute a value of the CURRENT DATE special register (variable) and calculate the range like so
SELECT * FROM Table
WHERE CreatedDate BETWEEN CURRENT DATE - 90 DAYS AND CURRENT DATE
Try this You will get data from last three month to tilldate
SELECT * FROM TABLE WHERE CreatedDate >= DATEADD(MONTH,-3,getdate())
Try this....
WHERE CreatedDate >= CAST(GETDATE() - 90 AS DATE)
AND CreatedDate <= CAST(GETDATE() AS DATE);
OR
WHERE CreatedDate >= CAST(DATEADD(MONTH, -3 , GETDATE()) AS DATE)
AND CreatedDate <= CAST(GETDATE() AS DATE);
How to convert CAST(0xE3350B00 AS DATE) to mysql ?
I used various forum supports and have following results
0xE3350B00 => 2059-04-03 22:56
0x0000A17F00000000 => 2013-03-12
But both dates belong to same line of data so i am confident that 0xE3350B00 should convert to nearby 2013-03-12 but not finding it technically? Can anyone help if conversion of 0xE3350B00 date??
I used following code in sql function:
return date_add(date_add("1900-01-01 00:00:00", interval conv(substr(HEX(raw_data), 5, 4), 16, 10) DAY), interval conv(substr(HEX(raw_data), 1, 4), 16, 10) MINUTE);
The code you have looks as though it parses SQL Server datetime values (though incorrectly if they have any time part).
This looks like a SQL Server binary date format.
These are stored differently. They occupy three bytes and represents the number of days since 0001-01-01 as a byte reversed integer.
You can use
SELECT
CAST(
'0001-01-01 00:00:00' +
INTERVAL CAST(CONV(
CONCAT(substr(HEX(BinaryData),5,2),
substr(HEX(BinaryData),3,2),
substr(HEX(BinaryData),1,2))
, 16, 10) AS SIGNED) DAY
AS DATE) AS converted_date
FROM
(
SELECT 0xE3350B00 AS BinaryData
) d
Which evaluates to 2012-07-08.
SQL Fiddle
I have a table named table1. It has a column NT_T_SENTDATE. This column keeps dates. It has data since 2005. Therefore this table is so heavy.
I want to see the rows which date is from dec 2013 to dec 2014 in NT_T_SENTDATE column. I know there is datediff method, I have used this query for this:
select *
from notificationtransactions
where datediff(day, NT_D_SentDate, '2015-03-25') > 360
BUT it is also showing the records older than 2013.
Can someone please correct me? What should I add to the query so that I can see only records older than 1 year, but not older than 3 years?
Looking forward your response
POOJA
This Query will return only this year records
SELECT *
FROM notificationtransactions
WHERE NT_T_SENTDATE> DATEADD(YY, -1, GETDATE())
This Query will return only Previous year records
SELECT *
FROM notificationtransactions
WHERE NT_T_SENTDATE BETWEEN
DATEADD(YY, -2, GETDATE()) AND DATEADD(YY, -1, GETDATE())
How about this:
SELECT *
FROM dbo.NotificationTransactions
WHERE
NT_D_SentDate <= '20140325'
AND NT_D_SentDate >= '20130101'
It shows all records with a "sent date" between Jan 1, 2013 and one year ago.
I've got an SSRS report that returns unique login count to our VDI Pools broken down by week.
For example for January it would read:
Week # of Logins
2015_JANUARY_WEEK_NO_1 3
2015_JANUARY_WEEK_NO_2 49
I'm using the Week column to link to another report that'll display the selected week's logins so I have to get the first day of the selected week and the last day of the select week and pass them to the other report as parameters.
I know how to get pull the year and week # from the Week column.
=Mid(Fields!Week.Value,1,4)
gives me the year and
=Trim(Mid(Fields!Week.Value,Len(Fields!Week.Value),Len(Fields!Week.Value)))
gives me the week.
I found this T-SQL that works:
DECLARE #WeekNum INT
, #YearNum char(4);
SELECT #WeekNum = 2
, #YearNum = 2015
-- once you have the #WeekNum and #YearNum set, the following calculates the date range.
SELECT DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + #YearNum) + (#WeekNum-1), 6) AS StartOfWeek;
SELECT DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + #YearNum) + (#WeekNum-1), 5) AS EndOfWeek;
but I cannot figure out how to turn that into an expression that doesn't throw an error.
This is what I've got so far:
=DateAdd("w", DateDiff("w", 6, '1/1/' + (Mid(Fields!Week.Value,1,4))) + (Trim(Mid(Fields!Week.Value,Len(Fields!Week.Value),Len(Fields!Week.Value)))
- 1), 6)
and when I try to run the report in design view it returns an Expression expected error.
Edit
Sorry, I guess I should've posted my original query that's populating the report. Here it is below:
SELECT Convert(varchar(20),UPPER(DATENAME(YEAR, Time)))
+'_'+CONVERT(varchar(20),UPPER(DATENAME(MONTH, Time)))
+'_WEEK_NO_'+CONVERT(varchar(10),(DAY(Time)
+ (DATEPART(DW, DATEADD (MONTH, DATEDIFF (MONTH, 0, Time), 0))-1) -1)/7 + 1) as 'Week'
, Count(DISTINCT SUBSTRING(ModuleAndEventText,LEN('User ') + 2
, CHARINDEX(' requested', ModuleAndEventText) - LEN('User ') - 2)) as WeekCount
FROM VE1_UserLogins
WHERE DesktopId = #Pool
AND ([Time] BETWEEN (#StartDate) and (DATEADD(ms, -1, #EndDate +1)))
GROUP BY Convert(varchar(20),UPPER(DATENAME(YEAR, Time)))
+'_'+CONVERT(varchar(20),UPPER(DATENAME(MONTH, Time)))
+'_WEEK_NO_'+CONVERT(varchar(10),(DAY(Time)
+ (DATEPART(DW, DATEADD (MONTH, DATEDIFF (MONTH, 0, Time), 0))-1) -1)/7 + 1),YEAR(Time),MONTH(Time)
ORDER BY YEAR(Time), MONTH(Time), Week
I am assuming your Fields!Week.Value is string in date format like MM/dd/yyyy or something similar so if you want to get the start of that week Sunday as first day and Saturday as last day and you want to get there date then you should use below expression,
For start of week,
=DateAdd("d",1- DatePart("w", CDate(Fields!WeekDate.Value)), CDate(Fields!WeekDate.Value))
For end of week,
=DateAdd("d", 7 - DatePart("w", CDate(Fields!WeekDate.Value)), CDate(Fields!WeekDate.Value))
----------
UPDATE
Now there are two ways to achieve what you want
1)Calculate start of week and end of week at the sql and get the result to directly display on repott
2) Get time field from the sql and set expression in the SSRS
For the first way to achieve you need to add these two lines in your select sql statement
DATEADD(dd, -(DATEPART(dw, MIN(Time))-1),MIN(Time)) AS 'StartOfWeek'
,DATEADD(dd, 7-(DATEPART(dw, MIN(Time))), MIN(Time)) AS 'EndOfWeek'
Second way to achieve is
Update your Sql statement to get the date part and then include that date into the above given expression.
So for you your query you need to add,
,MIN(Time) AS WeekDate
Then you can use the above expression with using the incoming field WeekDate as input(I have updated the expression).
But, If you have no actual need for the date other than calculating the start and end of week then use the first method to get the data from the sql server as formatted.