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

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

Related

SQL - convert yyyy-MM-ddThh:mm:ssZ to simple date with no time

I need to convert date/time with this format yyyy-MM-ddThh:mm:ssZ to simple date with no time.
Example : 2020-10-21T07:28:48.021Z
to : 2020-10-21
CAST(XX AS TIMESTAMP) works fine, but it doesn't drop the time in my sql query as i would like.
Any idea ? Thanks !
Below is for BigQuery Standard SQL
select date('2020-10-21T07:28:48.021Z')
with output
cast(field as date) OR try_cast( field as date)
/*
Try cast will not return error if it is not convertable it will return null*/
It's done differently in different databases, but in DB2, it's super simple:
select date(thatTimestamp)
from sysibm.sysdummy1;
HTH !
you want cast( 2020-10-21T07:28:48.021Z as date)
or
select cast(dateField as date) as [DateFieldName]

How to convert date from decimal to date

I have this in my Excel file that I receive : 1/1/1980 2:34:52 PM and after importing it's now stored as 29221.6075462963.
The database field is a nvarchar (200). I need a query that just shows exactly like excel : 1/1/1980 2:34:52 PM.
I am using SQL server 17.
In general, you can use something like this:
select dateadd(second,
floor((v.edt - floor(v.edt)) * 60*60*24),
dateadd(day, floor(v.edt), '1899-12-30')
)
from (values (29221.6075462963)) v(edt);

Remove the zeros using SQL Server

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.

SQL Server datetime Format Isn't Working

TimeOpened is a column in my dataase table of type datetime2.
My convert statement is:
convert(nvarchar, TimeOpened, 114)
My output is supposed to be just the time (24 hours based) without the date, i.e.
5/16/2016 1:38:00 PM --> 13:38:00
but in practice the output I get is 01:38:00
Why is this happening?
Additionally I would like to know how to remove the seconds. Basically I want my output to be 13:38.
Thanks.
Try this (SQL Server 2012 or later):
SELECT FORMAT(TimeOpened,'dd/MM/yyyy HH:mm:ss')
For sql server 2008 you can cast to time and then convert to char(5):
SELECT CONVERT(char(5), CAST(TimeOpened As Time))
SELECT FORMAT(cast(TimeOpened as time),N'hh\.mm') as mytime
you can see more examples on https://msdn.microsoft.com/en-us/library/hh213505.aspx#ExampleD
try this
SELECT LEFT(CONVERT(VARCHAR,TimeOpened,108),5)
Solved it.
The problem was that when I inserted the date I used c# datetime.now instead of getdate()

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