Solved

SQL  - Add numbers to a literal in query

Posted on 2016-09-23
8
33 Views
Last Modified: 2016-11-07
i am trying to add numbers to a literal in a select query.  I want to start at a specific number and add to it for each row.  The literal is set.  Now I have something like this:

"NO"1+5 as New_FacID

which i want to look like in the New_FacID field:

NO1
NO2
NO3
NO4
NO5
NO6

because I am selecting 6 records.

Thanks.
0
Comment
Question by:donnie91910
8 Comments
 

Author Comment

by:donnie91910
ID: 41812756
Actually I will be putting in the starting number and then the number will increment automatically for each record in my query.

starting number at 1.  Six records are returned.

Looks like this:
NO1
NO2
NO3
NO4
NO5
NO6

Second Select statement starting number at 7.  Three records are returned.
NO7
NO8
NO9

Thanks.
0
 
LVL 40

Expert Comment

by:lcohan
ID: 41812758
You could get the "relative record number" of each ro by running a statement like below - just replace actual table/column names in it


SELECT id, (SELECT COUNT(*) FROM table_name t2 WHERE t2.id <= t.id) AS rownumber
FROM table_name t
ORDER BY id


OR you can use that and add your specific number to the COUNT(*) to get the Starting position.

and you could concatenate those files like:



SELECT Id + CAST (  (SELECT COUNT(*) FROM table_name t2 WHERE t2.id <= t.id) AS sysname) AS  New_FacID
FROM table_name t
ORDER BY id
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 41812795
Counting all previous rows for every current row would be huge overhead.

Instead add a ROW_NUMBER() and then add that value to the starting value.

DECLARE @prefix varchar(30)
DECLARE @starting_number int
SET @prefix = 'NO'
SET @starting_number = <value_you_entered_for_starting_number>

SELECT
    @prefix + CAST(@starting_number + row_num - 1 AS varchar(10)) AS New_FacID,
    <column_name1>, <column_name2>, ...
FROM (
    SELECT *, ROW_NUMBER() OVER(ORDER BY <table_column>) AS row_num
    FROM table_name
) AS derived
--ORDER BY ...
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:donnie91910
ID: 41812809
how do I hardcode the literal "NO" in my query and have the number concatinate  and increment that I have hardcoded as my starting number and have it increment for the number of rows in my query result set.

Looks like:
Select statement starting number at 7.  Three records are returned.
NO7
NO8
NO9

thanks.
0
 

Author Comment

by:donnie91910
ID: 41812813
Scott Pletcher - i'll try it
0
 

Author Comment

by:donnie91910
ID: 41812856
Scott Pletcher can the code you sent me be modified for Toad Oracle?
0
 
LVL 41

Expert Comment

by:Sharath
ID: 41812886
What is your DB? Oracle or SQL Server?
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question