Nevron Gauge for SharePoint
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » SQL Server » Time format
       
Author Reply
nishant ranjan
posted 115 posts
since May 30, 2010 
from

Time format

  Posted on: 14 Feb 2012       
select left(convert(varchar, online_time,109),4)+' '+ right(convert(varchar, online_time,109),2) as online_time from time


above is my query

i want output as
04:30 am  4:30 am will do(if initial 0 is removed no prob)
09:45 pm
11:15 am

my prob is that above query is giving right output for time before 10 am/pm but after 9:59 am/pm time is wrong i.e, it displays

11:3 pm instead of 11:30 pm
and give right for
4:40 pm

how i can do that


 
nishant
Mark as "Accepted Answer" if it helps you.
Vulpes
posted  5419 posts
since  Feb 28, 2011 
from 

 Re: time format
  Posted on: 14 Feb 2012        0  
Off the top of my head, I'd have thought you should be getting the leftmost 5 rather than 4 characters as the ':' will count as well:

select left(convert(varchar, online_time,109),5)+' '+ right(convert(varchar, online_time,109),2) as online_time from time
nishant ranjan
posted  115 posts
since  May 30, 2010 
from 

 Re: time format
  Posted on: 15 Feb 2012        0  
well.. this doesnot solve my problem
if time is 9:00:00  in datebase then output is 9:00: am

in my database time is stored in this format
09:00:00
23:05:00
13:30:00

time format is wrong for time before 10 am
nishant
Mark as "Accepted Answer" if it helps you.
Suthish Nair
posted  4906 posts
since  Jul 09, 2009 
from  India

 Re: time format
  Posted on: 15 Feb 2012        0  

 try..
 
 SELECT CONVERT(varchar(5),right('01/01/2012 11:30', 5),108)
SELECT CONVERT(varchar(5),GETDATE(),114)  SELECT CONVERT(varchar(5),GETDATE(),114)

Edited:

SELECT CONVERT(varchar(5),left('09:00:00', 5),108)
SELECT CONVERT(varchar(5),left('23:05:00', 5),108)
SELECT CONVERT(varchar(5),left('13:30:00', 5),108)

http://www.sqlusa.com/bestpractices/datetimeconversion/
nishant ranjan
posted  115 posts
since  May 30, 2010 
from 

 Re: Time format
  Posted on: 15 Feb 2012        0  
well.. in this i have problem that i want to show time in 12 hour format.... not in 24 hour format. using 108 i cant display 11:30 PM ... it displays 23:30 PM
nishant
Mark as "Accepted Answer" if it helps you.
Abhimanyu Kumar Vatsa
posted  598 posts
since  May 01, 2010 
from 

 Re: Time format
  Posted on: 15 Feb 2012        0  
SELECT
SUBSTRING(CONVERT(CHAR(26), GETDATE(), 9), 12, 6)
GO

--OUTPUT = 11:30



and if you want to add AM or PM then


SELECT
SUBSTRING(CONVERT(CHAR(26), GETDATE(), 9), 12, 6) + '' +
SUBSTRING(CONVERT(CHAR(26), GETDATE(), 9), 25, 2) COLNAME
GO

--OUTPUT = 11:30AM

at the place of '' you can add spaces or any separator.
ADCA BCA  MINDCRACKER MVP
Website : http://www.itorian.com 
http://www.microsoftskill.in/
nishant ranjan
posted  115 posts
since  May 30, 2010 
from 

 Re: Time format
  Posted on: 15 Feb 2012        0  
SELECT
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 12, 6) + '' +
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 25, 2) from time

wrong output

00000p
nishant
Mark as "Accepted Answer" if it helps you.
Vulpes
posted  5419 posts
since  Feb 28, 2011 
from 

 Re: Time format
  Posted on: 15 Feb 2012        0  
The problem seems to be that, if the time is before 10:00, it loses the initial zero when converting to varchar. Consequently, when you take the 5 leftmost characters you get stuff like 9:00:

What is the actual type of online_time?
Abhimanyu Kumar Vatsa
posted  598 posts
since  May 01, 2010 
from 

 Re: Time format
  Posted on: 15 Feb 2012        0  
don't use 'from time' at the end. Just use

SELECT
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 12, 6) + '' +
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 25, 2) TIME
GO



'TIME' is just a name, not any column from table and it can be anything.


Please check.
ADCA BCA  MINDCRACKER MVP
Website : http://www.itorian.com 
http://www.microsoftskill.in/
nishant ranjan
posted  115 posts
since  May 30, 2010 
from 

 Re: Time format
  Posted on: 15 Feb 2012        0  
@Vulpes
yup thats exactly the problem is... its not reading initial 0
online_time time(7)


@Abhimanyu  
SELECT
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 12, 6) + '' +
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 25, 2)

giving error invalid column name
nishant
Mark as "Accepted Answer" if it helps you.
Abhimanyu Kumar Vatsa
posted  598 posts
since  May 01, 2010 
from 

 Re: Time format
  Posted on: 15 Feb 2012        0  
look at my reply carefully friend, you forgot to mention column name.

SELECT
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 12, 6) + '' +
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 25, 2) TIME
GO


Look the bold and underlined word, you need to write any word there to represent.

I am already using this on my system.


ADCA BCA  MINDCRACKER MVP
Website : http://www.itorian.com 
http://www.microsoftskill.in/
nishant ranjan
posted  115 posts
since  May 30, 2010 
from 

 Re: Time format
  Posted on: 15 Feb 2012        0  
@Abhimanyu 
still there is problem....
SELECT
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 12, 6) + '' +
SUBSTRING(CONVERT(CHAR(26), online_time, 9), 25, 2)  time

where time is my table name
i even tried with abc, online_time  etc in place of time

if i dont give table name anywhere then from which tble it will retrieve online_time column
nishant
Mark as "Accepted Answer" if it helps you.
Abhimanyu Kumar Vatsa
posted  598 posts
since  May 01, 2010 
from 

 Re: Time format
  Posted on: 15 Feb 2012        0  


Look at image, that's working on my system. If you are still getting error, undoubtedly there is some issue with your "online_time".

Can you check using GETDATE() instead of "online_time"?
ADCA BCA  MINDCRACKER MVP
Website : http://www.itorian.com 
http://www.microsoftskill.in/
Vulpes
posted  5419 posts
since  Feb 28, 2011 
from 

 Re: Time format
  Posted on: 15 Feb 2012        0  
The time(7) datatype has a length of 16 characters including the fractional seconds part.

Rather than converting to varchar, I'd try converting to the fixed size type char(16) to see whether that will preserve any leading zero. 
Suthish Nair
posted  4906 posts
since  Jul 09, 2009 
from  India

 Re: Time format
  Posted on: 15 Feb 2012   Accepted Answer     0  

 I already given the solution... but you must do some more RnD for final o/p...

 SELECT
 REPLICATE('0',2-LEN(DATEPART(HH,RIGHT(CONVERT(CHAR(17),CONVERT(datetime,  CAST('09:00:00' AS TIME), 100), 100),5)))) +
 LTRIM(RIGHT(CONVERT(CHAR(17),CONVERT(datetime,  CAST('09:00:00' AS TIME), 100), 100),5)) 
 
SELECT
 REPLICATE('0',2-LEN(DATEPART(HH,RIGHT(CONVERT(CHAR(17),CONVERT(datetime,  CAST('23:05:00' AS TIME), 100), 100),5)))) +
 LTRIM(RIGHT(CONVERT(CHAR(17),CONVERT(datetime,  CAST('23:05:00' AS TIME), 100), 100),5))
 
SELECT
 REPLICATE('0',2-LEN(DATEPART(HH,RIGHT(CONVERT(CHAR(17),CONVERT(datetime,  CAST('13:30:00' AS TIME), 100), 100),5)))) +
 LTRIM(RIGHT(CONVERT(CHAR(17),CONVERT(datetime,  CAST('13:30:00' AS TIME), 100), 100),5)) 

Suthish Nair
posted  4906 posts
since  Jul 09, 2009 
from  India

 Re: Time format
  Posted on: 15 Feb 2012        0  
Yes, Vulpes was right.. Edited and changed the datatype to char..
nishant ranjan
posted  115 posts
since  May 30, 2010 
from 

 Re: Time format
  Posted on: 16 Feb 2012        0  
@Suthis Nair
thanks replicate function is working

@Vulpes  &  @Suthis Nair

about changing to char, if u r talking abt below code than its nt wrking

SELECT left(CONVERT(char(5),'09:00:00',109),5)
SELECT left(CONVERT(char(5),'23:05:00',109),5)
SELECT left(CONVERT(char(5),'13:30:00',109),5)
               
                      nt converting to 12 hr fmt & nt evn in 108 format

nishant
Mark as "Accepted Answer" if it helps you.
nishant ranjan
posted  115 posts
since  May 30, 2010 
from 

 Re: Time format
  Posted on: 16 Feb 2012        0  
@Abhimanyu

getdate() is working for me too...
but online_time is not working i.e., when i replace getdate with online_time its not wrking

note :- online_time is column in "time" table of datatype time(7)
nishant
Mark as "Accepted Answer" if it helps you.
Vulpes
posted  5419 posts
since  Feb 28, 2011 
from 

 Re: Time format
  Posted on: 16 Feb 2012        0  
What I had in mind was this:

select left(convert(char(16), online_time),5) + ' ' + right(convert(varchar, online_time,109),2)

but, if Suthish's suggestion is working for your online_time, I'd go with that.
nishant ranjan
posted  115 posts
since  May 30, 2010 
from 

 Re: Time format
  Posted on: 16 Feb 2012        0  
@Vulpes
yup i am doing with Suthish's suggestion

as per your last suggestion

select left(convert(char(16), online_time),5) + ' ' + right(convert(varchar, online_time,109),2) from time

again i hv problem of data in 24 hour format which i want to be in 12 hour format


nishant
Mark as "Accepted Answer" if it helps you.
       
Nevron Gauge for SharePoint
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved