SqlShare.com Logo
 
Skip Navigation Links
Home
Video List
Events
Classes
About Us
Login / Register
Subscribe RSS Feed 

Takes Query Plan Analysis and Performance Monitoring to a New Level

Using the STR Function

STR (pronounced "string") is a built-in TSQL function to convert numbers to strings or text. It has some interesting features you'll want to see, and we'll contrast it with using CONVERT.

Duration:
5 mins 42 secs
Skill Level:
100
Rating:
4.39 out of 5
Publish Date:
November 02, 2010
Using the STR Function You must be logged in to view this video.  
Bookmark and Share
 
1=Poor, 3=Good, 5=Excellent

About the Author

Image of Andy Warren
Andy Warren is a software trainer focusing on SQL Server, a member of the PASS Board of Directors, and a principal in this site - SQLShare.com.

References

There are no downloads or recommended reading links for this video

Comments
John Piraino on 11/2/2010
Thank You!

John on 11/2/2010
hmmmm didn't know about that function . . . must read more

George on 11/2/2010
very informative. it reminds me of things i rarely use....thanks so much

Pramod on 11/2/2010
Thanks for lesson.

FELIX on 11/2/2010
Excellent video

Jeff Moden on 11/2/2010
I can't rate this one because of a major conflict. You're an outstanding presenter and your demonstrations are clean, simple, and well explained. However... I can't recommend using STR by itself for formatting. You might want to follow this up with some of the caveats of using the STR function. What most people don’t know is that the STR function first converts the number to a FLOAT. From there on, you can suffer all the same problems that you do with the FLOAT data-type… --===== Typical rounding problems same as FLOAT WITH cteGenerateExampleData AS ( SELECT TOP 1000000 SomeNumber = ROW_NUMBER() OVER(ORDER BY (SELECT NULL))*0.0001 FROM sys.all_columns ac1 CROSS JOIN sys.all_columns ac2 ) , cteConversions AS ( SELECT SomeNumber, SomeRound = ROUND(SomeNumber,2), SomeDecimal = CAST(SomeNumber AS DECIMAL(10,2)), SomeSTR = STR(SomeNumber,10,2) FROM cteGenerateExampleData ) SELECT *, ISNULL(CASE WHEN SomeDecimal <> SomeRound THEN 'Bad Decimal Rounding ' END,'') + ISNULL(CASE WHEN SomeStr <> SomeRound THEN 'Bad STR Rounding ' END,'') FROM cteConversions WHERE SomeDecimal <> SomeRound OR SomeStr <> SomeRound ; --===== Accuracy only to 15 digits SELECT 9223372036854775807, STR(9223372036854775807,19) UNION ALL --Incorrect STR value SELECT 922337203685477580, STR(922337203685477580,19) UNION ALL --Incorrect STR value SELECT 92233720368547758, STR(92233720368547758,19) UNION ALL --Incorrect STR value SELECT 9223372036854775, STR(9223372036854775,19) UNION ALL --Incorrect STR value SELECT 922337203685477, STR(922337203685477,19) --Finally! Correct answer! --===== Accuracy only to 15 digits SELECT .9223372036854775807, STR(.9223372036854775807,19,18) UNION ALL --Incorrect STR value SELECT .922337203685477580, STR(.922337203685477580,19,18) UNION ALL --Incorrect STR value SELECT .92233720368547758, STR(.92233720368547758,19,18) UNION ALL --Incorrect STR value SELECT .9223372036854775, STR(.9223372036854775,19,18) UNION ALL --Incorrect STR value SELECT .922337203685477, STR(.922337203685477,19,18) --Finally! Correct answer! --Jeff Moden jbmoden@ameritech.net

manjeet on 11/2/2010
nice video for str and convert

Sumitra on 11/2/2010
Good job on creating videos.

kalapala baburao on 11/2/2010
ok

Mohamed Irshad on 11/3/2010
very good

erhan emre on 11/3/2010
Nice

Dawit on 11/3/2010
Great examples thanks

zahid on 11/3/2010
very good

Todd Burrell on 11/3/2010
It would be interest to have video on the differences between Cast and Convert

Mark Hathaway on 11/3/2010
Very nice!

Bridget on 11/3/2010
Great explanations.

Dennis Allen on 11/3/2010
You bring up a good point with, contrasting with convert(). I am interested in contrasting convert() and cast(). For myself, I had been using convert() in all cases with the thought of the fewer functions I use, the easier to understand my work. Is there a value to using cast() over convert? I can see a clear value with convert() in the case of date and time, but I have not found (other than number of characters typed) a benefit for cast().

Faisal Lodhi on 11/3/2010
Awesome and Very Detail

Iryna on 11/3/2010
Thank you.

Any way to avoid rounding using str function?

Kenneth Wymore on 11/3/2010
thanks for explaining yet another function that I have never used!

Michael Neymit on 11/3/2010
Very good lesson. Thank you!

vin lawrence on 11/4/2010
Knew this anyway but nice to have it confirmed

WChaster on 11/4/2010
I am CONVERTed, it is STR all teh way! Really that is very insightful and your usage ideas are sound.

Jack Pines on 11/4/2010
Thanks for taking the time to touch on convert() as that's the way I've always done this business so now I know the difference and have a new tool for appropriate circumstances.

Might improve with a table column select. select STR(myNumber,6,3) from t

vijay on 11/9/2010
Good demo

Kat on 11/25/2010
Great explanation on the difference between convert and str function.

huruy yohannes on 11/29/2010
Thanks

Daniel Frankel on 6/13/2011
What happens if the number you are converting is over 10 charachters? I am trying to figure out how to convert a number like the following to a string A: 500000000.2358 --> 500,000,000.23 B: -500000000.2358 --> (500,000,000.23)

Michael on 2/8/2012
Good



Must Be Logged In
 

How Do I Become a Video Author? |  Newsletter History

Copyright © Fourdeuce, Inc., 2005-2009. All Rights Reserved | Privacy Policy | Terms & Conditions