T-SQL calculate with calendar week - sql

I have a T-SQL question - I'm using SQL-Server 2012:
I have a varchar calendar week column (1-52). In my where clause I only want to select the last 6 weeks going back from the current calendar week of the current year. I also have a year varchar column with values of "2011" , "2012" and "2013".
But now I'm struggling to set the expression for the where clause.
Or do I need a Having clause to get this done?
I've tried it like this:
Having Calendarweek BETWEEN max(Calendarweek)-6 AND max(Calendarweek) AND Year=2013
but it returns all weeks.
Hope you can help me out with this.
thank you.

Try this
where Year ='2013'
and CAST(Calendarweek AS INT) >= datepart(week,getdate())-6

Related

datepart in sql on year change

I am using datepart function in SP. It is used to compare week and year of specified date.
Now my query arise as new year starts. I have one table called 'Task' and it is storing tasks date wise. Now When I execute SP with DATEPART(YY,'2016-01-05') . It is giving proper 2016's data. But I execute it with DATEPART(ISOWK,'2016-01-05') , it is giving 2015's data also with 2016's data.
I want data from 28th dec,2015 to 2nd jan,2016. Data of the week. And I am not able get data of 1st and 2nd Jan in that. Please help me with this.
Thanks,
ISOWK return the week number
You are trying to get date from 2015-12-28 to 2016-01-02.
The week number of 2015-12-28 is 53 and 2016-01-02 is 53
If you already have your start and end dates decided then you could use a simple query as shown below to filter on date range.
When mentioning date string in your query make sure you stick to this format :yyyymmdd which SQL Server will automatically understand. You don't need DATEPART to get records between two dates.
Query for filtering on a date range
SELECT TaskID, TaskDescription
FROM Tasks
where TaskDate between '20151228' and '20160102'
Another query you can use for date range filtering
SELECT TaskID, TaskDescription
FROM Tasks
where TaskDate >= '20151228' and TaskDate <= '20160102'

Get the month and year now then count the number of rows that are older then 12 months in SQL/Classic ASP

I know this one is pretty easy but I've always had a nightmare when it comes to comparing dates in SQL please can someone help me out with this, thanks.
I need to get the month and year of now then compare it to a date stored in a DB.
Time Format in the DB:
2015-08-17 11:10:14.000
I need to compare the month and year with now and if its > 12 months old I will increment a count. I just need the number of rows where this argument is true.
I assume you have a datetime field.
You can use the DATEDIFF function, which takes the kind of "crossed boundaries", the start date and the end date.
Your boundary is the month because you are only interested in year and month, not days, so you can use the month macro.
Your start time is the value stored in the table's row.
Your end time is now. You can get system time selecting SYSDATETIME function.
So, assuming your table is called mtable and the datetime object is stored in its date field, you simply have to query:
SELECT COUNT(*) FROM mtable where DATEDIFF(month, mtable.date, (SELECT SYSDATETIME())) > 12

Changing the year of a date in SQL

I am relatively new to programming. I have a table and in the table I have a column which shows when an employee started at the company. I am creating a procedure and within that procedure, I want to take the users start date and replace it with the current year, this will then be followed by an if statement. If the date that has just been calculated is before today, then change the year of the date to next year. Thanks and sorry if the question isn't fully clear or has been asked before.
I don't think this is the most elegant solution, but you could take the date apart to replace it with the current year. Then use a case statement to change it to the next year if it has already passed. Here's the code:
SELECT employee_name,
CASE
WHEN CONVERT(VARCHAR,MONTH(start_date))+'/'+CONVERT(VARCHAR,DAY(start_date))+'/'+CONVERT(VARCHAR,YEAR(getdate())) < GETDATE() THEN CONVERT(VARCHAR,MONTH(start_date))+'/'+CONVERT(VARCHAR,DAY(start_date))+'/'+CONVERT(VARCHAR,YEAR(getdate())+1)
WHEN CONVERT(VARCHAR,MONTH(start_date))+'/'+CONVERT(VARCHAR,DAY(start_date))+'/'+CONVERT(VARCHAR,YEAR(getdate())) >= GETDATE() THEN CONVERT(VARCHAR,MONTH(start_date))+'/'+CONVERT(VARCHAR,DAY(start_date))+'/'+CONVERT(VARCHAR,YEAR(getdate()))
END AS anniversary_date
FROM Employees

Getting week number of date in SQL

Is there a way to get the week number of a date with SQL that is database independent?
For example to get the month of a date I use:
SELECT EXTRACT(MONTH FROM :DATE)
But the EXTRACT function doesn't know about weeks in SQL92.
Please pay attention to the fact that I want a solution that is database independent! Do not misinterpret this as a question regarding MS SQL Server.
There doesn't appear to be a single standard SQL function to extract the week number from a date - see here for a comparison of how different SQL dialects can extract different dateparts from date fields.
You can try this.
declare #date datetime
select #date='2013-03-15 00:00:00'
select 'Week No: '+ convert(varchar(10),datepart(wk,#date)) as weekno
Try this one :
SELECT DATENAME(yy,GETDATE())+RIGHT(DATENAME(wk,GETDATE()),2)
To understand DATENAME, please follow this link : sqltutorials.blogspot.be/2007/05/sql-datename.html and for the right, suite101.com/article/sql-functions-leftrightsubstrlengthcharindex-a209089 . There are examples to better understand ;-) It will work on MS and other sql servers normally ;-)
The only db independent solution I see is to get the number of days between today and Jan 1 of curr. year. Then divide it by 7 and round it up. There is 73 days from Jan 1 till today, which gives 10.43 as week number. The ISO week number is 11.
Correction: the number of days between last day of the current week and Jan 1 of curr. year. In this case the ISO week is 10, and 68/7 = 10 (rounded).
The best idea I found is
SELECT ROUND(((DAY(NOW())-1)/7)+1)
select (DATEPART(yyyy , CAST(GETDATE() AS datetime)) * 100 +
DATEPART(ww , CAST(GETDATE() AS datetime))) as week

query in SQL to get the months b/w the dates

I have a table with the following data.
id Date of Issue Date of Travel
---------------------------------
1 10-april-2011 10-may-2011
2 25-april-2011 22-may-2011
How can I display the number of months between the Date of Issue and Date of Travel in SQL (I am using an MS Access database)?
If I understand your question, you need to use the DateDiff function:
SELECT DATEDIFF ("m", [id Date of Issue], [Date of Travel]) FROM ...
To count the months between the dates:
SELECT MONTH('Date of Travel')-MONTH('Date of Issue')+12*(YEAR('Date of Travel')-YEAR('Date of Issue'))