Tags

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,


SQL PUZZLE | Multiple ways to convert an Integer value to String | SQL Interview Question

This question has been asked to one of my collegue in SQL SERVER interview. I know this is not a new way but have we explored any other option apart from cast and convert for this.

Well, there are multiple ways to convert an integer value to a string value.

Some of them are-

  • Method 1 | Using CAST function
  • Method 2 | Using Convert function
  • Method 3 | Using STR function
  • Method 4 | Using Implicit Conversion

Method 1 | Using CAST function

1. It is one of the old methods to convert data from one datatype to another

2. It is the most preferred method of conversion from one datatype to another

Examples below-

--


DECLARE @Int AS INT = 25
SELECT CAST (@Int AS VARCHAR(2)) String
GO

--

Method 2 | Using Convert function

1. It is one of the old methods to convert data from one datatype to another

2. This method is widely used for Date type conversion for variety of formats.

Examples below-

--


DECLARE @Int AS INT = 25
SELECT CONVERT(VARCHAR(2),@Int) String
GO

--

Method 3 | Using STR function

1. It is one of the lesser known fact. Str function returns character data converted from numeric data

2. Note that although this is the workaround you should always use CAST/CONVERT in your development code

--


DECLARE @Int AS INT = 25
SELECT STR(25,2,1) String
GO

--

Method 4 | Using Implicit conversion

1. Here we are taking a Varchar Variable and assigning the int varible
to varchar variable.

2. Note that although this is the workaround you should always use CAST/CONVERT in your development code

--


DECLARE @Int AS INT = 25
DECLARE @Str AS VARCHAR(2) = ''
SET @Str = @Int
SELECT @Str String

--

Please add a comment if you have any other or better solution in mind. I would love to learn it. We all need to learn. Thanks in advance.

Pawan Khowal

Pawan is a SQL Server Developer. If you need any help in writing code/puzzle or training please email at – pawankkmr”AT”gmail.com. Meanwhile please go throgh the top pages from his blog.

Page Detail URL
☛ SQL Advance Puzzles https://msbiskills.com/tsql-puzzles-asked-in-interview-over-the-years/
☛ SQL Tricky Queries https://msbiskills.com/sql-puzzles-finding-outputs/
☛ SQL Server Perfomance tuning Articles https://msbiskills.com/sql-performance-tuning/
☛ SQL Server Articles https://msbiskills.com/t-sql/
☛ SQL Interview Questions & Answers https://msbiskills.com/sql-server-interview-questions/

My SQL Groups on Facebook:

1. If you like this post, you may want to join my SQL SERVER Interview Puzzles/Interview Questions on Facebook: https://www.facebook.com/groups/1430882120556342/

2. If you like this post, you may want to join my SQL Server Puzzles on Facebook:
https://www.facebook.com/groups/206594023090781/

My SQL Page on Facebook:

2. For all the updates you may follow my page -> https://www.facebook.com/MSBISkillscom-1602779883299222/

Enjoy !!! Keep Learning

Http://MsbiSkills.com

Advertisements