SQL DateTime convert to Time String - sql

I have table Proccesses that consist of Date Time Field.
Proccesses ( Id as int , RunDate as DateTime)
I need to run Distinct by RunDate as Time Without seconds
For Example
ID RunDate
1 2011-12-13 12:36:26.483
2 2011-12-12 12:37:22.421
3 2011-12-11 12:36:44.421
I need to receive in output
Output
12:36
12:37
I tried to use DatePart but did not manage
Please help

This should work:
SELECT DISTINCT DATENAME(hour, RunDate) + ':' + DATENAME(minute, RunDate) AS Output...

For SQL Server
select CAST(DATEPART(hour, RunDate) as varchar) + ':' + CAST(DATEPART(minute, RunDate) as varchar)

Try
for hours: DATEPART(hh, RunDate)
for minutes: DATEPART(mi, RunDate)

select CONVERT(varchar,RunDate,103) + ' ' + CONVERT(varchar(5),RunDate,108)
(in Sql Server)

Related

Query Datediff to output HH:MM in SQL Server

I'm working with SQL Server 2012, and I have two columns start and end with varchar(5) values in HH:MM format.
The data looks like this
ID Start End
------------------------
1 00:00 06:00
2 06:00 16:00
3 16:00 18:00
4 18:00 24:00
My query is like this:
SELECT
a.start,
a.[end],
RIGHT('0' + CONVERT(VARCHAR(3), DATEDIFF(MINUTE, a.Start, a.[end]) / 60), 2) + ':' +
RIGHT('0' + CONVERT(VARCHAR(2), DATEDIFF(MINUTE, a.Start, a.[end]) % 60), 2) AS TotalHours
FROM
TransactionActivity a
I exec the query with where clause based on ID number, it gives me the correct result, until in ID 4: i got error like this
Conversion failed when converting date and/or time from character string.
I think it because the End time value is 24:00, how can I make it to get the time difference?
I think you'd be better off converting them into full datetime's (using an arbitrary date) because then the date functions will work correctly.
select
a.[start]
, a.[end]
, RIGHT('0' + CONVERT(varchar(3),DATEDIFF(minute,a.[Start], a.[end])/60),2) + ':' +
RIGHT('0' + CONVERT(varchar(2),DATEDIFF(minute,a.[Start],a.[end])%60),2)
as TotalHours
from (
select id
, case when [Start] = '24:00' then dateadd(minute, 1, convert(datetime, '23:59')) else convert(datetime, [Start]) end [Start]
, case when [End] = '24:00' then dateadd(minute, 1, convert(datetime, '23:59')) else convert(datetime, [End]) end [End]
from TransactionActivity
) a

How to group by Date and Hour :00:00 in SQL Server 2008

I want to create hourly report and this query is running perfectly.
SELECT
FORMAT(PingLogDate,'yyyy-MM-dd-HH:00:00') AS CalculatedTime,
COUNT(PingLogID) AS PingCount
FROM
tbl_PingLog
GROUP BY
FORMAT(PingLogDate,'yyyy-MM-dd-HH:00:00')
ORDER BY
FORMAT(PingLogDate,'yyyy-MM-dd-HH:00:00')
But I'm supporting SQL Server 2008 and Format is new in SQL Server 2012, so I tried this code
SELECT
CAST(PingLogDate AS date) AS CalculatedDate,
DATEPART(hour, PingLogDate) AS CalculatedTime,
COUNT(PingLogID) AS PingCount
FROM
tbl_PingLog
GROUP BY
CAST(PingLogDate AS date), DATEPART(hour, PingLogDate)
ORDER BY
CAST(PingLogDate AS date), DATEPART(hour, PingLogDate)
The problem is don't want to 2 column for date and hour for example
First query's result is
CalculatedTime | PingCount
--------------------+-----------
2017-11-07-12:00:00 | 359
2017-11-07-13:00:00 | 359
2017-11-07-14:00:00 | 350
Second query's result is
CalculatedDate |CalculatedTime | PingCount
---------------+---------------+----------
2017-11-07 |12 | 359
2017-11-07 |13 | 359
2017-11-07 |14 | 350
I don't want second one. I want first one. How to do that with SQL Server 2008 supported tools.
You can use CONVERT to transform the datetime column to yyyy-mm-dd hh:mi:ss(24h) format and then replace the space with -.
Something like this:
SELECT REPLACE(CONVERT(VARCHAR(32), PingLogDate, 120), ' ', '-') AS CalculatedTime,
COUNT(PingLogID) AS PingCount
FROM tbl_PingLog
GROUP BY REPLACE(CONVERT(VARCHAR(32), PingLogDate, 120), ' ', '-')
ORDER BY REPLACE(CONVERT(VARCHAR(32), PingLogDate, 120), ' ', '-')
Try this method with convert and concatenation of hours.
This will give you ping count for each hour and datetime format same as you got using FORMAT method in SQL Server 2012:
SELECT REPLACE(CONVERT(CHAR(10), date_field, 120),' ','-') + '-' + RIGHT('0' + CONVERT(VARCHAR(2), DATEPART(HOUR, date_field)), 2) + ':00:00' as CalculatedTime,
COUNT(PingLogID) AS PingCount
FROM tbl_PingLog
GROUP BY REPLACE(CONVERT(CHAR(10), date_field, 120),' ','-') + '-' + RIGHT('0' + CONVERT(VARCHAR(2), DATEPART(HOUR, date_field)), 2) + ':00:00'
ORDER BY REPLACE(CONVERT(CHAR(10), date_field, 120),' ','-') + '-' + RIGHT('0' + CONVERT(VARCHAR(2), DATEPART(HOUR, date_field)), 2) + ':00:00'
Simplified version of other solutions
SELECT
replace(convert(varchar(13),PingLogDate,120),' ','-')+':00:00' AS CalculatedTime,
COUNT(PingLogID) AS PingCount
FROM
tbl_PingLog
GROUP BY
convert(varchar(13),PingLogDate,120)
ORDER BY CalculatedTime
what about using this easy statement in your code?
declare #date datetime = getdate()
select DATEADD(dd, DATEDIFF(dd, 0, #date), 0)
result will be like 2017-11-25 00:00:00.000
This is the best way to trim the time from the datetime field

How to convert separated Year(int), Month(int) and Day(int) columns into DateTime

Our ERP system holds year, month and day in separate int columns. I want to combine those 3 int columns into one DateTime, How can I convert it in the SQL Server.
Year | Month | Day
--------------------
2016 | 1 | 23
You could use DATEFROMPARTS(SQL Server 2012+):
SELECT DATEFROMPARTS([Year], [Month], [Day]) AS DateTim
FROM table_name
SQL Server 2008 (assuming that year is 4-digit):
SELECT CAST(CAST([year] AS VARCHAR(4)) + '-' +
CAST([month] AS VARCHAR(2)) + '-' +
CAST([day] AS VARCHAR(2))
AS DATE) AS DateTim
FROM table_name;
LiveDemo
As Gordon Linoff proposed in comment to utilize YYYYMMDD as INT to convert to DATE:
SELECT CAST(CAST([year] * 10000 + [month]*100 + [day] AS VARCHAR(8))
AS DATE) AS DateTim
FROM table_name;
LiveDemo2
Addendum
To address ypercubeᵀᴹ concerns about dateformat we could utilize ISO-8601 which is not affected by DATEFORMAT or LANGUAGE settings:
yyyy-mm-ddThh:mm:ss[.mmm]
SELECT CAST(RIGHT('000'+ CAST([year] AS VARCHAR(4)),4) + '-' +
RIGHT('0' + CAST([month] AS VARCHAR(2)),2) + '-' +
RIGHT('0' + CAST([day] AS VARCHAR(2)),2) + 'T00:00:00' AS DATE)
FROM table_name
LiveDemo3
To handle years before 1000 should be padded with zeros.
We can use SQL's CONVERT function to get Datetime value :
SELECT CONVERT(DATETIME, CONVERT(VARCHAR(2), Day) + '/' + CONVERT(VARCHAR(2), Month) + '/' + CONVERT(VARCHAR(4), Year),103) FROM Table_Name

How to get min date with date part in SQL

I would like to ask how to get MIN DatePart from a DateTime column in a SQL Server Select query.
This is my query:
SELECT
MIN(CAST(DATEPART(DD, DateCreated) AS NVARCHAR) +
N'/' +
CAST(DATEPART(MM, DateCreated) AS NVARCHAR) +
N'/' +
CAST(DATEPART(YYYY, DateCreated) AS NVARCHAR)) AS DateFrom
FROM
[User]
The result should be 04/04.2013 but right now, the result is 01/04/2014
Please help ....
SELECT MIN(CAST(DATEPART(DD,DateCreated) AS NVARCHAR) +
N'/' +
CAST(DATEPART(MM,DateCreated) AS NVARCHAR) +
N'.' +
CAST(DATEPART(YYYY,DateCreated) AS NVARCHAR)) AS DateFrom
FROM [User]
use the aggregation query like below one
select min(date) from tablename

Datetime format help

I want only Hr and Min in time format so i did
select convert(varchar(20),GETDATE(),108) ===>> output is "12:57:54"
but i want only "12:57" this much so how i will do that in sql server
select convert(varchar(5),GETDATE(),108)
Select cast(DatePart(hour, getdate()) as varchar(2)) + ':' + cast(DatePart(minute, getdate())as varchar(2))