Below mentioned is my Query:
SELECT DPSNumber, StatusDateTime FROM DispatchTool
The Resulst is:
DPSNumber StatusDateTime
123123526 8/4/14 12:00 AM
123123527 8/5/14 12:00 AM
123123528 8/6/14 12:00 AM
123123529 8/7/14 12:00 AM
I want to add an additional column to this result as shown below:
DPSNumber StatusDateTime FiscalWeek
123123526 8/4/14 12:00 AM 27
123123527 8/5/14 12:00 AM 27
123123528 8/6/14 12:00 AM 27
123123529 8/7/14 12:00 AM 27
Fiscal Week for my Organization started on January 31, 2014
Looks like DateDiff Function is what you need
SELECT DPSNumber,
StatusDateTime,
Datediff(wk, CONVERT(DATE, 'January 31, 2014'), CONVERT(DATE, StatusDateTime)) FiscalWeek
From Tablename
Related
I have a table named nifty_50 with the below two columns.
Date Close
Apr 07, 2022 17,763.40
Apr 06, 2022 17,807.65
Apr 05, 2022 17,957.40
Apr 04, 2022 18,053.40
Apr 01, 2022 17,670.45
Mar 31, 2022 17,464.75
I am trying to add another column which says the day name of the Date column.
Expected Output:
Date Close Day_name
Apr 07, 2022 17,763.40 Thursday
Apr 06, 2022 17,807.65 Wednesday
Apr 05, 2022 17,957.40 Tuesday
Apr 04, 2022 18,053.40 Monday
Apr 01, 2022 17,670.45 Friday
Mar 31, 2022 17,464.75 Thursday
I tried doing
select date, close, datename(date ,getdate()) as day_name, from nifty_50;
The above code doesn't work and all the other codes which i googled and tried also don't work. I know this is a simple code. can someone please help me with this?
Use the TO_CHAR function with the Day format model.
select "DATE",
close,
TO_CHAR("DATE", 'fmDay') as day_name
from nifty_50;
Note: fmDay will strip trailing spaces from the day names (otherwise the days will all be output as strings of length equal to the longest day name padded with trailing spaces).
Note 2: DATE is a reserved word in Oracle and cannot be used as an unquoted identifier. It is better practice to pick a different identifier instead of using reserved words.
Which, for the sample data:
CREATE TABLE nifty_50 ("DATE", Close) AS
SELECT DATE '2022-04-07', 17763.40 FROM DUAL UNION ALL
SELECT DATE '2022-04-06', 17807.65 FROM DUAL UNION ALL
SELECT DATE '2022-04-05', 17957.40 FROM DUAL UNION ALL
SELECT DATE '2022-04-04', 18053.40 FROM DUAL UNION ALL
SELECT DATE '2022-04-01', 17670.45 FROM DUAL UNION ALL
SELECT DATE '2022-03-31', 17464.75 FROM DUAL;
Outputs:
DATE
CLOSE
DAY_NAME
2022-04-07 00:00:00
17763.4
Thursday
2022-04-06 00:00:00
17807.65
Wednesday
2022-04-05 00:00:00
17957.4
Tuesday
2022-04-04 00:00:00
18053.4
Monday
2022-04-01 00:00:00
17670.45
Friday
2022-03-31 00:00:00
17464.75
Thursday
db<>fiddle here
You can try below to fetch Day name:
DECLARE #DateVal DATE = '2022-07-04';
SELECT #DateVal As [Date],
DATENAME(WEEKDAY, #DateVal) AS [Day Name],
DATENAME(DW, #DateVal) AS [Day Name],
DATENAME(W, #DateVal) AS [Day Name];
Is there a way to specify a date (2020-01-20) and then return every 2 weeks forward and backwards between a start and finish period. So 01 Jan 2020 to 01 Mar 2020 for example. A list of of all the dates every 2 weeks from this input date.
Sample of table data for reference...
DECLARE #Source TABLE(bookingcode int, NextWeekCommDate DATETIME, Cycle int)
insert into #Source (bookingcode, NextWeekCommDate, cycle)
select 556789, '23 Mar 2020', 2
insert into #Source (bookingcode, NextWeekCommDate, cycle)
select 556790, '30 Mar 2020', 3
select * from #Source
declare #from datetime = '01 Mar 2020'
declare #to datetime = '01 Jun 2020'
Then I am trying to output the following results based on the declared#to and #from dates
bookingcode CycleOccurDate
556789 2020-03-09 00:00:00.000
556789 2020-03-23 00:00:00.000
556789 2020-04-06 00:00:00.000
556789 2020-04-20 00:00:00.000
556789 2020-05-04 00:00:00.000
556789 2020-05-18 00:00:00.000
556790 2020-03-30 00:00:00.000
556790 2020-04-20 00:00:00.000
556790 2020-05-11 00:00:00.000
So it works backwards as well from the NextWeekCommDate if the #from date is before this Thank you again
You can use a recursive CTE:
with dates as (
select convert(date, '2020-01-01') as dte
union all
select dateadd(week, 2, dte)
from cte
where dte < '2020-03-01'
)
select *
from dates;
If this can return more than 100 rows, then use option (maxrecursion 0).
It is not clear to me what '2020-01-20' has to do with the question.
Sure, you can add directly or substract fom a date field
SELECT GETDATE(), GETDATE()-14, GETDATE()+14
or in date
SELECT CAST(GETDATE() AS DATE), CAST(GETDATE()-14 AS DATE), CAST(GETDATE()+14 AS DATE)
Take a look at DATE FUNCTIONS in SQLSERVER, there are plenty of them that you may use
https://learn.microsoft.com/es-es/sql/t-sql/functions/date-and-time-data-types-and-functions-transact-sql?view=sql-server-ver15
This query counts the amount of bottles we produce over a month and groups them by day. If there are no bottles produces that day then it is skipped from the output instead of returning 0 bottles produced. How can I return with the day's timestamp if there are no bottles produced? I heard the calendar table has to be used for this.
SELECT CONVERT(datetime,CAST(t_stamp AS DATE)), COUNT(bottles) AS 'Good Bottles'
FROM bottles
WHERE t_stamp
BETWEEN "any date"
AND "any date"
GROUP BY CAST(t_stamp AS DATE)
ORDER BY CAST(t_stamp AS DATE) ASC
Current Output:
Aug 12, 2019 12:00 am..................4302
Aug 13, 2019 12:00 am..................2302
Aug 17, 2019 12:00 am..................1302
Aug 18, 2019 12:00 am..................4302
Desired Output:
Aug 12, 2019 12:00 am..................4302
Aug 13, 2019 12:00 am..................2302
Aug 14, 2019 12:00 am..................0
Aug 15, 2019 12:00 am..................0
Aug 16, 2019 12:00 am..................0
Aug 17, 2019 12:00 am..................1302
Aug 18, 2019 12:00 am..................4302
You need to generate the days. A pretty simple method uses a recursive CTE:
WITH dates as (
SELECT CONVERT(date, "any date1") as dte
UNION ALL
SELECT DATEADD(day, 1, dte)
FROM dates
WHERE dte < "any date2"
)
SELECT d.dte, COUNT(bottles) AS GoodBottles
FROM dates d LEFT JOIN
bottles b
ON CAST(t_stamp as DATE) = d.dte
GROUP BY d.dte
ORDER BY d.dte ASC;
Notes:
If you have a calendar or tally table, then use that instead.
If the number of dates exceed 100, you need to add OPTION (MAXRECURSION 0).
COUNT(bottles) looks suspicious. Do you really intend SUM(bottles)?
Converting a column to a date and then to a datetime is also suspicious. It is unclear why you would want a datetime for the first column.
I have following sql query get sum of total hours for whole day,
this query works ok except the day for our objects starts from 02:00:00 AM till 02:00:00 AM of following day and in this query it groups daytime by normal day hours 12:00:00 AM till 12:59:59 PM.
How to add logic to group daytime from 02:00:00 AM till 02:00:00 AM?
select object_code,
sum(on_stream_hrs) on_stream_hrs,
daytime
from(select object_code,
on_stream_hrs,
trunc(daytime) daytime
from sub_day_status
where trunc(daytime) between to_date('1 Feb 2017') and to_date('28 Feb 2017')
)
where (object_code, daytime) in (select object_code,
trunc(daytime)
from sub_day_status
where trunc(daytime) between to_date('1 Feb 2017') and to_date('28 Feb 2017')
and on_stream_hrs >0.5)
group by object_code, daytime
order by daytime
thanks,
S
Just subtract 2 hours from the daytime before truncating:
SELECT object_code,
SUM( on_stream_hrs ) AS on_stream_hrs,
TRUNC( daytime - INTERVAL '2' HOUR ) AS daytime
FROM sub_day_status
WHERE daytime >= DATE '2017-02-01' + INTERVAL '2' HOUR
AND daytime < DATE '2017-03-01' + INTERVAL '2' HOUR
GROUP BY
object_code,
TRUNC( daytime - INTERVAL '2' HOUR )
HAVING MAX( on_stream_hrs ) > 0.5
ORDER BY
daytime;
The correlated sub-query appears to be something that you can either use a simple WHERE on_stream_hrs > 0.5 or by using a HAVING clause (as above) depending on how you want to filter the rows.
where trunc(daytime) between to_date('1 Feb 2017') and to_date('28 Feb
2017')
you can move the date by 2 hours by the following.
select to_date('1 Feb 2018', 'DD MON YYYY') + 2/24 from dual
Reference
What i want to do is:
If i select a record from the date 2016-06-01 06:00:00 to 2016-06-02 05:59:59 it should display under 2016-06-01 and not under 02
...GROUP BY CAST((DATEADD(hour, -6, YourDate) AS DATE)
if you want to find records occurring in '2016-08-05' (according to your requirement you do
CAST((DATEADD(hour, -6, YourDate) AS DATE) = 2016-08-05'
note that in my method 06:00:00 then acts like 'midnight' in a regular day system - i.e. at the stroke of 6, it is a new day
Simply subtract six hours:
select dateadd(hour, -6, mydate)
from mytable
this is i want to do. The records after 00:00:00 to 06:00:00 should comes under '2016-06-01 06:00:00' if there is a records for the date from 2016-06-01 06:00:00 to 2016-06-02 06:00:00
CASE
WHEN DATEPART(HOUR, RechargeOn) < DATEADD(HOUR, 6, RechargeOn)
DATEADD(HOUR, 18, DATEADD(DAY, -1, RechargeOn))
RechargeOn
END