Solved

SQL  - Add numbers to a literal in query

Posted on 2016-09-23
8
29 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
Comment Utility
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 39

Expert Comment

by:lcohan
Comment Utility
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:
ScottPletcher earned 500 total points
Comment Utility
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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 

Author Comment

by:donnie91910
Comment Utility
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
Comment Utility
Scott Pletcher - i'll try it
0
 

Author Comment

by:donnie91910
Comment Utility
Scott Pletcher can the code you sent me be modified for Toad Oracle?
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
What is your DB? Oracle or SQL Server?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Server 2008 Cluster Fail-over Errors 5 40
SQL Script to find duplicates 16 19
BULK INSERT most recent CSV 19 17
Azure SQL DB? 3 13
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now