I have the following SQL Server code, that I need to translate to PL/SQL.
I'm trying to understand what this code does, can someone help me?
--STEP 0a:
Declare prior MONDAY and prior SUNDAY
DECLARE #ENDINGSUNDAY DATE
SET #ENDINGSUNDAY = --'2013-06-23' --PUT ENDING DATE FOR WEEK HERE(the sunday ending the week)
--/*
(SELECT MAX(DATE)
FROM DIM.DATE
WHERE DAY_OF_WEEK_DESC LIKE 'SUNDAY' AND DATE <=CAST(GETDATE()) AS DATE))
SELECT MAX(DATE) FROM DIM.DATE WHERE DAY_OV_WEEK_DESC LIKE 'SUNDAY' AND DATA <= SYSDATE
Related
I am using a SQL Server database, and I need to get the total number of newly inserted records per day, per week, per month, and per year separately. I have a column in my SQL Server database that records the date and time (the data type is datetime).
I used the following code to get the daily records but doesn't work
SQL:
select count(*)
from dbo.Firsttimer
where (Signed_in) = date(date_sub(now());
Please how do I achieve this?
You'll need to use conditional aggregation, i.e. a count(case...), along with the getdate() and dateadd() functions.
select
Today = cast(getdate() as date)
,TodayCount = count(case when cast(Signed_in as date) = cast(getdate() as date) then Signed_in)
,RollingWeekCount = count(case when cast(Signed_in as date) >= dateadd(day,-7,cast(getdate() as date)) then Signed_in)
,Rolling30Days = count(case when cast(Signed_in as date) >= dateadd(day,-30,cast(getdate() as date)) then Signed_in)
,Rolling365Days = count(case when cast(Signed_in as date) >= dateadd(day,-365,cast(getdate() as date)) then Signed_in)
from
YourTable
If you wanted this split out for each week, each month, each year... then that's best handled another way. However you didn't specify this so I provided a rolling method.
In SQL Server, you get the current DateTime value using the built in GetDate() method.
So to get all the records that have today's date in a specific DateTime column you can do something like this:
DECLARE #FromDate date = CAST(GETDATE() AS date)
DECLARE #ToDate date = DATEADD(DAY, 1, #FromDate)
SELECT COUNT(*)
FROM dbo.Firsttimer
WHERE [Signed_in] >= #FromDate
AND [Signed_in] < #ToDate
To get the first and last day of the week you can read this SO post,
first day and last day of the month on this SO post,
and first and last day of the year on this SO post
I'm creating a query in SQL Server 2008, for an SSRS report that runs every Monday at 0330am, retrieving weekend sales details.
The report is ready, but I had to hard-code the date range, as I'm having trouble expressing that condition in the WHERE statement.
I need to retrieve data based on column [salestime] (of type datetime), between Friday at 1230pm and Monday at 330am.
I'd really appreciate your assistance with this.
How about something like:
WHERE SalesTime BETWEEN DATEADD(HH,-63,GETDATE()) AND GETDATE()
I believe the time values are 63 hours apart. This is if your report automatically runs at 3:30 AM, which is what it sounds like in your post.
If you want to run your report any time during the week to report on last weekend's sale
SET DATEFIRST 7
DECLARE #ThisMonday date = DATEADD(DAY, 2 - DATEPART(WEEKDAY, GETDATE()), GETDATE())
DECLARE #LastFriday date = DATEADD(DAY, -3, #ThisMonday)
DECLARE #StartTime datetime = CAST(#LastFriday AS datetime) + CAST('12:30' AS datetime)
DECLARE #EndTime datetime = CAST(#ThisMonday AS datetime) + CAST('03:30' AS datetime)
SELECT #ThisMonday, #LastFriday, #StartTime, #EndTime
Now you can filter your report with salestime BETWEEN #StartTime AND #EndTime.
I have a Tasks table that contains Date, Description and WorkerID columns. I want to write a stored procedure that selects all tasks from this week and the following week. Can someone help me get to a solution please? I am not so good at this.
If indexing (performance) isn't a concern then it's a simple expression if your week coincides with the SQL Server setup:
where datediff(week, getdate(), [Date]) between 0 and 1
The snippet below is another option that works with both dates and datetimes. It's possible to adapt it to work with any ##datefirst setting but I'm just going to assume that the setting matches with the start of week you're looking to report on.
where
[Date] >= dateadd(day, 1 - datepart(weekday, getdate()), cast(getdate() as date))
and [Date] < dateadd(day, 15 - datepart(weekday, getdate()), cast(getdate() as date))
The following should give you what you want (assuming Monday is the first day of the week, and that the DBMS is SQL Server):-
select *
from Tasks
where [Date]
between
dateadd(dd,-(datepart(dw,[Date])-2),[Date]) --Monday of this week
and
dateadd(dd,13,dateadd(dd,-(datepart(dw,[Date])-2),[Date])) --Sunday of next week
I am using a gridview in my asp.net application in which i want to limit the dataset to requests that are submitted in the current month and year
Iam new to application development and thinking in the below way
to get the month and year part from the getdate() function and then compare it with the month and year of the receiveddate column in our table in the where condition
My select statement would be like below
select requestno,receiveddate,businessneed from customerrequests
where receiveddate = getdate()
in the above sql statement how do i acheive
There are two ways you could do this:
In SQL by passing two parameters for month and year. This is what your questions sounds like and can be done by just doing this:
select requestno,receiveddate,businessneed from customerrequests where YEAR(receiveddate) = YEAR(getdate()) AND MONTH(receiveddate) = MONTH(getdate())
The other way would be to construct a date object on the client side and pass that in to your query and then just use regular date comparison techniques.
Hope that helps.
You can do something like below:
DECLARE #FirstDate DATETIME
SELECT #FirstDate = DATEADD(month, DateDiff(Month, 0, GETDATE()), 0)
SELECT
requestno,receiveddate,businessneed
FROM
customerrequests
WHERE
receiveddate >= #FirstDate
Hope this Helps!!
How do I exclude values in a DateTime column that are Saturdays or Sundays?
For example, given the following data:
date_created
'2009-11-26 09:00:00' -- Thursday
'2009-11-27 09:00:00' -- Friday
'2009-11-28 09:00:00' -- Saturday
'2009-11-29 09:00:00' -- Sunday
'2009-11-30 09:00:00' -- Monday
this is the result I'm looking for:
date_created
'2009-11-26 09:00:00' -- Thursday
'2009-11-27 09:00:00' -- Friday
'2009-11-30 09:00:00' -- Monday
Thanks!
When dealing with day-of-week calculations, it's important to take account of the current DATEFIRST settings. This query will always correctly exclude weekend days, using ##DATEFIRST to account for any possible setting for the first day of the week.
SELECT *
FROM your_table
WHERE ((DATEPART(dw, date_created) + ##DATEFIRST) % 7) NOT IN (0, 1)
SELECT date_created
FROM your_table
WHERE DATENAME(dw, date_created) NOT IN ('Saturday', 'Sunday')
Assuming you're using SQL Server, use DATEPART with dw:
SELECT date_created
FROM your_table
WHERE DATEPART(dw, date_created) NOT IN (1, 7);
EDIT: I should point out that the actual numeric value returned by DATEPART(dw) is determined by the value set by using SET DATEFIRST:
http://msdn.microsoft.com/en-us/library/ms181598.aspx
Try the DATENAME() function:
select [date_created]
from table
where DATENAME(WEEKDAY, [date_created]) <> 'Saturday'
and DATENAME(WEEKDAY, [date_created]) <> 'Sunday'
The answer depends on your server's week-start set up, so it's either
SELECT [date_created] FROM table WHERE DATEPART(w,[date_created]) NOT IN (7,1)
if Sunday is the first day of the week for your server
or
SELECT [date_created] FROM table WHERE DATEPART(w,[date_created]) NOT IN (6,7)
if Monday is the first day of the week for your server
Comment if you've got any questions :-)
Calculate Leave working days in a table column as a default value--updated
If you are using SQL here is the query which can help you: http://gallery.technet.microsoft.com/Calculate...
Try this code
select (DATEDIFF(DD,'2014-08-01','2014-08-14')+1)- (DATEDIFF(WK,'2014-08-01','2014-08-14')* 2)