Remove nano seconds from "Time" SQL - sql

I have the following code in SQL
select "Time"
from Order
which returns - 09:10:11.0000000
How do I format this to give me just the hh:mm:ss ?

You can try like this:
SELECT CONVERT(VARCHAR, [time], 20) as 'Time'
from Order

TIME_FORMAT(time, '%T') will help you.
SELECT TIME_FORMAT(time, '%T') AS newTime FROM Order

It depends on the SQL implementation, but for SQL Server you should probably use the CONVERT() function.
Alternatively you could perform a substring on what is currently returned, or even a combination of the two.

Cast the value to time(0). The value in parentheses specifies the fractional seconds precision.
select cast([Time] as time(0)) as [Time]
time (Transact-SQL)

You should convert the field with date/time Styles.
Styles 20,108 will do the job:
select CONVERT(VARCHAR, [time], 108) as 'Time' from Order
select CONVERT(VARCHAR, [time], 20) as 'Time' from Order

Related

Convert to HH:MM in SQL Server

I would like to convert a date time format to hh:mm using SQL Server.
For example: convert this value 2017-08-17 15:31:18.217 into 15:31.
If anyone knows how to do this, please help.
Here is one way to do it:
DECLARE #DateTime datetime = '2017-08-17 15:31:18.217'
SELECT CONVERT(char(5), #DateTime, 108)
Result: 15:31
SELECT FORMAT(cast('2017-08-17 15:31:18.217' as datetime),'hh:mm')
SELECT CONVERT(VARCHAR(5), GETDATE(), 108)
You may want to use TRY_CONVERT if supported,and you don't always have a in that column.
One thing, the value that gets converted needs to be of type DATETIME.
Please execute these 2 queries , you will get the hours and minutes of current time.
SELECT GETDATE()
SELECT CONVERT(VARCHAR(5), CAST(GETDATE() AS TIME))

SQL convert single int to a time value

I am trying to convert a single integer which represents the hour value into a time. I've tried using cast but this converts the value to a date
cast(datepart(hh,tstart) as datetime) as test
I've also tried casting it as a time and the conversion is not allowed.
The numbers I am working with are 7,8,9,10,11,12,13,...,23, 0
The format I would like is convert 7 to 7:00, 23 to 23:00, etc
Thank you
There are many ways to do that:
WITH table_name AS
(
SELECT * FROM (VALUES
(1),(2),(3),(10),(23)
) T(H)
)
SELECT DATEADD(HOUR, H, 0) DateValue,
CONVERT(time, DATEADD(HOUR, H, 0)) TimeValue,
CONVERT(varchar(2), H)+':00' TextValue
FROM table_name T1
I would recomend storing value as TIME datatype.
You can try this:
select cast(dateadd(hour,3,'00:00:00') as time)
which gives result:
03:00:00.0000000
Use your int values in place of 3 in above statement
Use convert datetime to varchar datatype and use case statement
DECLARE #date datetime ='2016-05-01 10:00:000'
SELECT CASE cast(DATEPART(hh,#date)as varchar(10))
WHEN '10'then '10:00'
WHEN '11'then '11:00'
WHEN '12'then '12:00'
.
.
.
WHEN '23' then '23:00'
END 'Hourpart'
You can concatenate a minute to the time and cast it to time
select cast(concat(20,':00') as time(7)) as 'time'
If you wanted to display like 'HH:MM',use the below query.
SELECT CAST(23 AS VARCHAR(50))+':00'
If you wanted to get the result as time format,use the below query.
SELECT CAST(CAST(23 AS VARCHAR(50))+':00:00' AS TIME)
OR
SELECT CAST(DATEADD(HOUR,23,'00:00:00') as time)
here is the sample output :
Assuming all of your potential values are integers between 1 and 24, I think FORMAT is the simplest way.
FORMAT(tstart*100,'00:00')

Need part of a date

I am using SQL Server 2008R2.
I am trying to get part of a date in an output, and my column is in datetime datatype.
Eg,
If Current date and time is 2016-06-28 17:34:12.060, then I need output as 17:00 only the Hour with :00 mins.
I have tried this until now,
Select DateName(HH,SUBSTRING('2016-06-28 17:34:12.060',12,5)) +':00'
which gives me right output.But when I pass Column Name which is of datetime datatype, then it gives error,
Select DateName(HH,SUBSTRING(TimeInHour,12,5)) +':00'
gives error,
Argument data type time is invalid for argument 1 of substring function.
I know I am using SUBSTRING() at wrong place, But I really don't know how to achieve that output.
A help will be much appreciable.I need output as HH:00, Hour will be anything but 00 mins.
Why would you use substring() at all? The second argument to datename() should be a date/time data type. So, just do:
Select DateName(hour, '2016-06-28 17:34:12.060') + ':00'
Try this:
Select CAST(DATEPART(hour,'2016-06-28 17:34:12.060') AS VARCHAR(2)) +':00'
Below is the code that might be helpful
SELECT CONVERT(VARCHAR(50),DATEPART(YY,'2016-06-28 17:34:12.060')) -- Year
SELECT CONVERT(VARCHAR(50),DATEPART(mm,'2016-06-28 17:34:12.060')) -- Month
SELECT CONVERT(VARCHAR(50),DATEPART(d,'2016-06-28 17:34:12.060')) -- Day
SELECT CONVERT(VARCHAR(50),DATEPART(HH,'2016-06-28 17:34:12.060'))+':00' -- Hour
SELECT CONVERT(VARCHAR(50),DATEPART(mi,'2016-06-28 17:34:12.060'))+':00' -- Minutes
SELECT CONVERT(VARCHAR(50),DATEPART(ss,'2016-06-28 17:34:12.060')) -- Seconds
SELECT CONVERT(VARCHAR(50),DATEPART(ms,'2016-06-28 17:34:12.060')) -- Millisecond
You need to cast your DATETIME type column first, Use CAST function
Select DateName(HH,SUBSTRING(CAST(ColumnName AS VARCHAR(30)),12,5)) +':00'
Or alternative to do is Use LEFT and CONVERT
SELECT LEFT(CONVERT(VARCHAR, ColumnName ,108),2)+':00'
select convert(varchar, datepart(hour, getdate())) + ':' + convert(varchar, datepart(second, getdate()))

Selecting date in format dd/mm/yyyy from nchar column

I have date in column of nchar(255) datatype in this format
mm/dd/yyyy
Now I want to change it into
dd/mm/yyyy
I don't think that CONVERT help me in this as I have tried different queries like below
SELECT CONVERT(NCHAR(20), col1, 103) from table1
SELECT CONVERT(VARCHAR(20), col1, 103) from table1
select Convert(varchar(10),CONVERT(date,col1,101),103) from table1
In my view 103 which is in UK format converts only from yyyy-mm-dd
So I tried to CAST my col1 but I get an error.
Am I missing something? Is there any easy way to do this?
Do this in two expiicit steps. First, convert the string to a date using the 101 format (which is mm/dd/yyyy). Then explicitly convert the date back to a string using 103 (which is dd/mm/yyyy):
select convert(varchar(255), convert(date, datecol, 101), 103)
Two points. First, I don't think the result needs to be nvarchar() (the string only consists of numbers and a slash). Second, always include a length when using varchar() in SQL Server.
Here is a little SQL Fiddle.
You can try this:
SELECT CONVERT(VARCHAR, CONVERT(DATETIME, col1, 101), 103)
Check this:
Select convert(varchar(15), cast('01/26/2015' as datetime), 103) as FormattedDate
Where, '01/26/2015' is your col1
Edited Answer:
Select convert(nchar(255), cast(col1 as datetime), 103) as FormattedDate From table1
Where table1 is your table.
I am answering my own question.(Just in case anyone wants to know what is the solution)
There was no such problem with the query i was using. i.e.
select Convert(varchar(10),CONVERT(date,col1,101),103) from table1
The problem was with my nchar field.
It was having a special character (in my case a space) in every entry which was giving "out-of-range" error when tried to convert or cast.
So removing Special character(a space) solved my problem.

How to display the date as mm/dd/yyyy hh:mm Am/PM using sql server 2008 r2?

My sample query is
SELECT D30.SPGD30_LAST_TOUCH_Y
from CSPGD30_TRACKING D30
My given date format is like "2013-01-01 00:00:00.000". I need to convert this date format to "mm/dd/yyyy hh:mm AM/PM". Do you have any idea about that?
I think there is no single format to give them both. Try this using Convert; Sql-Demo
declare #mydate datetime = getdate()
select convert(varchar(10),#mydate, 101) + right(convert(varchar(32),#mydate,100),8)
| COLUMN_0 |
----------------------
| 02/22/2013 9:36AM |
The FORMAT() function is available from version 2012 onwards.
Once upgraded, you can use
select FORMAT(#date,'MM/dd/yyyy hh:mm:s tt')
Use this
select CONVERT(VARCHAR(10), mydate, 101) + ' ' + RIGHT(CONVERT(VARCHAR, mydate, 100), 7) from tablename
Try this
SELECT convert(varchar(20), GetDate(), 0);
To extract only AM/PM
substring(convert(varchar(30), GetDate(), 9), 25, 2);
Fiddle
Use the following scenario for getting date,time,day,month,year,hours,minutes,seconds,AM/PM
:)
SELECT UpdatedOn ,
CONVERT(varchar,UpdatedOn,100) DateTime,
CONVERT(varchar,UpdatedOn,10) Date ,
CONVERT(varchar,UpdatedOn,108) Time ,
substring(CONVERT(varchar,UpdatedOn,106),1,2) Day,
substring(CONVERT(varchar,UpdatedOn,106),4,3) CMonth,
substring(CONVERT(varchar,UpdatedOn,105),4,2) NMonth,
substring(CONVERT(varchar,UpdatedOn,106),8,4) Year,
left(right(CONVERT(varchar,UpdatedOn,100),7),2) Hours_12,
substring(CONVERT(varchar,UpdatedOn,108),1,2) Hours_24,
substring(CONVERT(varchar,UpdatedOn,108),4,2) Minutes,
substring(CONVERT(varchar,UpdatedOn,108),7,2) Second,
right(CONVERT(varchar,UpdatedOn,100),2) AM_PM
FROM dbo.DeviceAssignSim
WHERE AssignSimId=55;
You can do it like this:
SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS [MM/DD/YYYY]
For more information look this:
date-format
Use the convert method to format a datetime value. Example:
select convert(varchar(20), D30.SPGD30_LAST_TOUCH_Y, 101)
The third parameter determines the format. You can find the available formats in the cast and convert documentation.
Use this
Select (Convert(Varchar,GetDate(),101))+' '+(Right(('0'+(LTrim((Left((Right((Convert(Varchar,GetDate(),100)),7)),5))))),5))+' '+(Right((Convert(Varchar,GetDate(),100)),2))
I realize this is a 8 year old question but it was answered in many ways and none are simple enough. This is what I found to be simple and matches just about what the user is asking for (year is two digits, and seconds are present), assuming that the date he is getting is from GETDATE(), not that it matters but that is where my answer comes from.
SELECT CONVERT(varchar, D30.SPGD30_LAST_TOUCH_Y, 22) AS [DateTime]
12/16/20 10:19:18 AM