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

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

Related

having a time with 2 digits when using DATEPART [duplicate]

I want to add a leading zero before time in (12 Hrs Format)
Example:
SELECT RIGHT('0000000'+CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0), 100), 7)
The result is 1:00PM
I am expected result like 01:00PM
But, when I try below code it works
SELECT RIGHT('0000000'+'1:00PM',7)
I want to know why the first query not given the expected result.
Try with this
SELECT FORMAT (getdate(), 'hh:mmtt') as time
The result is: 07:30AM
SELECT FORMAT (CONVERT(DATETIME, '01:00PM', 0), 'hh:mmtt') as time
The result is : 01:00PM
If you run:
select '"' + CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0), 100) + '"'
You will get:
"Jan 1 1900 1:00PM"
I'm not sure why you think that prepending '0000000' and taking the last seven characters would do what you want.
It is a little hard to figure out what you really want, because you are starting with a constant that has the format you want.
If you want a zero-padded 12-hour time format, you can use:
select right('0' + convert(varchar(255), convert(time, getdate()), 100), 7)
Just try it step by step to see what is happening:
SELECT CONVERT(DATETIME, '01:00PM', 0)
--> 1900-01-01T13:00:00Z
SELECT CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0))
--> Jan 1 1900 1:00PM
SELECT '0000000'+CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0))
--> 0000000Jan 1 1900 1:00PM
And the seven rightmost letters of the last expression are, obviously, 1:00PM.
As others have suggested, use FORMAT (SQL Server 2012 and above) instead:
SELECT FORMAT(CONVERT(DATETIME, '01:00PM', 0), 'hh:mmtt')
--> 01:00PM
This part of the code:
CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0), 100)
will give you this result: Jan 1 1900 1:00PM
This part:
CONVERT(DATETIME, '01:00PM', 0)
will return this: 1900-01-01 13:00:00.000
What is the goal here ?
I agree with the option provided from #SANDEEP answer.
Check the demo

Leading Zero Before Time in SQL Server

I want to add a leading zero before time in (12 Hrs Format)
Example:
SELECT RIGHT('0000000'+CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0), 100), 7)
The result is 1:00PM
I am expected result like 01:00PM
But, when I try below code it works
SELECT RIGHT('0000000'+'1:00PM',7)
I want to know why the first query not given the expected result.
Try with this
SELECT FORMAT (getdate(), 'hh:mmtt') as time
The result is: 07:30AM
SELECT FORMAT (CONVERT(DATETIME, '01:00PM', 0), 'hh:mmtt') as time
The result is : 01:00PM
If you run:
select '"' + CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0), 100) + '"'
You will get:
"Jan 1 1900 1:00PM"
I'm not sure why you think that prepending '0000000' and taking the last seven characters would do what you want.
It is a little hard to figure out what you really want, because you are starting with a constant that has the format you want.
If you want a zero-padded 12-hour time format, you can use:
select right('0' + convert(varchar(255), convert(time, getdate()), 100), 7)
Just try it step by step to see what is happening:
SELECT CONVERT(DATETIME, '01:00PM', 0)
--> 1900-01-01T13:00:00Z
SELECT CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0))
--> Jan 1 1900 1:00PM
SELECT '0000000'+CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0))
--> 0000000Jan 1 1900 1:00PM
And the seven rightmost letters of the last expression are, obviously, 1:00PM.
As others have suggested, use FORMAT (SQL Server 2012 and above) instead:
SELECT FORMAT(CONVERT(DATETIME, '01:00PM', 0), 'hh:mmtt')
--> 01:00PM
This part of the code:
CONVERT(VARCHAR(20), CONVERT(DATETIME, '01:00PM', 0), 100)
will give you this result: Jan 1 1900 1:00PM
This part:
CONVERT(DATETIME, '01:00PM', 0)
will return this: 1900-01-01 13:00:00.000
What is the goal here ?
I agree with the option provided from #SANDEEP answer.
Check the demo

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

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')

Formatting a date/time in SQL Server 2005

I have a datetime field in a SQL Server 2005 table that has values like this:
2012-04-23 09:00:00.000
2012-04-23 14:00:00.000
The minutes, seconds, and microseconds are always zero.
I need to display a "time slot" (basically, the time plus one hour) like this:
2012/04/23 09:00 AM - 10:00 AM
2012/04/23 02:00 PM - 03:00 PM
I got what I needed using this:
SELECT SUBSTRING(CONVERT(VARCHAR, TimeSlot, 20), 1, 10) + ' ' +
RIGHT('00000' + LTRIM(SUBSTRING(CONVERT(VARCHAR(24), TimeSlot, 109), 13, 5)), 5) + ' ' +
SUBSTRING(CONVERT(VARCHAR(19), TimeSlot, 100),18,2) + ' - ' +
RIGHT('00000' + LTRIM(SUBSTRING(CONVERT(VARCHAR(24), DATEADD(hh, 1, TimeSlot), 109), 13, 5)), 5) + ' ' +
SUBSTRING(CONVERT(VARCHAR(19), DATEADD(hh, 1, TimeSlot), 100), 18, 2) AS TimeSlot
FROM MyTable
It works, but I feel so dirty.
I know I could do it easier in the code (VB .NET), but assume I have to do it in SQL.
Is there any cleaner way to do this?
Taking the built-in conversions, I would use something like this:
SELECT
convert(varchar, [TimeSlot], 111) + ' ' +
RIGHT(convert(varchar, [TimeSlot], 0), 7) + ' - ' +
RIGHT(convert(varchar, dateadd(hour, 1, [TimeSlot]), 0), 7)
FROM
[MyTable];
this gives you the line you want as
2012/04/23 9:00AM - 10:00AM
in detail:
convert(varchar, #dt, 111) converts the datetime to the Japanese format yy/mm/dd
convert(varchar, #dt, 0) converts the datetime to Apr 23 2012 9:00AM so we can use the time part
dateadd(hour, 1, #dt) adds one hour to the current datetime value
you can test it without tables with:
DECLARE #dt DATETIME
SET #dt = '2012-04-23 09:00:00'
PRINT convert(varchar, #dt, 111) + ' ' +
RIGHT(convert(varchar, #dt, 0), 7) + ' - ' +
RIGHT(convert(varchar, dateadd(hour, 1, #dt), 0), 7);
The built in date/time formats in SQL Server will only format to an actual date format, so you're up for custom formatting anyway. If you're feeding SSRS with this then you might be able to use an expression to format it within SSRS. Otherwise you're up for formatting it with the query using something like the technique you're describing.
If you're actually displaying it with a VB app then you could do that client-side.
If your date/time values are always on the hour or some consistent interval you could also make a reference table keyed on the time with the formatted interval stored as a varchar column and use that. That would save you in-lining the formatting expression into every query that needed to produce the interval description.
FollowingConcernedOfTunbridgeW's suggestion, if you maintain a two-column table with a primary key int column [hr] that goes from 0 to 23 and a char(20) column [rng] containing the space-prefixed range string for each hour
' 12:00 AM - 01:00 AM' (in the row where hr = 0)
...
' 11:00 PM - 12:00 AM' (in the row where hr = 23)
this simply query should produce what you want:
select
convert(char(10),TimeSlot, 111) + rng as TimeRange
from T join RangesByHr
on datepart(hour,TimeSlot) = hr;

Converting datetime format to 12 hour

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