[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

DATEPART with milliseconds, how to get leading zero

Posted on 2008-10-02
4
Medium Priority
?
989 Views
Last Modified: 2009-01-05
I am trying to pull the milliseconds from a datetime column using CAST(DATEPART(millisecond, qual.time)AS VARCHAR(3))

I need the first two digits of the milliseconds with a leading zero.

Right now if the milliseconds is .090  I am getting .90 back which is incorrect.   I have tried casting to an INT but i get an error that "String 2:2 cannot be cast to an int".  I believe this is from the data in the database which is 1900-01-01 02:02.090  .  For some reason the datepart function and the cast are attemping to convert the entire date instead of just the milliseconds.
0
Comment
Question by:skykingjwc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 22626421
I am pretty sure 90 is correct, but here is how you can get all 3 digits

declare @x varchar(50)
set @x = '1900-01-01 02:02.090'
select substring(@x, charindex( '.', @x)+1, len(@x))
0
 
LVL 4

Author Comment

by:skykingjwc
ID: 22632973
9 tenths of a second is not the same as 9 hundredths of a second.
If you read the second line in the question, I wanted the first two digits (tenths and hundredths) after the decimal.
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 2000 total points
ID: 22633092
ok...

declare @x varchar(50)
set @x = '1900-01-01 02:02.090'
select left(substring(@x, charindex( '.', @x)+1, len(@x)), 2)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question