[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DATEPART with milliseconds, how to get leading zero

Posted on 2008-10-02
4
Medium Priority
?
993 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
  • 2
3 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

834 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