Professional Documents
Culture Documents
10 - Reasons - Convert - Datatypes in SQL Server
10 - Reasons - Convert - Datatypes in SQL Server
0
SQL Server data types March 13, 2008
CONVERT to format
2
CONVERT lets you convert and format in one step as follows, where style specifies the format:
CONVERT (datatype, expression, style)
Like CAST, datatype must be system-supported, as it won’t work with user-defined data types. Visit
http://msdn2.microsoft.com/en-us/library/ms187928.aspx to view a list of style values.
Figure A
Figure B
Page 1
Copyright ©2008 CNET Networks, Inc. All rights reserved.
For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html
10 reasons to explicitly convert SQL Server data types
CAST to truncate
4
When trying to convert decimal values into
integers, you might start with ROUND. However, that
function doesn’t always get the job done. For
instance, suppose your shipping volume is so great
that your distribution company charges by the
smaller whole unit—forgiving the overage into the
next pound. In other words, if the item weighs 2.5
pounds, the company charges for only 2.
In this case, you’d need to know the shipping weight
and not necessarily the item’s true weight. As you
can see in Figure C, this is one of those times when Figure C
ROUND doesn’t work. It rounds down in some
cases, which works. In other cases, it rounds up,
and you’d pay more money than necessary.
In Figure D, CAST solves the problem. In this
case, converting the weight value to an integer
truncates the decimal portion.
Figure F
Page 2
Copyright ©2008 CNET Networks, Inc. All rights reserved.
For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html
10 reasons to explicitly convert SQL Server data types
Figure H
8 CONVERT datetime to a string
It’s impossible to get rid of the date or the time component in
a datetime data type, but you can work with the date and
time values separately. One way to get the date is to convert
the datetime value to an unformatted string, as shown in
Figure I. The good news is that SQL Server correctly
interprets the unformatted string, regardless of datetime or
language settings, when you convert the string back to
datetime.
Figure I
Page 3
Copyright ©2008 CNET Networks, Inc. All rights reserved.
For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html
10 reasons to explicitly convert SQL Server data types
Figure J
A word of warning
It’s important to note something that seems obvious but that we often forget. Use converted results for only the
purpose intended. It’s just too easy to forget about the conversion and in doing so, to generate subsequent errors that
are hard to debug.
Susan Sales Harkins is an independent consultant and the author of several articles and books on database
technologies. Her most recent book is Mastering Microsoft SQL Server 2005 Express, with Mike Gunderloy, published
by Sybex. Other collaborations with Gunderloy are Automating Microsoft Access 2003 with VBA, Upgrader’s Guide to
Microsoft Office System 2003, ICDL Exam Cram 2, and Absolute Beginner's Guide to Microsoft Access 2003, all
published by Que. Currently, Susan volunteers as the Publications Director for Database Advisors. You can reach her
at ssharkins@gmail.com.
Page 4
Copyright ©2008 CNET Networks, Inc. All rights reserved.
For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html
10 reasons to explicitly convert SQL Server data types
Additional resources
Version history
Version: 1.0
Published: March 13, 2008
TechRepublic downloads are designed to help you get your job done as painlessly and effectively as possible.
Because we're continually looking for ways to improve the usefulness of these tools, we need your feedback.
Please take a minute to drop us a line and tell us how well this download worked for you and offer your
suggestions for improvement.
Thanks!
Page 5
Copyright ©2008 CNET Networks, Inc. All rights reserved.
For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html