How to get time part from GetDate() As hh:mm tt in SQL Server - sql

this is not duplicate of How to get time part from SQL Server 2005 datetime in 'HH:mm tt' format
be cause all answers of that question returns 12:06PM (without space and need space)
I am trying to get only time part from SQL GETDATE()
and I am trying
SELECT LTRIM(RIGHT(CONVERT(VARCHAR(20), GETDATE(), 100), 7))
but it is returning
12:06PM
I need 12:06 PM (Space before AM or PM)..
search a lot but failed...

For SQL 2012 above:
SELECT FORMAT(GETDATE(), 'h:mm tt', 'en-US')

Try this
SELECT Replace(Replace(LTRIM(RIGHT(CONVERT(VARCHAR(20), GETDATE(), 100), 7)),'PM',' PM'),'AM',' AM')

You can use:
select CONVERT(VARCHAR(5), GETDATE(), 108) + ' ' +
RIGHT(CONVERT(VARCHAR(30), GETDATE(), 9),2)
SQL fiddle: http://sqlfiddle.com/#!6/a7540/2377

Try this:
SELECT substring(CONVERT(varchar(20), GetDate(), 9), 13, 5) +
' ' +
substring(CONVERT(varchar(30), GetDate(), 9), 25, 2)

After A lot Practice Shortest Answer I Get..
SELECT CONVERT(VARCHAR(11),
STUFF(RIGHT(
CONVERT(VARCHAR,GETDATE(),100 ) ,7),
6, 0, ' '))
Thanks All

FORMAT(GETDATE(), 'h:mm tt', 'en-US')

Related

SQL Server - converting date to D-MMM-YYYY format

How can I get the string from GETDATE() in D-MMM-YYYY format, e.g 3 May 2016
If I use CONVERT(VARCHAR, GETDATE(), 106), I would get a leading zero on day which is not what I want.
If you are on SQL Server 2012 or later, use FORMAT:
SELECT FORMAT(GETDATE(), 'd MMM yyyy')
Edit: some of the answers below are just flat-out wrong so I'm adding a solution for older versions of SQL Server. 2005 is the earliest that I can get my hands on:
SELECT CASE
WHEN CONVERT(varchar(20), GETDATE(), 106) LIKE '0%'
THEN SUBSTRING(CONVERT(varchar(20), GETDATE(), 106), 2, 20)
ELSE CONVERT(varchar(20), GETDATE(), 106)
END
SELECT case when left(convert(varchar(20),[DateColumn],106),1) ='0'
then right(convert(varchar(20),[DateColumn],106),len(convert(varchar(20),[DateColumn],106))-1)
else convert(varchar(20),[DateColumn],106)
end
FROM [DB].[dbo].[Table]
Some sample output :
29 Apr 2016
2 Apr 2016
If you cannot use FORMAT (Below SQL Server 2012)
DECLARE #date DATE = '20160503'
SELECT REPLACE(DATEPART(DAY, #date),' 0','') + ' ' +
CONVERT(CHAR(3), #date, 0) + ' ' +
CAST(DATEPART(YEAR, #date) AS CHAR(4))

Convert DateTime Format using Convert function in SQL Server

Can anyone help me to find out below format using Convert function in SQL Server?
DateTime
yyyy/MM/dd hhmm
yyyy/MM/dd hhmmssstt
Time
hh:mm
hh:mm:ss tt
hh:mm tt
Thanks in advance...
This MSDN page here lists all available "styles" for conversion.
If you're using SQL Server 2012 or newer, you can also look at the new FORMAT function for even more flexibility
Try this:
SQL provides different methods to convert date and time.
SELECT CONVERT(VARCHAR(19),GETDATE())
SELECT CONVERT(VARCHAR(10),GETDATE(),10)
SELECT CONVERT(VARCHAR(10),GETDATE(),110)
SELECT CONVERT(VARCHAR(11),GETDATE(),6)
SELECT CONVERT(VARCHAR(11),GETDATE(),106)
SELECT CONVERT(VARCHAR(24),GETDATE(),113)
Try these statements:
SELECT REPLACE(REPLACE(SUBSTRING(CONVERT(VARCHAR(50), GETDATE(), 121), 1, 16), ':', ''), '-', '/')
SELECT REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(50), GETDATE(), 121), ':', ''), '-', '/'), '.', '')
SELECT SUBSTRING(CONVERT(VARCHAR(50), GETDATE(), 114), 1, 5)
SELECT STUFF(SUBSTRING(CONVERT(VARCHAR(50), GETDATE(), 114), 1, 11), 9, 1, ' ')
SELECT STUFF(SUBSTRING(CONVERT(VARCHAR(50), GETDATE(), 114), 1, 11), 6, 4, ' ')
for these results:
2015/07/13 1357
2015/07/13 135723767
13:57
13:57:23 76
13:57 76
I am unable to get exact format which i need with Convert() so i am using Format() now.
Also want to say Format() is poor performance compare to Convert().

How to Show Date with AM/PM instead of specifying time in SQL

is there a way can we convert Datetime to (dd/MM/yyyy AM/PM) format
for example 18/03/2014 AM in SQL
For MYSQL:
select DATE_FORMAT(NOW(), '%d/%m/%Y %p')
For SQL SERVER
SELECT convert(varchar(20), GetDate(), 103) + ' ' + substring(convert(varchar(30), GetDate(), 9), 25, 2);
For Postgres:
SELECT TO_CHAR(NOW(), 'dd/mm/yyyy AM')
For Microsoft SQL Server you can use
select CONVERT(VARCHAR(24), GETDATE(), 100)
I finally found the way
select CONVERT(nvarchar, GETDATE(), 103)
+ CASE WHEN DATEPART(HOUR, GETDATE()) < 12 THEN ' AM'
ELSE ' PM' END

Format SQL DATETIME result like --> 8/25/2011 4:35 PM <-- NOT --> 2011-08-25 16:35:51.000 <--

I have a SQL Query running on SQL Server 2008 R2 that returns a DATETIME column. The current formatting returns this:
2011-08-25 16:35:51.000
and what I would rather see is this:
8/25/2011 4:35 PM
Ideas of how I can modify my SELECT statement? Here a simple statement to work with...
SELECT DtCheckIn from Ticket
Thank you, Andrew
You could do the following:
SELECT convert(varchar, getdate(), 101) + RIGHT(convert(varchar, getdate(), 100), 7)
Resources on date time formatting:
http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/
http://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/
EDIT - it's a little hokey but it should give you what you are looking for.
Basically the first part SELECT convert(varchar, getdate(), 101) gives you your date 08/25/2011.
The second part SELECT convert(varchar, getdate(), 100) gives you your date and time in the format Aug 25 2011 5:35PM. You only want the time portion which is in the format hh:mmAM or hh:mmPM the total number of characters you want is 7. So if you change this to SELECT RIGHT(convert(varchar, getdate(), 100), 8) you will get your time 5:37PM. I used 8 in my RIGHT() to get an extra space. But now you only want your time not AM/PM so add SELECT LEFT(RIGHT(convert(varchar, getdate(), 100), 8), 6) and you will get your hh:mm.
Finally you want your AM/PM SELECT RIGHT(convert(varchar, getdate(), 100), 2) this will get you the last 2 characters.
Put it all together and you get:
SELECT convert(varchar, getdate(), 101)
+ LEFT(RIGHT(convert(varchar, getdate(), 100), 8), 6)
+ ' ' + RIGHT(convert(varchar, getdate(), 100), 2)
Which gives your final product:
08/25/2011 5:39 PM

Convert SQL DateTime format

How can I display a DATETIME value (2010-12-02 15:20:17.000) as 02/12-2010 15:20?
For SQL Server:
select stuff(convert(varchar, getdate(), 105), 3, 1, '/') + ' ' + left(convert(varchar, getdate(), 8), 5)
DateTime is a DateTime is a DateTime - it just holds a date and time and doesn't have any string representation, really.
See the CAST and CONVERT topic in the SQL Server Books Online for details - it shows all supported date formats that SQL Server supports.
For your source format (2010-12-02 15:20:17.000) you could probably use style no. 121
DECLARE #source VARCHAR(50)
SET #source = '2010-12-02 15:20:17.000'
DECLARE #Date DATETIME
SELECT #Date = CONVERT(DATETIME, #source, 121)
SELECT #Date
but your target format is a bit odd..... I don't see any "out of the box" style that would match your needs. You'll need to use some string manipulation code to get that exact format.
Use MSSQL's build-in function to convert datetime to string with format,
SELECT CONVERT(VARCHAR(8), GETDATE(), 1) AS [MM/DD/YY] --2/5/12
SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY] --5/2/2012
You need to create custom function to get various format to use like this;
SELECT dbo.ufn_FormatDateTime(GETDATE(),'YYYY-MM-DD HH:mm:SS tt')
--Output : 2012-02-05 01:58:38 AM
SELECT dbo.ufn_FormatDateTime(GETDATE(),'(dddd) mmmm dd, yyyy hh:mm:ss.fff tt')
--Output : (Sunday) February 05, 2012 01:58:38.723 AM
SELECT dbo.ufn_FormatDateTime(GETDATE(),'dd/MM/yyyy')
--Output : 05/02/2012
SELECT dbo.ufn_FormatDateTime(GETDATE(),'yyyy MMM, dd (ddd) hh:mm:ss tt')
-- Output : 2012 Feb, 05 (Sun) 01:58:38 AM
Get the code snippet from this link.
http://www.tainyan.com/codesnippets/entry-62/sql-server-date-time-format-function.html
http://msdn.microsoft.com/en-us/library/ms189491.aspx
Is this what you're looking for?
Assuming Oracle:
select TO_CHAR(SYSDATE, "dd/mm-yyyy HH24:mi")
from DUAL;
Assuming SQL Server:
select STR(DATEPART(DAY, GETDATE()), 2)
+ '/'
+ STR(DATEPART(MONTH, GETDATE()), 2)
+ '-'
+ STR(DATEPART(YEAR, GETDATE()), 4)
+ ' '
+ STR(DATEPART(HOUR, GETDATE()), 2)
+ ':'
+ STR(DATEPART(MINUTE, GETDATE()), 2);
Little example I use for Germany and Switzerland: dd.mm.yyyy hh:mm
SELECT CONVERT(varchar, GETDATE(), 104) + ' ' + LEFT(CONVERT(varchar, GETDATE(), 108), 5)