Get hour:minutes from DATETIME2 - sql-server

I have a problem in SQL Server 2008 R2. I want to get HH:mm from a DATETIME2 column. When I write this statement:
SELECT CONVERT(VARCHAR(5), getdate(), 8)
I am getting perfect result that is 11:19, but when I try to get time from:
SELECT (CONVERT(VARCHAR(5), '1900-01-01 01:10:00.0000000', 8))
I am getting result 1900 but I need 01:10.
How can I get the result from this statement? I know there there are multiple questions/answers like this but I have not found any answer that matches my question.

On SQL Server side (in stored procedure, for example), you can use this kind of conversion:
declare #dt datetime2(0) = sysdatetime();
select #dt as [Date], left(convert(time(0), #dt, 108), 5) as [HH:mm];
If you need to display your data on the client side, such as a report or your app, it's better to use client' formatting capabilities. This way, you may be able to take such factors into account as preferred calendar system, 12- or 24-hour clock, etc.

Try this:
SELECT RIGHT('0' + CONVERT(VARCHAR(2),
DATEPART(HOUR, '1900-01-01 01:10:00.0000000')), 2) hh,
DATEPART(minute, '1900-01-01 01:10:00.0000000') mm
Or
SELECT LEFT(CONVERT(TIME(0), '1900-01-01 01:10:00.0000000'), 2) hh , DATEPART(minute, '1900-01-01 01:10:00.0000000') mm

For many format of 24H datetime length is 19 so you can use the following
SELECT RIGHT('0' + CAST(DATEPART(HOUR, Substring('1900-01-01 01:10:00.0000000',1,19)) AS VARCHAR(2)), 2) + ':' + CAST(DATEPART(minute, substring('1900-01-01 01:10:00.0000000',1,19)) AS VARCHAR(2)) HHmm
Hope it helps

Try this, I think this is what you want.
this will give answer: 1:10
SELECT CONVERT(VARCHAR(5),CONVERT(DATETIME,'1900-01-01 01:10:00.000'),8)
You can also try this
select convert(VARCHAR,DATEPART(HOUR,'1900-01-01 01:10:00.0000000'))+':'+CONVERT(VARCHAR,DATEPART(MINUTE,'1900-01-01 01:10:00.0000000'))

Related

slice data between the range of dates SQL Server

I am having a miserable time in achieving a simple task. I want to slice data for every 6-months every month. I am not getting any output. My SQL skills are very bad. I searched for the solution a lot and they gave me some idea but I am not able to any output.
Below is my attempt:
SELECT TOP 10000 CONVERT(VARCHAR,DATEADD(HOUR,-4,DATEADD(s, ch.dateTimeOrigination, '19700101')), 121) as CallDate
FROM [dbo].[CallData] AS ch
WHERE LEN(ch.callingPartyNumber) = 4 AND
CONVERT(VARCHAR,DATEADD(HOUR,-4,DATEADD(s, ch.dateTimeOrigination, '19700101')), 121) BETWEEN CONVERT(VARCHAR(10), GETDATE(), 110) AND CONVERT(VARCHAR(10), DATEADD(month, -6, GETDATE()), 110)
The table definitely has the data for the time period I am trying to query. So I am not sure why this is not giving me any output. I will really appreciate your help. Thank you.
I believe your BETWEEN values are backwards.
EDIT: Your CONVERT statements in the WHERE clause are also different formats, so when you compare your varchars, the comparison fails. You should instead use DATETIME.
Try this:
SELECT TOP 10000
CONVERT(VARCHAR,DATEADD(HOUR,-4,DATEADD(s, ch.dateTimeOrigination, '19700101')), 121) as CallDate
FROM [dbo].[CallData] AS ch
WHERE LEN(ch.callingPartyNumber) = 4 AND
CAST(DATEADD(HOUR,-4,DATEADD(s, ch.dateTimeOrigination, '19700101')) AS DATETIME) BETWEEN DATEADD(month, -6, GETDATE()) AND GETDATE()

How to convert mm/dd/yyyy militarytime -> mm/dd/yyyy hh:mm:ss AM/PM

I'm using the following code and almost getting what I'm looking for:
SELECT sdb.NAME AS DatabaseName
,COALESCE(CONVERT(VARCHAR(10), cast(max(bus.backup_finish_date) as date), 101) + ' ' + convert(varchar(12), max(bus.backup_finish_date), 108), 'Never Restored') as [LastBackupTime]
FROM sys.sysdatabases sdb
INNER JOIN dbo.backupset bus
ON bus.database_name = sdb.NAME
GROUP BY sdb.NAME,
bus.backup_finish_date
Your result should be something like:
mm/dd/yyyy HH:mm:ss
I'm trying to get
mm/dd/yyyy hh:mm:ss AM/PM
I've tried multiple converts, a series of casts, ltrim/right, and even offering homage to the T-SQL overlords. No luck yet.
I've even tried
SELECT sdb.NAME AS DatabaseName
--Code below needs changed to show Date & time--
,COALESCE(CONVERT(VARCHAR(30), MAX(bus.backup_finish_date), 100), 'Never
backed up.') AS LastBackUpTime
FROM sys.sysdatabases sdb
INNER JOIN dbo.backupset bus
ON bus.database_name = sdb.NAME
GROUP BY sdb.NAME,
bus.backup_finish_date
but that gets me (for example) Mar 21 2017 10:47AM. We really prefer 3/21/2017 10:47AM.
Suggestions? I'm still picking this apart but could use some help.
Thanks!
If you are using SQL Server 2012 or later, you can use FORMAT():
Select Format(Max(bus.backup_finish_date), N'MM/dd/yyyy hh:mm:ss tt')
If you are using SQL Server 2012 or later you can use FORMAT, although be wary of doing this on large data sets.
SELECT FORMAT(GETDATE(), 'MM/dd/yyyy hh:mm:sstt')
For earlier versions, or if performance is a concern, For earlier versions, or if performance is a concern, you can concatenate the date in the format MM/dd/yyyy (style 101), with the time in the format hh:mm:ss (style 8) and a case expression to determine AM or PM
SELECT CONVERT(VARCHAR(10), GETDATE(), 101) + ' '
+ CONVERT(VARCHAR(10), GETDATE(), 8)
+ CASE WHEN DATEPART(HOUR, GETDATE()) < 12 THEN 'AM' ELSE 'PM' END
HOWEVER, formatting is a job for the presentation layer. If it was me doing this, then I would just send the native datetime, including nulls back to the presentation layer and let this handle it. It means that in your application layer you can still work with the dates, perform date calculations, or sort etc without the worry that 15/01/2017 is going to appear after 02/02/2017. It also means you can display dates in the end user's preferred locale, rather than yours.
One easiest way is to use format but it is not highly performant:
select FORMAT(Max(bus.backup_finish_date),'MM/dd/yyyy hh:mm:ss tt')
For earlier versions one another naive way of doing is as below:
Select CONVERT(VARCHAR(10), getdate(), 101) + ' ' + LTRIM(RIGHT(CONVERT(CHAR(20), getdate(), 22), 11))
Instead of GetDate() use your date
But that is already mentioned by #GarethD So never mind

Format date as 'DD Month YYYY'

Just have a mssql query that has a date in the format:
'2016-03-22 00:00:00.000'
I need to format it as:
'22 March 2016'
I'm using SQL Server 2012. I've tried googling and the usual 106, 112 codes don't seem to work.
Is there a specific code format I can use?
Try the FORMAT function:
SELECT FORMAT(GETDATE(), 'D', 'en-gb')
If your version does not support the FORMAT function, you can do it by concatenating the date parts:
SELECT
RIGHT('00' + CAST(DATEPART(DAY, GETDATE()) AS VARCHAR(2)), 2) + ' ' +
DATENAME(MONTH, GETDATE()) + ' ' +
CAST(DATEPART(YEAR, GETDATE()) AS VARCHAR(4))
Use Format Function in SQL Server 2012
SELECT FORMAT(GETDATE(),'dd-MMMM-yyyy')
See Image for your datatype ref
Try this query:
SELECT CONVERT(VARCHAR, CONVERT(DATETIME, '2016-03-22 00:00:00.000'), 106)
You need to convert into DATETIME then you will get your desired output
More Datetime Sql formate
SQLFiddle
This may help you
Try Convert function for SQL server:
select convert(varchar(11),cast('2016-03-22 00:00:00.000' as datetime), 106)
How to change day, month, year in PHP:
echo $purchase_date=date('F-Y',strtotime('2022-03-22 00:00:00.000'));
Answer:
March-2022

Easiest way to get time in hour and minutes from a long time format in sql?

I need to get only hour and minutes (hh:mm) from a long time format (hh:mm:ss) in sql.
CONVERT(char(5), SomeDateTimeValue, 108)
See CAST and CONVERT on MSDN
If you have time in TIME format in MySQL you can use SELECT TIME_FORMAT (time_column,"%H:%i") from table;
Refer to http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
In mysql you'd use this:
select time_format('12:34:44','%H:%i');
You can use the DATEPART transact sql function Datepart in msdn
You can check out the various combinations to achieve what you need.
If the underlying representation is a DATETIME, then you can try this (mocked up with a call to GETDATE()).
SELECT DATEPART(hour, GETDATE()) AS MyHours,
DATEPART(minute, GETDATE()) AS MyMinutes
Assuming your column is called timeCol, this will work:
select RIGHT('00' + cast (DATEPART(hour,timeCol) as varchar(2)), 2) + ':' +
RIGHT('00' + cast (DATEPART(minute,timeCol) as varchar(2)), 2),
from ...
The DATEPART calls get you the hour & minute, the RIGHT pads with a leading zero if the value is only one digit.
This works good for me:
SELECT SUBSTRING( convert(varchar, YourColumnName ,108),1,5) as 'StartDate'
FROM YourTableName

Remove time from DateTime sql server 2005

I need date part from datetime. in format of "dd-mm-yyyy"
I have tried follwoing
Query:
select Convert(varchar(11), getdate(),101)
Output:
01/11/2011
Query
SELECT cast(floor(cast(GETDATE() as float)) as datetime)
Output
2011-01-11 00:00:00.000
Query:
SELECT
CONVERT(VARCHAR(MAX),DATENAME(DD,GETDATE())) + '-' +
CONVERT(VARCHAR(MAX),DATEPART(MONTH,GETDATE())) + '-' +
CONVERT(VARCHAR(MAX),DATENAME(YYYY,GETDATE())) `
Output:
11-1-2011 i.e. "d-m-yyyy"
I required output in "dd-mm-yyyy" format.
SELECT CONVERT(VARCHAR(10),GETDATE(),105)
Try:
SELECT convert(varchar, getdate(), 105)
More here.
Here you can find some examples how to do this: http://blog.pengoworks.com/index.cfm/2009/1/9/Useful-tips-and-tricks-for-dealing-with-datetime-in-SQL
Using the CONVERT function "works" but only if you're comparing strings with strings. To compare dates effectively, you really need to keep the SMALLDATETIME data type strongly typed on both side of the equation (ie "="). Therefore 'apros' comment above is really the best answer here because the blog mentioned has the right formulas to use to strip off the time component by "flattening" it to midnight (ie 12:00:00) via rounding and any date column in SQL Server 2005 will always default to 12:00:00 if the date is given without a time.
This worked for me ...
select dateadd(day, datediff(day, '20000101', #date), '20000101')

Resources