How to Display week names in the given date [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
For the Given date the Query should return the week names of particular date.
DECLARE #Date DATE = '2014-06-02 00:00:00.000'
If i given the the date as above it should return the week names of that particular week.
Expects output like this
______________________________________________________________________________________
mon tue wed thu fri sat sun
______________________________________________________________________________________
2014-06-02 2014-06-03 2014-06-04 2014-06-05 2014-06-06 2014-06-07 2014-06-08
______________________________________________________________________________________
I have the table like this
____________________________________________________________
empID empCheckInTime empCheckOut
____________________________________________________________
1 Jun 2 2014 12:37 PM Jun 2 2014 12:44 PM
2 Jun 6 2014 12:02 PM Jun 6 2014 12:03 PM
____________________________________________________________
My sql Query is
DECLARE #StartDate DATETIME = '2014-06-02 00:00:00.000'
DECLARE #EndDate DATETIME = '2014-06-09 23:59:59.999'
SELECT CONVERT(VARCHAR(10),DATEDIFF(HOUR,#StartDate,#EndDate))+'.'
+CONVERT(VARCHAR(10),DATEDIFF(MINUTE,#StartDate,#EndDate)% 60)
AS NoOfHours
Help me with select query in SQL Server 2008

Here is the query:
DECLARE #Date DATE = '2014-06-02'
;WITH CTE AS
(
SELECT #Date [Date],
DATENAME(DW,#Date) DayOfTheWeek,
1 i
UNION ALL
SELECT DATEADD(DAY,1,[Date]),
DATENAME(DW,DATEADD(DAY,1,[Date])),
i + 1
FROM CTE
WHERE i + 1 <=7
)
SELECT DayOfTheWeek,
[Date]
FROM CTE
ORDER BY i ASC

Related

Incremental and decremental count based on a date

Name Start_date end_date
aaa 01/02/2017 05/03/2017
bbb 03/05/2017 07/07/2017
ccc 02/01/2017 10/09/2017
I want to write a query that calculates the number of people who exist in the DB in a certain month/year.
Answer:
Jan 2017 1
Feb 2017 2
Mar 2017 3
Apr 2017 3
May 2017 2 (one person - aaa ,ended in May 2017)
Jun 2017 2
Jul 2017 1 (bbb ended in July 2017)
How do I write a PSQL query to get the desired output?
Thanks.
First, get the max and min dates in order to declare the dates range.
Second, with etc select all the month in the range.
Third, sum the number of the records in each dates.
Like:
declare #date date
declare #toDate date
select #date = min(Start_date),
#toDate = max(end_date)
from table_name
;With dt As
(
Select #date As [TheDate]
Union All
Select DateAdd(month, 1, TheDate) From dt Where [TheDate] < #toDate
)
select month(dt.TheDate),
year(dt.TheDate),
sum(case when table_name.Name is not null then 1 else 0 end)
from dt
left join table_name
on table_name.Start_date >= dt.TheDate
and table_name.end_date < dateadd(day,-1,dateAdd(month,1,dt.TheDate))

SQL Server 2014 Exploding Dates show only year

I got this data from my table:
id arrival_date departure_date
1 2017-10-10 2017-10-15
2 2017-11-16 2017-11-30
3 2017-10-14 2017-10-31
4 2018-05-12 2017-05-22
5 2021-01-16 2021-03-15
6 2018-06-02 2017-07-02
All i want is to explode these date but i only want to show the year only
date
2017
2018
2019
2020
2021
these should be the once that I wanted to see is their any query for these?
Note: if this year is 2018 it should start in 2018, so i will use getdate() in that instance, i want to get the max in departure_date part, can I do that?
You can try a recursive cte
DECLARE #maxYear INT
SELECT #maxYear = MAX(YEAR(departure_date)) FROM YourTable
;WITH cte
AS
(
SELECT YEAR(GETDATE()) AS [Year]
UNION ALL
SELECT [Year] + 1
FROM cte
WHERE [Year] < #maxYear
)
SELECT *
FROM cte
DEMO
Just use YEAR(arrival_date) and YEAR(departure_date)

How to get the data of previous weeks in the same month knowing the day as a search point

How to get the previous week(s) in the month knowing the day .
for example if i have table like this :
emp_num trans_date
22 1-10-2015
22 5-10-2015
22 7-10-2015
22 11-10-2015
22 14-10-2015
22 19-10-2015
22 27-10-2015
Now every month has 4 weeks .
So i have a date like 11-10-2015 (in the second week)
i want to get the result of all previous weeks in the same month .
so in this example i want the result of the first week in Oct month like this :
emp_num trans_date
22 1-10-2015
22 5-10-2015
22 7-10-2015
Note :
The start day of the week is: sat
The end day of the week is:Fri
If I understand your question correctly, for October 2015, you anticipate the following results:
RunDate Weekday Result
01-10-2015 Thurs [nothing]
02-10-2015 Fri [nothing]
03-10-2015 Sat 01-10-2015, 02-10-2015
04-10-2015 Sun 01-10-2015, 02-10-2015 (same as above)
...
09-10-2015 Fri 01-10-2015, 02-10-2015 (same as above)
10-10-2015 Sat 01-10-2015, ... , 09-10-2015
...
16-10-2015 Fri 01-10-2015, ... , 09-10-2015 (same as above)
17-10-2015 Sat 01-10-2015, ... , 16-10-2015
20-10-2015 Tue 01-10-2015, ... , 16-10-2015 (same as above)
30-10-2015 Fri 01-10-2015, ... , 23-10-2015
31-10-2015 Sat 01-10-2015, ... , 30-10-2015
If that is the case, then the following query will do what you require:
SELECT trans_date, emp_num, COUNT(1)
FROM transaction_table
WHERE MONTH(trans_date) = MONTH(TODAY)
AND YEAR(trans_date) = YEAR(TODAY)
AND trans_date <= DECODE(WEEKDAY(TODAY),
6, TODAY -1,
TODAY - WEEKDAY(TODAY) -2)
GROUP BY 1, 2
ORDER BY 1, 2
The logic in the DECODE() is to manage the offset from the default behaviour of WEEKDAY(), where Sun => 0 and Sat => 6. The keyword TODAY in the query above can be replaced with a variable or any arbitrary single date.
I havent worked in informix but here is how it looks like in SQL:
DECLARE #SomeDate DATE
SET #SomeDate = '2015-10-11'
DECLARE #DayOfWeek INT
SELECT #DayOfWeek = ((DATEPART(dw, #SomeDate) ) % 7)
DECLARE #WeekStart DATE
SELECT #WeekStart = DATEADD(d, -#DayOfWeek, #SomeDate)
SELECT * FROM SomeTable
WHERE MONTH(trans_date) = MONTH(#SomeDate) AND trans_date < #WeekStart

Issue with input parameters and data types in SQL Server

I have a table called report with 3 columns:
reportdate date, name varchar(10), location varchar(10)
Sample data:
reportdate name location
-------------------------------
2014-01-01 sachin vizag
2014-02-02 tendulkar vizag
2014-03-03 ram vizag
2014-04-04 robert vizag
2014-05-05 rahim vizag
2014-06-06 king vizag
2013-01-01 sachin vizag
2013-02-02 tendulkar vizag
2013-03-03 ram vizag
2013-04-04 robert vizag
2013-05-05 rahim vizag
2013-06-06 king vizag
Sample SQL Fiddle
My actual requirement was to create a date parameter for SSRS reports. So I have taken reportdate column for that purpose.
I need to create 3 parameters from single reportdate column
Parameter 1 :
I need to select only year value from reportdate column, for which I used this query
select distinct YEAR(ReportDate) as ReportYear
from report
and the result of it was
ReportYear
----------
2013
2014
Parameter 2:
I need to create a parameter for months column and this is the stored procedure which I have created
create procedure months
#years date
as begin
select distinct
Case
When CONVERT(varchar(2), ReportDate, 101) = 01 Then 'JAN'
When CONVERT(varchar(2), ReportDate, 101) = 02 Then 'FEB'
When CONVERT(varchar(2), ReportDate, 101) = 03 Then 'MAR'
When CONVERT(varchar(2), ReportDate, 101) = 04 Then 'APR'
When CONVERT(varchar(2), ReportDate, 101) = 05 Then 'MAY'
When CONVERT(varchar(2), ReportDate, 101) = 06 Then 'JUN'
When CONVERT(varchar(2), ReportDate, 101) = 07 Then 'JUL'
When CONVERT(varchar(2), ReportDate, 101) = 08 Then 'AUG'
When CONVERT(varchar(2), ReportDate, 101) = 09 Then 'SEP'
When CONVERT(varchar(2), ReportDate, 101) = 10 Then 'OCT'
When CONVERT(varchar(2), ReportDate, 101) = 11 Then 'NOV'
When CONVERT(varchar(2), ReportDate, 101) = 12 Then 'DEC'
end ReportMonth
--,year(reportdate) As ReportYear
from
report
where
reportdate in (select CAST(cast(#years as datetime)as date))
--order by DATEPART(m,ReportMonth)
end
I am passing years as parameter in the above procedure, if I pass value 2013/2014 as parameter then my output has to be as below
ReportMonth ReportYear
-----------------------
JAN 2014
FEB 2014
MAR 2014
APR 2014
MAY 2014
JUN 2014
JAN 2013
FEB 2013
MAR 2013
APR 2013
MAY 2013
JUN 2013
instead of the above output I am getting below output if I run the above stored procedure for year 2013.
ReportMonth ReportYear
------------------------
JAN 2013
I don't understand why only one month is being populated instead of all the available months in the table.
If you want to pass parameters as for example 2013/2014 you probably need to use a function to split the parameters (there are plenty of examples of split functions around the net), but if you don't want to handle splitting parameters one way to accomplish this is to use dynamic sql in the procedure:
CREATE PROC Months #YEARS VARCHAR(50)
AS
BEGIN
DECLARE #SQL VARCHAR(500) =
'SELECT DISTINCT
MONTH(ReportDate) AS [Month],
UPPER(LEFT(DATENAME(MONTH,ReportDate),3)) AS ReportMonth,
YEAR(ReportDate) AS ReportYear
FROM Report
WHERE YEAR(ReportDate) IN (' + #YEARS + ')
ORDER BY ReportYear DESC, [Month]'
EXEC (#SQL)
END;
I included the month number for sorting (and it might be good to have too).
When called as EXEC Months '2013, 2014' this would output:
Month ReportMonth ReportYear
----------- ----------- -----------
1 JAN 2014
2 FEB 2014
3 MAR 2014
4 APR 2014
5 MAY 2014
6 JUN 2014
1 JAN 2013
2 FEB 2013
3 MAR 2013
4 APR 2013
5 MAY 2013
6 JUN 2013
Another option would be to create a table valued type, declare a variable based on that type, insert the years you want to filter by and have the procedure accept a table valued type as parameter:
CREATE TYPE YearsType AS TABLE (Y INT)
GO
CREATE PROC M #YEARS YearsType READONLY
AS
SELECT DISTINCT
MONTH(ReportDate) AS [Month],
UPPER(LEFT(DATENAME(MONTH,ReportDate),3)) AS ReportMonth,
YEAR(ReportDate) AS ReportYear
FROM Report
WHERE YEAR(ReportDate) IN (SELECT Y FROM #YEARS)
ORDER BY ReportYear DESC, [Month]
GO
DECLARE #Years YearsType
--INSERT #Years VALUES (2013)
INSERT #Years VALUES (2013),(2014)
EXECUTE M #YEARS
This obviously has some drawbacks in that you need to declare a local table variable and populate it before calling the procedure.
Sample SQL Fiddle for both versions above.
I would say your where clause doesn't look correct. You should be checking the DATEPART(YEAR,reportdate) to equal the year you passed as a parameter to your stored procedure.
Your WHERE clause checks if reportdate is IN a set with only one date, which equals the 1 january of that year. This is obviously not what you want.

How to add years to date in SQL? [duplicate]

This question already has answers here:
How automatically add 1 year date to an existing date in SQL Server
(3 answers)
Closed 9 years ago.
How to add 2 years to date in SQL.
select left(cast(D as datetime) ,11) from table
In the above query how can i add 2 years.
Output is,
Jan 1 2012
Jan 2 2012
Jan 3 2012
Jan 4 2012
but i want output as,
Jan 1 2014
Jan 2 2014
Jan 3 2014
Jan 4 2014
Thank you
DateAdd() is the function you want http://technet.microsoft.com/en-us/library/ms186819.aspx
SELECT DateAdd(yy, 2, Cast(d As datetime))
select dateadd(yy,2,getdate())
finally i got, Query is SELECT left(DateAdd(yy, 2, Cast(d As datetime)),11) from table
You can use convert instead of the left:
SELECT CONVERT(VARCHAR, DATEADD(year, 2, CAST(d AS DATETIME)), 107) FROM table