|
INTERVIEW QUESTIONS
DATABASE
MS SQL SERVER
DETAILS
Question: How To Locate and Take Substrings with CHARINDEX() and SUBSTRING() Functions?
Answer: Transact-SQL is not a language designed for manipulating strings, but it does have two simple functions to locate and take substrings: CHARINDEX() and SUBSTRING(). The tutorial exercise below assumes two given strings: 'Pages: 18' and 'Words: 3240'. The objective is to calculate the number of words per page. Read the script below to see how this is done by using CHARINDEX() and SUBSTRING() functions:
DECLARE @sPages VARCHAR(40), @sWords VARCHAR(40); SET @sPages = 'Pages: 18'; SET @sWords = 'Words: 3240'; SET @sPages = SUBSTRING(@sPages, CHARINDEX(':', @sPages)+1, 20); SET @sWords = SUBSTRING(@sWords, CHARINDEX(':', @sWords)+1, 20); PRINT 'Number of words per page: ' + CONVERT(VARCHAR(20), CONVERT(INT, @sWords)/CONVERT(INT, @sPages)); GO Number of words per page: 180
If you are a PHP developer, you can get this done in a much quick way.
|
|
|
Category |
MS SQL Server Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.2) By 8551 users |
Added on |
9/23/2014 |
Views |
71877 |
Rate it! |
|
|
Question:
How To Locate and Take Substrings with CHARINDEX() and SUBSTRING() Functions?
Answer:
Transact-SQL is not a language designed for manipulating strings, but it does have two simple functions to locate and take substrings: CHARINDEX() and SUBSTRING(). The tutorial exercise below assumes two given strings: 'Pages: 18' and 'Words: 3240'. The objective is to calculate the number of words per page. Read the script below to see how this is done by using CHARINDEX() and SUBSTRING() functions:
DECLARE @sPages VARCHAR(40), @sWords VARCHAR(40); SET @sPages = 'Pages: 18'; SET @sWords = 'Words: 3240'; SET @sPages = SUBSTRING(@sPages, CHARINDEX(':', @sPages)+1, 20); SET @sWords = SUBSTRING(@sWords, CHARINDEX(':', @sWords)+1, 20); PRINT 'Number of words per page: ' + CONVERT(VARCHAR(20), CONVERT(INT, @sWords)/CONVERT(INT, @sPages)); GO Number of words per page: 180
If you are a PHP developer, you can get this done in a much quick way. Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
How To Insert New Line Characters into Strings?
|
View Answer
|
|
What Are the Character String Functions Supported by SQL Server 2005?
|
View Answer
|
|
How To Convert a Unicode Strings to Non-Unicode Strings?
|
View Answer
|
|
What Happens When Unicode Strings Concatenate with Non-Unicode Strings?
|
View Answer
|
|
How To Concatenate Two Character Strings Together?
|
View Answer
|
|
How To Generate Random Numbers with the RAND() Function in MS SQL Server?
|
View Answer
|
|
How To Round a Numeric Value To a Specific Precision?
|
View Answer
|
|
How To Convert Numeric Values to Integers in MS SQL Server?
|
View Answer
|
|
What Are the Mathematical Functions Supported by SQL Server 2005?
|
View Answer
|
|
What Happens When Converting Big Values to NUMERIC Data Types?
|
View Answer
|
|
What Happens When Converting Big Values to Integers?
|
View Answer
|
|
How To Convert Character Strings into Numeric Values?
|
View Answer
|
|
How To How To Convert Numeric Expression Data Types using the CONVERT() Function??
|
View Answer
|
|
How To Convert Numeric Expression Data Types using the CAST() Function?
|
View Answer
|
|
How To Convert Numeric Expression Data Types by Assignment Operations?
|
View Answer
|
|
How To Convert a Numeric Expression from One Data Type to Another?
|
View Answer
|
|
What Is an Expression in MS SQL Server?
|
View Answer
|
|
What Are the Underflow and Overflow Behaviors on FLOAT Literals?
|
View Answer
|
|
How REAL and FLOAT Literal Values Are Rounded?
|
View Answer
|
|
How Extra Digits Are Handled with NUMERIC Data Type Literals?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All MS SQL Server Interview Questions & Answers - Exam Mode /
Learning Mode
|