t-sql convert datetime to time only with NO SECONDS - sql

There are many t-sql CONVERT strings to produce quite a variety of date and or time strings. But I cannot find the solution to needing no date and no seconds.
We want to return the time only from a datetime field, and eliminate the seconds. It would not matter if the seconds were truncated or rounded, but we need to show no seconds.
desired results- from any DATETIME field
10:00 AM
11:00 AM
4:59 PM
any and all insights or suggestions appreciated!!

Would this do it?
select CONVERT(varchar(15),CAST(GETDATE() AS TIME),100)
Just change out GETDATE() with your date variable.

Try this:
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), GETDATE(), 100), 7))
Put your DATETIME field instead GETDATE()

If you want a space then the AM /PM try this:
SELECT
Left(CONVERT(varchar(15),CAST(GETDATE() AS TIME),100),(len(CONVERT(varchar(15),CAST(GETDATE() AS TIME),100))-2)) +' ' + right((CONVERT(varchar(15),CAST(GETDATE() AS TIME),100)),2) as NiceTimeAMPm
NiceTime
7:35 AM
3:00 PM

Related

Converting Unix time to DateTime 103

I have a stored procedure that joins two tables of hotel booking data, however, the API that I pull my data from uses Unix time. I need to convert this to DateTime to match with my companies fields.
Currently, my conversion looks like this.
IIF([start] IS NOT NULL,
CONVERT(varchar(10), [start], 103),'') as 'ArrivalDate'
This just returns the value 1547310796 so no conversion has been done.
How do I convert the value to match 103 Date Time?
This should do it:
SELECT DATEADD(second, 1547310796 - DATEDIFF(second, GETDATE(), GETUTCDATE()), '1970-01-01')
The DATEDIFF(second, GETDATE(), GETUTCDATE()) part will give you how far behind you are from UTC time. You need to subtract that many seconds from the UTC timestamp to get the local time.
I just wanted to come back to this and add another answer in that works if you just want the date and not time.
SELECT cast(DATEADD(S,[start], '1970-01-01') as date) as 'ArrivalDate'
works on Oracle/Sql
select TO_date ('19700101000000','YYYYMMDDHH24MISS') + NUMTODSINTERVAL(<YOUR_COLUMN>+ decode (sessiontimezone, '+01:00', 3600, '+02:00', 7200, 0) , 'SECOND') as ArrivalDate

How to convert oracle to_timestamp to sql?

I want to convert Oracle
to_timestamp(coloum_name,'DD-MM-YYYY') to sql
required output : 24-APR-17 12.00.00.000000000 PM
I know this is old, but it has an very searchable title, and there's no accepted answer.
The TO_TIMESTAMP function converts text representations of dates to a standard date/time format. From your question, it sounds like you have dates stored as characters in the format 'DD-MM-YYYY', but you want SQL Server DATETIME2(7) (based on the number of decimals in the seconds) as your output. It also seems you want the default time to be noon, rather than midnight, since your sample output shows 12:00 PM, not AM.
Using CONVERT with style 103 will change the European styled date to a DATETIME2(7), as shown below. But then you'll need to do a DATEADD to move from midnight (which will be the default value) to noon, which is twelve hours later.
DECLARE #DateSample NVARCHAR(10) = '17-04-2017';
SELECT CONVERT( DATETIME2(7), #DateSample, 103 );
--Results
--2017-04-17 00:00:00.0000000
SELECT DATEADD( HOUR, 12, CONVERT( DATETIME2(7), #DateSample, 103 ));
--Results
--2017-04-17 12:00:00.0000000
The SQL Server default is 24 hour time, so if you absolutely must switch to AM/PM designators, you'll have to convert it back to a string, which seems to be the opposite of what you're trying to do.
This is a way to convert a date/timestamp into varchar2 in Oracle with the format you want
select to_char(yourColumn, 'DD-MON-YY HH.MI.SS.FF9 PM')
from yourTable
SELECT FORMAT(SYSDATETIME(), 'dd-MMM-yyyy h.mm.ss.fffffff tt')

SQL: Add space before AM/PM after military time conversion

I researched everywhere about this but I cannot seem to find it.
I have a column called OPEN_TIME which contains military time such as:
1900-01-01 23:00:00.000
I only want to extract the time, which I did successfully by doing:
LTRIM(RIGHT(CONVERT(varchar, OPEN_TIME, 100), 7))
However, this gives me a time of:
11:00PM
I would like to put a space before AM/PM so that it looks like:
11:00 PM
Not sure if this is as simple as it looks? Any advice would be appreciated.
If 2012+ you can use Format()
Declare #Open_Time DateTime = '1900-01-01 23:00:00.000'
Select Format(#Open_Time,'hh:mm tt')
Returns
11:00 PM
I should note that Format() is not known for its performance.
In any version of SQL Server you can use the REPLACE function
REPLACE(LTRIM(RIGHT(CONVERT(varchar, OPEN_TIME, 100), 7)),'PM',' PM')
If you aren't using 2012 or don't want to use Format you can accomplish this with STUFF.
Declare #Open_Time DateTime = '1900-01-01 23:00:00.000'
SELECT
STUFF(
LTRIM(RIGHT(CONVERT(VARCHAR, #OPEN_TIME, 100), 7)),
LEN(LTRIM(RIGHT(CONVERT(VARCHAR, #OPEN_TIME, 100), 7))) - 1,
0,
' ')
https://msdn.microsoft.com/en-us/library/ms188043.aspx

T-SQL : convert(datetime) to include/exclude certain hours

Using date range to select values, but also need to use an hour range to determine if a record should be selected. The date ranges and time ranges are not necessarily associated.
game_time (between 6 am and 6 pm)
have tried straight between statement and datepart, but cannot get anything to capture what we need.
create table gametime(name varchar, start_time datetime, end_time datetime)
insert assorted name, start_times and end_times
Desired results
name start_time end_time
name1 8:00 AM 10:00 AM
name2 8:00 AM 11:30 AM
name3 4:00 PM 5:30 PM
name4 6:00 PM 9:00 PM
datetime is used is storage, but not needed in presentation.. only times are needed in presentation.
Selected games should only start between the hours of 6:00 AM and 6:00 PM.
Any and all suggestions and insight appreciated......
Using
LTRIM(RIGHT(CONVERT(VARCHAR(20), start_time, 100), 7))
to get the correct format for presentation,
but when I try to use
LTRIM(RIGHT(CONVERT(VARCHAR(20), start_time, 100), 7)) > 6
I get conversion errors.
I would use DATEPART rather than relying on converting to/comparing strings:
WHERE DATEPART(hour,start_time) BETWEEN 6 AND 18
Try CONVERT(VARCHAR(5),start_time,108) BETWEEN '06:00' AND '18:00'. Right now you're trying to compare a string to an integer.
Here's another way, provided you're on SQL Server 2008 or higher and have the TIME type available:
SELECT *
FROM gametime
WHERE CAST(start_time AS TIME) BETWEEN '06:00:00' and '18:00:00'
This can be a bit more flexible when your time range is not anchored to exact hours. It also is sarg-able -- i.e. it will use an index, where calling DATEPART will prevent that.

How to assign current date with specific time to column?

How do i assign current date with a specific time?
let's say 8:00:00 AM to Column EXIT_DT of datatype datetime??
I have tried GETDATE() AS EXIT_DT but it gives me current datetime. I am using Sql server 2005. Any help?
Lets say Today is 1/3/2013 and i want my result to return as a datetime datatype with value 1/3/2013 8:00:00 AM. If i run the statement ytd, the result will be 1/2/2013 8:00:00 AM
This formula will always produce 08:00 for the day it is called, and avoids string manipulation:
select DATEADD(day,DATEDIFF(day,'20010101',GETDATE()),'2001-01-01T08:00:00')
Try to avoid solutions that convert to and from strings - treating datetime values as strings is one of the largest sources of bugs.
It works by computing the number of days (as an integer) that have elapsed since 1st January 2001. It then adds that same number of days to 08:00 on 1st January 2001.
You can try this :
DECLARE #dt datetime;
SET #dt=CONVERT(DateTime, CONVERT(VARCHAR,GETDATE(),101)+' 8:00:00')
SELECT CONVERT(VARCHAR, #dt, 101)+' '+ LTRIM(RIGHT(CONVERT(VARCHAR(20),#dt, 100), 7))
Visit http://www.sql-server-helper.com/tips/date-formats.aspx for datetime formats.
Use Convert along with getdate() to get specific formats.
ex:
SELECT CONVERT(VARCHAR(30),GETDATE(),113)
This is a bit stupid, but it works
select cast(cast(getdate() as date) as datetime) + '08:00:00'
it casts the getdate() to date thus losing the hours, than it casts it to datetime and adds 8 hours.
If you want to avoid implicit conversion of varchar to datetime, you could use this version:
select cast(cast(getdate() as date) as datetime)
+ convert(datetime,'08:00:00',114)
This is also working. (1). convert today's date to ISO format (yyyymmdd) (2). add the time, (3). convert back to datetime
Select convert(datetime, convert(varchar, getdate(),112) + ' ' + '8:00:00AM')
--Results
2013-01-03 08:00:00.000
If you need in specific format you need to convert back to varchar again.
-- AM/PM --
SELECT TO_CHAR(sysdate, 'MM/DD/YYYY HH:MI:SS AM') FROM dual
/
-- 24 hrs format --
SELECT TO_CHAR(sysdate, 'MM/DD/YYYY HH24:MI:SS') FROM dual
/