I've made an SQL query which shows Date and Time, with Time split into Hours and Minutes.
select convert(varchar(10),[Date],23) as [Date],
datepart(hour, [Time])as Hour, datepart(minute, [Time])as Minutes,
FROM [SQLIOT].[dbo].[ZEPB_CaseLog]
The table shows Hours in 24-hour format.
But I want to have it in 12-hour format instead, with single digits having a '0' prefix.
i.e: 01, 02, 03, etc.
I thought of using case to do it:
select convert(varchar(10),[Date],23) as [Date],
case when
datepart(hour, [Time])> 12 then (datepart(hour, [Time])- 12) as Hour
, datepart(minute, [Time])as Minutes,
FROM [SQLIOT].[dbo].[ZEPB_CaseLog]
Doing so gives me a syntax error on line
datepart(hour, [Time])> 12 then (datepart(hour, [Time])- 12) as Hour
--Incorrect syntax near the keyword 'as'.
I'm not too familiar with doing subtractions in SQL itself. Is there anything else I should add to fix this?
There is a syntax error on case when, try the below.
select convert(varchar(10),[Date],23) as [Date]
, case when datepart(hour, [Time])> 12
then (datepart(hour, [Time])- 12)
else datepart(hour, [Time]) End as Hour
, datepart(minute, [Time])as Minutes,
FROM [SQLIOT].[dbo].[ZEPB_CaseLog]
Given you are interested in formatting, i.e. display, then lets use strings.
declare #Time time = '09:33:44 pm'
select #Time [Original]
, format(cast(#Time as datetime),N'hh') [Format Hour] -- SQL Server 2012 + (format works differently for time and datetime - datetime is required here)
, case when len(convert(varchar(32),#Time,109)) > 17 then convert(varchar(2),#Time,109) else '0' + convert(varchar(1),#Time,109) end [Convert Hour] -- SQL Server pre-2012
-- If you ultimatly want the hours and minutes together then do it as one
, format(cast(#Time as datetime),N'hh\:mm') [Format hour:min] -- SQL Server 2012 + (format works differently for time and datetime - datetime is required here)
, case when len(convert(varchar(32),#Time,109)) > 17 then convert(varchar(5),#Time,109) else '0' + convert(varchar(4),#Time,109) end [Convert hour:min] -- SQL Server pre-2012
-- And if you want am/pm
, format(cast(#Time as datetime),N'hh\:mm tt') [Format hour:min tt] -- SQL Server 2012 + (format works differently for time and datetime - datetime is required here)
, case when len(convert(varchar(32),#Time,109)) > 17 then convert(varchar(5),#Time,109) else '0' + convert(varchar(4),#Time,109) end -- SQL Server pre-2012
+ case when #Time >= '12:00:00' then ' PM' else ' AM' end [Convert hour:min tt] -- SQL Server pre-2012
Returns:
Original Format Hour Convert Hour Format hour:min Convert hour:min Format hour:min tt Convert hour:min tt
21:33:44.0000000 09 09 09:33 09:33 09:33 PM 09:33 PM
If you were interested in adding/subracting dates then use dateadd.
Related
My Input :
2020-01-01 09:01:00.000
2020-01-03 18:01:00.000
My Output Should be like :
9 AM
6 PM
You can use the format function like this as long as you're using SQL Server 2012 or later.
SELECT FORMAT(GETDATE(),'h tt') AS MyTime
Note the use of the lower case 'h' which gives a 12 hour clock with no leading zero, 'HH' gives a two digit 24 hour clock so you end up with output like '16 PM'.
If the input is a date-related type, you can use FORMAT with the appropriate format string, eg :
SELECT FORMAT(getdate(), 'HH:mm tt')
For me, this returns :
12:51 PM
Just HH tt returns the desired string, in this case :
12 PM
You can do conversion :
SELECT CONVERT(varchar(10), CONVERT(TIME, '2020-01-01 09:01:00.000'), 100)
I would do:
SELECT V.YourColumn,
LTRIM(SUBSTRING(ca.v,13,2) + ' ' RIGHT(ca.v,2))
FROM (VALUES(CONVERT(Datetime2(3),'2020-01-01 09:01:00.000')),
(CONVERT(Datetime2(3),'2020-01-03 18:01:00.000')))V(YourColumn)
CROSS APPLY (VALUES(CONVERT(varchar(30),V.YourColumn,0))) ca(v);
In the CROSS APPLY I convert the datetime to the format MMM d YYYY h:mmAM/PM, then you can use SUBSTRING/LEFT to get the bits you want.
An alternative approach would be to use DATEPART:
SELECT CONCAT(CASE DATEPART(HOUR,V.YourColumn) WHEN 12 THEN 12 WHEN 0 THEN 12 ELSE DATEPART(HOUR,V.YourColumn) % 12 END,' ', RIGHT(CONVERT(varchar(30),V.YourColumn,0),2))
FROM (VALUES(CONVERT(Datetime2(3),'2020-01-01 09:01:00.000')),
(CONVERT(Datetime2(3),'2020-01-03 00:01:00.000')),
(CONVERT(Datetime2(3),'2020-01-03 18:01:00.000')))V(YourColumn);
Because 12 % 12 and 0 % 12 both = 0, then you need the CASE expression to handle those.
Your query.
SELECT convert(VARCHAR(30), cast('2020-01-01 09:01:00.000' AS DATETIME), 100)
,convert(VARCHAR(30), cast('2020-01-03 18:01:00.000' AS DATETIME), 100)
I'm converting from SQL Oracle to Microsoft SQL Server
>= trunc(current_date, 'DD') converting to >= CONVERT(DATE, GETDATE())
But it's not giving me the same result.
My question is what does trunc(current_date, 'DD') look like (What format?)? I'm not able to test it as the old database has been deleted.
Should the conversion be convert(varchar, getdate(), 105) instead?
if you are going to store this as string type in mssql, this is the nearest datetime format that I can think of.
SELECT CONVERT(VARCHAR(50),DATEADD(day, DATEDIFF(day, '19000101', GETDATE()), '19000101'), 126) + 'Z'
Trunc in oracle will always return date as an output.
Trunc by DD will truncate time portion and give the floor date, not the nearest date.
So consider that your_date is 24-sep-2019 05:30:00 PM.
Select trunc(your_date, 'DD') from dual
Will give following result
24-sep-2019
-- see time portion is truncated
-- and return type is also date
Now, you can convert this query accordingly in mssql.
Cheers!!
In Sql Server for following query you get the out put as '25 Sep 2019'
select convert(varchar, getdate(), 106)
In Oracle to get the same date output as '25 Sep 2019' you can use the below query.
SELECT
TO_CHAR(
TRUNC(TO_DATE( '25 Sep 2019 15:35:32', 'DD Mon yyyy HH24:MI:SS' )),'DD Mon yyyy')
FROM
dual;
In the script below, How do I convert the column HHMM to HH:MM and
convert it to 12hr clock?
The column is a char(4) and format 0000 - 2359.
Just want time or text with a colon(:), no date.
Please note that I do not want date or seconds. How do I do this?
Thank you for your help!
Select
th.[store] as 'My Store',
---> this column th.hhmm as Time, (I need this to format hh:mm)
emp.[First Name],
td.Type as [Total],
Count(*) as Qty,
Convert(varchar(50),SUM(Amount1),1) AS Amount
Try this. It uses LEFT and RIGHT functions
declare #time char(4)='2359'
select left(convert(varchar(100),cast(concat(left(#time,2),':',right(#time,2)) as time),9),5)+
' '+
right(convert(varchar(100),cast(concat(left(#time,2),':',right(#time,2)) as time),9),2)
SQL Server:
right('00' + cast((cast(substring(th.hhmm, 1, 2) as int) - 1) % 12 + 1 as varchar(2)), 2) +
':' + substring(th.hhmm, 3, 2) +
case when cast(substring(th.hhmm, 1, 2) as int) >= 12 then 'pm' else 'am' end
You can use substr to do this in Oracle.
select substr(th.hhmm,1,2)||':'||substr(th.hhmm,3,2) from t
In SQL Server, you can use substring
select substring(th.hhmm,1,2)+':'+substring(th.hhmm,3,2) from t
Fiddle with sample data
Edit:
select
case when cast(substring(th.hhmm,1,2) as int) < 12 then
case when cast(substring(th.hhmm,1,2) as int) = 0 then
'12'+':'+substring(th.hhmm,3,2)+' AM'
else substring(th.hhmm,1,2)+':'+substring(th.hhmm,3,2)+' AM' end
when cast(substring(th.hhmm,1,2) as int) > 12 then
cast(substring(th.hhmm,1,2)%12 as varchar(2)) +':'+substring(th.hhmm,3,2)+' PM'
when cast(substring(th.hhmm,1,2) as int) = 12 then
substring(th.hhmm,1,2)+':'+substring(th.hhmm,3,2)+' PM'
end
I want to get only hh:mm from date.
How I can get this?
I have tried this :
CONVERT(VARCHAR(8), getdate(), 108)
Just use the first 5 characters...?
SELECT CONVERT(VARCHAR(5),getdate(),108)
You can easily use Format() function instead of all the casting for sql 2012 and above only
SELECT FORMAT(GETDATE(),'hh:mm')
This is by far the best way to do the required conversion.
Another method using DATEPART built-in function:
SELECT cast(DATEPART(hour, GETDATE()) as varchar) + ':' + cast(DATEPART(minute, GETDATE()) as varchar)
If you want to display 24 hours format use:
SELECT FORMAT(GETDATE(),'HH:mm')
and to display 12 hours format use:
SELECT FORMAT(GETDATE(),'hh:mm')
Following code shows current hour and minutes in 'Hour:Minutes' column for us.
SELECT CONVERT(VARCHAR(5), GETDATE(), 108) +
(CASE WHEN DATEPART(HOUR, GETDATE()) > 12 THEN ' PM'
ELSE ' AM'
END) 'Hour:Minutes'
or
SELECT Format(GETDATE(), 'hh:mm') +
(CASE WHEN DATEPART(HOUR, GETDATE()) > 12 THEN ' PM'
ELSE ' AM'
END) 'Hour:Minutes'
The following works on 2008R2+ to produce 'HH:MM':
select
case
when len(replace(replace(replace(right(cast(getdate() as varchar),7),'AM',''),'PM',''),' ','')) = 4
then '0'+ replace(replace(replace(right(cast(getdate() as varchar),7),'AM',''),'PM',''),' ','')
else replace(replace(replace(right(cast(getdate() as varchar),7),'AM',''),'PM',''),' ','') end as [Time]
You can cast datetime to time
select CAST(GETDATE() as time)
If you want a hh:mm format
select cast(CAST(GETDATE() as time) as varchar(5))
Here is syntax for showing hours and minutes for a field coming out of a SELECT statement. In this example, the SQL field is named "UpdatedOnAt" and is a DateTime. Tested with MS SQL 2014.
SELECT Format(UpdatedOnAt ,'hh:mm') as UpdatedOnAt from MyTable
I like the format that shows the day of the week as a 3-letter abbreviation, and includes the seconds:
SELECT Format(UpdatedOnAt ,'ddd hh:mm:ss') as UpdatedOnAt from MyTable
The "as UpdatedOnAt" suffix is optional. It gives you a column heading equal tot he field you were selecting to begin with.
TO_CHAR(SYSDATE, 'HH')
I used this to get the current hour in apex PL/SQL
I have this query
select CONVERT(varchar(5), tdate ,108) AS [Time] from table
which gives me the time in 24 hour format( military)
I wanted to convert it into a 12 hour format so i tried the query below
select SUBSTRING(CONVERT(VARCHAR, tdate, 100),13,2) + ':'
+ SUBSTRING(CONVERT(VARCHAR, tdate, 100),16,2) + ''
+ SUBSTRING(CONVERT(VARCHAR, tdate, 100),18,2) AS T
from table
and i get the 12 hour format but I am just curious if there is a shorter or better way of doing it. any help?
If you want to convert the current datetime for example:
SELECT CONVERT(VARCHAR, getdate(), 100) AS DateTime_In_12h_Format
Instead of getdate() you can put your desired column in a query (such as tdate in your example). If you want JUST the time in 12h and not the date and time use substring/right to separate them. It seems that you already know how to =).
This page lists every datetime conversion. It's really handy if you need other types of conversions.
This will return just the time, not the date.
SELECT RIGHT(CONVERT(VARCHAR, getdate(), 100), 7) AS time
For your table data:
select RIGHT(CONVERT(varchar, tdate ,100), 7) AS [Time] from table
Below code will return only time like 10:30 PM
SELECT FORMAT(CAST(getdate() AS DATETIME),'hh:mm tt') AS [Time]
Get date of server
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), GETDATE(), 100), 7))
or
If it is stored in the table
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), datename, 100), 7))
Result:
11:41AM
ifnull(date_format(at.date_time,'%d/%m/%Y'),"") AS date_time,
ifnull(time_format(at.date_time ,'%h:%i:%s'),"") AS date_time
This is how a SQL procedure looks...(for separating date and time)..there is no need of a special column for time/date....
Note:if H instead of h it will show the "hour in 24 hour" format