How to convert format to12 HRS instead of 24hrs SQL - sql

SELECT
Warehouses.Name, CONVERT(TIME,AirwayBillTrucks.CheckOutTime) AS CheckOutTime,

Assuming you are using SQL Server -
This will fetch you 12hr format
SELECT CONVERT(VARCHAR, Your_column_Name, 100) AS 12_hr_format
To show just the time
SELECT RIGHT(CONVERT(VARCHAR, Your_column_Name, 100), 7) AS time_in_12hr_format
OR simply use the code 108
SELECT CONVERT(VARCHAR, Your_column_Name, 108) AS time_in_12hr_format
Conversion -
100 - mon dd yyyy hh:miAM (or PM)
121 - yyyy-mm-dd hh:mi:ss.mmm(24h)
You can see all the type of format conversion here at Microsoft CAST and CONVERT

If SQL Server 2012+
Select Format(GetDate(),'hh:mm:ss tt')
Returns
03:55:30 PM

Related

How to get time in AM/PM 08:00AM format in SQL Server

This is the query I am using:
SELECT
CONVERT(varchar(15), CAST('08:00:00.0000000' AS TIME), 100)
And I get this output: 8:00AM
I want the output to be this: 08:00AM (two digit hours)
How can I get this output?
SELECT FORMAT(start_time,'hh:mm tt') AS start_time
FROM table_name
SQL Server way of LPAD
SELECT RIGHT('0'+ CONVERT(varchar(15),CAST('08:00:00.0000000' AS TIME),100), 7)

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

Convert date time to below format SQL Server

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 to subtract 2 dates in SQL and get HH:MI:SS

How to subtract 2 dates in SQL and get HH:MI:SS
(SQL Server 2005)
Iam using MS Access to do this.
Example:
23-09-2013 15:43:59
23-09-2013 15:43:33 -
Wanted answer 00:00:26
Use DateDiff, for the timespan format use string methods:
SELECT Diff =
right('0'+ rtrim(CAST(DateDiff(hour, #dt2, #dt1) AS VARCHAR(2))), 2) + ':' +
right('0'+ rtrim(CAST(DateDiff(minute, #dt2, #dt1)AS VARCHAR(2))), 2) + ':' +
right('0'+ rtrim(CAST(DateDiff(second, #dt2, #dt1)AS VARCHAR(2))), 2)
Demo
If the two dates are in a range of 24 hours you can use this code:
SELECT LEFT(CONVERT(VARCHAR, DATEADD(SECOND, DATEDIFF(SECOND, #Date2, #Date1), 0), 114), 8)
Source: How to convert Seconds to HH:MM:SS using T-SQL
If you can't use native SQL query in your Access program, give a look at these links (Access syntax):
LEFT
DATEADD
DATEDIFF
Maybe you can manipulate the resulting string without using CONVERT (not supported in Access).

DateAdd converting query from sql to oracle(sql developer)

I'm trying to migrate my sql query to oracle but it seems That i cannot convert my query due to DateAdd function.
STRINGVARIABLE = '1361439468476'
output is : Feb 20,2013
convert (char(12), (dateadd(s, convert(bigint, STRINGVARIABLE) / 1000, convert(datetime, '1-1-1970 00:00:00'))), 107)
DATEADD doesn't exist in Oracle. There are various ways to manipulate dates, but this is fairly straightforward:
select date '1970-01-01' + (to_number('1361439468476') / (1000*60*60*24))
from dual;
DATE'1970
---------
21-FEB-13
... which is actually 21/02/2013 09:37:48, so not sure why you have it as 20-FEB-13.
If you want to keep the millisecond precision you can use a TIMESTAMP instead:
select timestamp '1970-01-01 00:00:00'
+ numtodsinterval(to_number('1361439468476')/1000, 'SECOND')
from dual;
TIMESTAMP'1970-01-0100:00:00'+NUMTODSINTERVAL(TO_NUMBER('1361439468476')/10
---------------------------------------------------------------------------
21-FEB-13 09.37.48.476000000
I'm not sure what the 107 in your query is doing though, or convert, perhaps they are formatting the result as a string?
OK, I see what convert(..., 107) is doing; the equivalent is:
select to_char(date '1970-01-01'
+ (to_number('1361439468476') / (1000*60*60*24)), 'Mon DD, YYYY') as dt
from dual;
DT
------------
Feb 21, 2013
... using to_char() with a Mon DD, YYYY format model.