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
Related
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')
So my data is 1:30 PM. I want to have a Data when user selects a time, I want it to add 1hr 30mins to that and make the data as 1:30 PM - 3:00 PM. I've been searching for an answer and tried some workarounds but i couldn't get it. BTW the datatype of my Time is Varchar, because when I tried using the Time datatype I had some issues on inserting from C# to SQL
tested with sql-server 2008
declare #t varchar(25)
set #t = '1:30 PM'
--conert a varchar to time
select CONVERT(time, #t)
--if you want to add 90 minutes to it
select DATEADD(minute,90,CONVERT(time, #t))
--this does a few things
-- 1) converts the time stored as a varchar to the time datatype
-- 2) adds 90 minutes
-- 3) converts the time result back to the varchar datatype
select convert(varchar(10), dateadd(mi, 90, convert(time, '1:30 PM')), 100)
--this will show a final result of "3:00PM"
EDIT:
The following query will also return a varchar with the space between before the AM/PM.
select format(dateadd(mi, 90, convert(datetime, '1:30 PM')), 'h:mm tt')
The 'h:mm tt' is written with one "h" so that it will show up as "3:00 PM", instead of "03:00 PM".
Using Replace:
This would be how I would use replace to get to the goal of "3:00 PM"
select replace(replace(convert(varchar(10), dateadd(mi, 90, convert(time, '1:30 PM')), 100), 'P', ' P'), 'A', ' A')
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
I tried to obtain date in this format:
'05-31-2014 01:20:25 AM'
I used below code:
Sql Fiddle here, but output date strangely changed to a different date: 30-26-2011 01:30:38 AM
select format(CAST('2011-11-26 01:30:38.000' AS datetime), 'mm-dd-yyyy hh:mm:ss tt')
Sql Fiddle here
When using the FORMAT() function, mm is minutes, MM is month, so change to:
SELECT FORMAT(CAST('2011-11-26 01:30:38.000' AS DATETIME), 'MM-dd-yyyy hh:mm:ss tt')
If there's already an appropriate format available via CONVERT(), that is preferable as it performs better than the FORMAT() function.
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
/