How to sort hours in SQL Server? - sql-server

I have a table called Transaction. In that a column Time with TimeStamp datatype is found.
So the data will be looking like 2015-01-17 08:12:48.000
I want to display like 8 am
For example
`2015-01-17 08:12:48.000` `8 AM`
`2015-01-17 14:12:48.000` `2 PM`
now i got the result like above. This is my result
Hour
----
01 PM
02 PM
04 PM
05 PM
06 PM
07 AM
07 PM
08 AM
09 AM
10 AM
11 AM
12 PM
This is the query for above result.
SELECT
FORMAT(CAST(Time as datetime),'hh tt') hour,
COUNT(TransactionNumber) Total_Transaction,
SUM(Total) salesCost
FROM
[HQMatajer].[dbo].[Transaction]
WHERE
StoreID = '1001'
AND YEAR(Time) = '2015'
AND MONTH(Time) = '01'
AND DAY(Time) = '15'
GROUP BY
FORMAT(CAST(Time as datetime),'hh tt')`
Now I want to sort the hours. It should display like
07 AM
08 AM
09 AM
10 AM
11 AM
12 PM
01 PM
02 PM
.
.
07 PM
Thanks

Try adding this to the end of your statement:
, convert(varchar(2), [time], 8)
order by convert(varchar(2), [time], 8)
Resulting in this:
SELECT
FORMAT(CAST(Time as datetime),'hh tt') hour,
COUNT(TransactionNumber) Total_Transaction,
SUM(Total) salesCost
FROM
[HQMatajer].[dbo].[Transaction]
WHERE
StoreID = '1001'
AND YEAR(Time) = '2015'
AND MONTH(Time) = '01'
AND DAY(Time) = '15'
GROUP BY
FORMAT(CAST(Time as datetime),'hh tt')
, convert(varchar(2), [time], 8)
order by convert(varchar(2), [time], 8)
convert(varchar(2),[time],8) returns the datetime with style 8 in the following format: hh:mi:ss, and using varchar(2) truncates it to hh.
Documentation for convert and styles.
As Shakeer Mirza posted, using datepart() works as well.
Documentation for datepart.

Use DATEPART function simply
Select DATEPART(HH, YOUR_DATETIME_COL) AS HR, ......
........ --Write your Statements
........
ORDER BY HR
DATEPART will give result in integer format. So the Order by will give exact order

Related

how to convert month date in a text column into datetime

I have a table containing two columns like this:
Month_Date Year
Dec 31 2018
May 01 2020
Jun 05 2021
Jan 18 2022
Jul 19 2019
I hope to combine the Month_date and year in the same row and put it in a new column as a datetime format column. Could anyone help, please? I tried to convert the first column to a valid date, but failed because it doesn't show a complete month name.
You can try below approach.
Approach1:
select convert(datetime, [Month_Date] + ', ' + cast([Year] as varchar(4)), 107) from <TableName>
Approach2:
select cast(right(month_date,2)+'-'+left(month_date,3)+'-'+cast([Year] as varchar(4)) as date) from <TableName>

Joining three tables with OVER, for accumulative monthly figures

I have made the amendments as per comments and entered here as not enough characters allowed in comments. I think there may be a few issues on the OVER and with the JOIN. Updated query:
SELECT RIGHT('0' + CAST(day(oh_datetime) AS VARCHAR (2)), 2),
SUM(SUM((CASE oh_sot_id WHEN 1 THEN 1 WHEN 4 THEN -1 END) * oht_net)) over (ORDER BY day(oh_datetime) ROWS UNBOUNDED PRECEDING) AS 'Orders In($)',
SUM((CASE oh_cd_id WHEN 11728 THEN 1 END) * oht_net) AS 'Target($)',
SUM(SUM((CASE ih_credit WHEN 'false' THEN 1 WHEN 'true' THEN -1 END) * ih_net)) over (ORDER BY day(ih_datetime) ROWS UNBOUNDED PRECEDING) AS 'Sales($)'
FROM order_header_total
JOIN order_header ON order_header_total.oht_oh_id = order_header.oh_id
JOIN invoice_header ON order_header.oh_id = invoice_header.ih_oh_id
WHERE oh_datetime >= DATEADD(day, 1, EOMONTH(GETDATE(), -1)) AND oh_datetime < DATEADD(day, 1, EOMONTH(GETDATE()))
GROUP BY day(oh_datetime), day(ih_datetime)
UNION SELECT 'YAxis','0','0','0'
Table should display:
Date
Orders In
Target
Sales
01
6402.19
12321.57
128539.13
02
17795.94
24643.14
148258.63
03
50703.09
36964.71
171231.14
04
116034.29
49286.28
188157.69
28
353989.36
345004.00
446808.05
I have not done the full month for sake of space, but included the last day to show how it should accumulate. Instead it looks like this:
Date
Orders In
Target
Sales
01
5507.32
5507.32
01
5833.52
5833.52
01
6402.19
29377.18
02
10312.35
188157.69
02
16592.09
16023.42
02
17795.94
54950.68
03
30439.28
28666.76
03
30581.03
28808.51
04
36029.72
24700.77
04
37082.36
38767.55
23
144191.70
143992.45
Again a selection of data and then showing the last row for the sake of space. The date is not grouping, 'Orders In' and 'Sales' are not accumulating, and there is no data displaying for 'Target'.

How to do sorting by Day and Hour field?

I have a string value as 07052018080504623
It represents MM DD YYYY HH MM SS MS
means
MM = 07, DD = 05, YYYY=2018, HH = 08, MM=05 , SS= 04, MS = 623
Now I have a table defined as
declare #t table (WorkRequestId varchar(100))
insert into #t values
('07052018080504623'),('07062018012756663'),('07062018020148130'),('07062018095201231'),
('07062018102203805'),('07062018103718059'),('07062018110304836'),('07062018115356135'),
('07062018120624983'),('07062018124035480'),('07062018080504623'),('07062018070504623')
select
*
from #t
The records should be sorted in ascending order such that
WorkRequestId
07052018080504623
07062018095201231
07062018102203805
07062018103718059
07062018110304836
07062018115356135
07062018120624983
07062018124035480
07062018012756663
07062018020148130
The cutoff time is 8PM of previous day to 19:59:59 PM of next day.
In our example, 05 is previous day while 06 is next day.
Also no transaction happens between 1AM - 7:59:59 AM of next day. It starts again from 8AM of next day and continues till
19:59:59 PM.
So when we encounter 07062018012756663, the DD=06 and HH = 01. It means 13HRs (i.e. 1PM) of 6th. Same for 07062018020148130 where DD=06 and HH = 02 (i.e. 2PM or 14Hrs).
But 07062018095201231 where DD=06 and HH = 09 means 9AM of 6th.
That is why
07062018095201231 comes before 07062018012756663 and 07062018020148130
while ordering
My attempt so far (not correct yet)
select
*
,DY=SUBSTRING([WorkRequestId],3,2)
,HH = SUBSTRING([WorkRequestId],9,2)
,CurrentDY=CONVERT(varchar(2), getdate(), 103)
from #t
order by left([WorkRequestId],8) +
cast(iif(
SUBSTRING([WorkRequestId],3,2) = '6',--CONVERT(varchar(2), getdate(), 103),
iif(cast(SUBSTRING([WorkRequestId],9,2) as int) between 1 and 7,
cast(SUBSTRING([WorkRequestId],9,2) as int)+12,SUBSTRING([WorkRequestId],9,2)),
cast(SUBSTRING([WorkRequestId],9,2)as varchar(4)))as varchar(20))
+right([WorkRequestId],7)
So, I guess you should realize by now that storing dates as strings (and in fact, storing anything in the wrong data type) is bad practice.
The correct solution is to change the database structure to hold that data as DateTime2 instead of a string. However, assuming this can't be done for some reason, you can get the results you want by converting the string values to datetime2, adding 12 hours where the hour is between 1 a.m. and 8 a.m., and sort by that date.
I've written my suggestion in a cumbersome way because I wanted to show every part of the process - I've used 3 common table expression though if can be done in a single query - again, that's just to illustrate every step of the solution:
;WITH CTEDateParts AS -- break down the string to it's parts
(
SELECT WorkRequestId,
SUBSTRING(WorkRequestId, 5, 4) As Year,
SUBSTRING(WorkRequestId, 1, 2) As Month,
SUBSTRING(WorkRequestId, 3, 2) As Day,
SUBSTRING(WorkRequestId, 9, 2) As Hour,
SUBSTRING(WorkRequestId, 11, 2) As Minute,
SUBSTRING(WorkRequestId, 13, 2) As Second,
SUBSTRING(WorkRequestId, 15, 3) As Millisecond
FROM #t
), CTEDates AS -- create datetime values from the string parts
(
SELECT WorkRequestId,
CAST(Year +'-'+ Month +'-'+ Day +'T'+
Hour +':'+ Minute +':'+ Second +'.'+ Millisecond As DateTime2(7)) As DateValue
FROM CTEDateParts
), CTEFixedDates AS -- add 12 hours for hours between 1 and 8 a.m.
(
SELECT WorkRequestId,
DateValue,
CASE WHEN DATEPART(HOUR, DateValue) >= 1 AND DATEPART(HOUR, DateValue) <= 8 THEN
DATEADD(Hour, 12, DateValue)
ELSE
DateValue
END As FixedDate
FROM CTEDates
)
-- finally, select order by the FixedDate column
SELECT WorkRequestId
FROM CTEFixedDates
ORDER BY FixedDate
Results:
WorkRequestId
07052018080504623
07062018095201231
07062018102203805
07062018103718059
07062018110304836
07062018115356135
07062018120624983
07062018124035480
07062018012756663
07062018020148130
07062018070504623
07062018080504623
You can try the following:
select
*
from #t
order by left (WorkRequestId, 8) + (case when SUBSTRING(WorkRequestId, 9,2) between '01' and '07' then CAST(SUBSTRING(WorkRequestId, 9,2) + 12 AS CHAR(2)) else SUBSTRING(WorkRequestId, 9,2) end) + SUBSTRING(WorkRequestId, 11,7)

sql query to calculate the time difference between the two successive rows

Can anyone help me to calculate the time difference(in hh:mm) between the two rows, when the Procs_ID changes from 01 to another Procs_ID.
Procs_ID meter_id date
01 0000012 2015-10-12 09:07:22.530
03 0000013 2015-10-12 09:11:51.733
01 0000014 2015-10-12 09:12:38.550
02 0000015 2015-10-12 10:38:52.923
03 0000016 2015-10-12 10:40:33.467
01 0000017 2015-10-12 10:40:56.013
Thanks in advance
Here is an example with LEAD which is avaliable for SQL Server 2012+
select
*,
case
when Procs_Id = '01'
and lead(Procs_ID) over (order by [date]) <> Procs_ID then datediff(mi,[date],lead([date]) over (order by [date]))
end as TimeDiffInMin
from YourTable

yearweek on SQL server

How can I extract the year and week combination from a date in SQL Server using T-SQL and have it match the MySQL yearweek result?
For example these MySQL queries (Click here for MySQL SQL Fiddle):
SELECT yearweek('2013-12-31');
SELECT yearweek('2014-01-01');
This returns 201352 for both dates. That is the expected result.
But in SQL Server...
datepart works as expected for the year extract
sometimes datepart does not return the expected value for iso_week
The MySQL result cannot be achieved with this T-SQL query...
SELECT datepart(year, #dt) * 100 + datepart (iso_week, #dt);
T-SQL versions of the MySQL queries above (Click here for T-SQL SQL Fiddle):
SELECT datepart(year, '2013-12-31') * 100 + datepart (iso_week, '2013-12-31');
SELECT datepart(year, '2014-01-01') * 100 + datepart (iso_week, '2014-01-01');
The result is 201352 for the first date and 201401 for the second date.
However, this is not the expected result because...
2014-01-01 belongs to the last week of 2013
So the expected result is 201352
Do any of you more experienced T-SQL developers know how to extract the year/week of a given date and have this match what I see in MySQL?
I need to have the week start on Monday. This is why I am using iso_week. I have tested the results with week anyway and found the same issue. This query also produces 201401 when 201352 is expected.
SELECT datepart(year, '2014-01-01') * 100 + datepart (week, '2014-01-01');
If you look at the ISO_Week datepart definition at http://msdn.microsoft.com/en-us/library/ms174420.aspx, you'll see the following:
ISO 8601 includes the ISO week-date system, a numbering system for
weeks. Each week is associated with the year in which Thursday
occurs...
The numbering system in different countries/regions might not comply
with the ISO standard.
Since January 1, 2014 was a Wednesday; January 2, 2014 was the first Thursday of the year and thus week 1 of 2014 (according to ISO 8601).
Furthermore, looking at the MySQL definitions for yearweek and week, there are several mode options (http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week)
So, I think you're going to have to write your own yearweek function for the week counting rule you want:
CREATE FUNCTION dbo.yearweek(#date date)
RETURNS INT
as
begin
set #date = dateadd(dd,-datepart(dw,#date)+1, #date)
return datepart(year,#date)*100 + datepart(week,#date)
end
go
select dbo.yearweek('2013-12-31'), dbo.yearweek('2014-01-01')
NOTE: I haven't fully tested this code, and I'm not sure exactly what your requirements are. This is just meant as an example of the type of process you need to follow.
I think it's because you're using "ISO_WEEK" as the datepart
Look at the description under the "ISO_WEEK datepart" here: http://msdn.microsoft.com/en-us/library/ms174420.aspx
using "WEEK" instead might get the desired result
TO extract YEAR, MONTH or DAY you can simply use the YEAR, MONTH and DAY function as shown in the followinf example. for any other part you have to use the DATEPART function , read here for a detailed list of arguements you can pass to DATEPART function.
SELECT YEAR(GETDATE()) AS [YEAR]
, DATEPART(WEEK, GETDATE()) [Week]
UPDATE
SELECT CAST(YEAR(GETDATE()) AS VARCHAR(4)) +'-'
+ CAST(DATEPART(WEEK, GETDATE()) AS VARCHAR(2))
It seems to me that what you want is a week number based on the previous Monday. Is this correct? If so, iso_week is no use to you as it is based on this week's Thursday.
To get the year and week based on the previous Monday, I think you need something like the following.
set datefirst 1 ;
select
SomeDate ,
datename( weekday, SomeDate ) as [WeekdayName] ,
datepart( weekday, SomeDate ) as [Weekday] ,
datepart( week, SomeDate ) as [Week] ,
datepart( iso_week, SomeDate ) as [ISO_week] ,
dateadd( day, -( datepart( weekday, SomeDate ) - 1 ), SomeDate ) as [PreviousMonday] ,
datepart( year, dateadd( day, -( datepart( weekday, SomeDate ) - 1 ), SomeDate ) ) as [MondayYear] ,
datepart( week, dateadd( day, -( datepart( weekday, SomeDate ) - 1 ), SomeDate ) ) as [MondayWeek]
from
dbo.DateDemo
order by
SomeDate ;
Abbreviated sample output is as follows.
SomeDate PreviousMonday MondayYear MondayWeek
2013-12-25 2013-12-23 2013 52
2013-12-26 2013-12-23 2013 52
2013-12-27 2013-12-23 2013 52
2013-12-28 2013-12-23 2013 52
2013-12-29 2013-12-23 2013 52
2013-12-30 2013-12-30 2013 53
2013-12-31 2013-12-30 2013 53
2014-01-01 2013-12-30 2013 53
2014-01-02 2013-12-30 2013 53
2014-01-03 2013-12-30 2013 53
2014-01-04 2013-12-30 2013 53
2014-01-05 2013-12-30 2013 53
2014-01-06 2014-01-06 2014 2

Resources