Remove the zeros using SQL Server - sql

Hello I am using SQL Server and I have this column :
data
6/19/2019 3:10:12 PM
12/23/2016 5:02:15 AM
I wanted to extract only the time so I used the following query :
select try_parse([data] as time using 'en-US') from Table_1
But the problem is I get as a result this :
15:10:12.0000000
05:02:15.0000000
All is okay except the 0 I mean I would like to get :
15:10:12
05:02:15
Thank you very much for your help !

You can use TIME() with the correct scale to get your desired results.
select Convert(time(0) , Data ) as time from Table_1
time(0) means a scale of 0, which has a precision of 8. This results as HH:MM:SS. Using another scale, like 1, would give you a single digit for the fractional second. You can read up on all of the scales in the MS Docs

Declare #data varchar(28)='12/23/2016 5:02:15 AM'
select Convert(varchar(20),Cast(#data as time),24)

Try this
select CONVERT(varchar(8), data, 108) from Table_1
that only show time (17:00:59) but you can add + between them if you want full format date as dd/MM/yyyy hh:mm:ss
select CONVERT(varchar(10), data, 101) + ' ' + CONVERT(varchar(8), data, 108) from Table_1
They will work if data using DATETIME type

You can extract time using below query
SELECT CONVERT(VARCHAR(8), cast('12/23/2016 5:02:15 AM' as time),108)
The output is- 05:02:15. I can hope this query/answer is self explanatory.

Related

How can I have the right format of time with SQL Server?

Hello I have this column :
date1
6/19/2019 3:10:12 PM
I wanted just to extract the time so I used the following query :
select try_parse([date1] as time using 'en-US') from myTable
But the problem is I got this :
15:10:12.0000000
whereas I would like to just have this :
15:10:12
How can I solve this ?
Thank you very much !
You can use FORMAT:
SELECT FORMAT(try_parse([date1] as datetime using 'en-US'), 'HH:mm:ss') from myTable;
You can try
SELECT convert(varchar(max), try_parse('6/19/2019 3:10:12 PM' AS time USING 'en-US'), 108)
FROM mytable;
if you want the result as a string for displaying purposes. (But you shouldn't store a date/time as string. Use the proper data types.)

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))

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()))

Convert time in SQL to 12 hour format WITH seconds on

I have a script that I am using to populate a time dimension table and I would like to have a column for the time in 12 hour format.
I know this can be done by doing something along the lines of
SELECT CONVERT(varchar(15),[FullTime],100)
Where [FullTime] is a column containing a TIME field in HH:MM:SS format.
But this gives the following result 2:30pm and I would like 2:30:47PM, note the inclusion of seconds.
I know I could build this up using substrings etc. but I wondered if there was a prettier way of doing it.
Thanks
SELECT GETDATE() 'Today',
CONVERT(VARCHAR(8), [FullTime], 108) 'hh:mi:ss'
Taken from here
This will give you a column 'today' followed by the time value you seek 'hh:mi:ss'
If having also milliseconds is not a problem for you, you can use
SELECT CONVERT(VARCHAR, [FullTime], 109)
Declare #tstTime datetime
set #tstTime = GetDate()
Select
#tstTime
,CONCAT(SUBSTRING(CONVERT(varchar(26), #tstTime, 109),1,20),RIGHT(CONVERT(varchar(26), #tstTime, 109),2))

How to convert date and time in SQL Server

I have the following columns in a table:
Signed_In_Date Signed_Out_Time
11/1/2005 12:00:00 am 11/1/2005 10:27:00PM
I would like to convert them to the following output:
Signed_In_Date Signed_Out_Time
11/1/2005 10:27:00PM
Is there a function or conversion code in SQL Server that would do it?
Assuming that the columns you're referring to are DATETIME columns, I would use the code below:
Date Only
SELECT CONVERT(VARCHAR(10), GETDATE(), 101)
Time Only
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), GETDATE(), 100), 7))
You can see the queries in action / play with them here.
For Sign_In_Date Use
select CONVERT(VARCHAR(10),'11/1/2005 10:27:00PM',108)
Ouput:
11/1/2005
For Sing_Out_Time
declare #time time
set #time=cast('11/1/2005 10:27:00PM' as Time)
select convert(varchar(10),#time,100)
Output:
10:27PM
try that :
select CONVERT(VARCHAR(10),Signed_Out_Time,108) ,-- 108 is d/M/yyyy if you want mm/dd/yyy you should use 101
CONVERT(VARCHAR(8),Signed_In_Date,103)
You can use CONVERT to change datetime format to your own desirable format:
SELECT CONVERT(VARCHAR(10),Signed_In_Date,101) as 'Signed_In_Date',
CONVERT(VARCHAR(10),Signed_Out_Time,108) as 'Signed_Out_Time';
For more date format, go over this link:
http://www.sql-server-helper.com/tips/date-formats.aspx