Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 7,2000
»
T-SQL
»
COVERTING NUMERIC TO STRING, PRESERVING...
COVERTING NUMERIC TO STRING, PRESERVING LEADING ZEROS
Rate Topic
Display Mode
Topic Options
Author
Message
dave-dj
dave-dj
Posted Thursday, October 30, 2008 4:20 AM
Old Hand
Group: General Forum Members
Last Login: Thursday, June 13, 2013 5:09 AM
Points: 380,
Visits: 1,020
Hi all,
thought this might be helpful to someone else.
if you want to convert a numeric value e.g. 01 to a string 01 then this can be done like so:
select '0' + CAST(01 as varchar(2)) - results in 01
if you want to make this a little more generic so you can accomadate a conversion if required for number ranging 1-20 for example, then you could do this
select CAST(RIGHT('0'+ CAST(10 AS VARCHAR(2)),2) - this will result in 010, but using the right command we can strip this back to the two characters we need - 10.
an example of how I have used this is to construct a date from 3 fields:
OPENDATE - a datetime field holding just the date
OPENHOURS - the hour in which a task was completed for the given date
OPENMINUTES - the minute of the hour in which a task was completed for the given date.
the aim of the select statement is to convert
2008-10-27
09
53
to a datetime field of '2008-10-27 09:53:00'
SELECT convert(VARCHAR(30), SUBSTRING(convert(varchar(20), OPENDATE, 120), 0, 11) + ' ' + CAST(RIGHT(' 0'+ CAST(OPENHOURS AS VARCHAR(2)),2) + ':'+ RIGHT(' 0'+ CAST(OPENMINUTES AS VARCHAR(2)),2) + ':00' AS VARCHAR(10))) AS [Open DT]
_____________________________________________________________________________
MCITP: Business Intelligence Developer (2005)
Post #594180
Scalability Doug
Scalability Doug
Posted Thursday, October 30, 2008 10:47 AM
SSChasing Mays
Group: General Forum Members
Last Login: Thursday, April 05, 2012 12:43 PM
Points: 655,
Visits: 6,097
Dave, check out a cleaner, reusable version at
http://www.sqlservercentral.com/Forums/Topic594387-145-1.aspx
DAB
Post #594543
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.