Solved

T SQL Select a Constant Value and Concatenate with another Field

Posted on 2016-09-20
13
47 Views
Last Modified: 2016-09-26
I am using a proprietary front end application to connect to a SQL Server database.
I want to pad a field with a constant string, by using the code below it works with one minor issue * see below:

SELECT (VAL("123456")+Customer.CustomerID FROM Customer

The return value inserts commas every third character:
123,456,555,555,555,555

I tried a few different changes to the syntax but still getting the same results.   The application I use to connect to the SQL Server Database isn't easy to work with - so it is quite a challenge.  Thanks for any info.
0
Comment
Question by:fjkaykr11
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 10

Assisted Solution

by:HuaMinChen
HuaMinChen earned 83 total points
ID: 41807996
Use money if you need comma to the displayed column, like
SELECT CONVERT(varchar, CAST(987654321 AS money), 1)

Open in new window

0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 251 total points
ID: 41807998
you do NOT want commas?
 so you want to treat the result as a string....

SELECT '123456' + Customer.CustomerID AS StringID
FROM Customer
0
 
LVL 3

Author Comment

by:fjkaykr11
ID: 41808031
Paul thanks for the reply, sorry for the confusion.  That is correct I do not want commas in the return value.
I would like it to be displayed as 123456555555555  instead of 123,456,555,555,555
0
 
LVL 10

Expert Comment

by:HuaMinChen
ID: 41808035
Try
SELECT replace((VAL("123456")+Customer.CustomerID,',','') FROM Customer

Open in new window

0
 
LVL 49

Assisted Solution

by:Ryan Chong
Ryan Chong earned 83 total points
ID: 41808045
what's the format of Customer.CustomerID ?

what Paul commented in ID: 41807998 should return the result you wanted?
1
 
LVL 26

Assisted Solution

by:Zberteoc
Zberteoc earned 83 total points
ID: 41808698
Use this:
SELECT '123456'+cast(Customer.CustomerID AS VARCHAR(25)) AS concat_value FROM Customer

Open in new window

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Author Comment

by:fjkaykr11
ID: 41809809
thanks for all the replies. I might not be able to do this as many functions do not work
in the app I am using (get a lot of errors), including with CAST.
when I tried something like this:
SELECT '123456'+convert(text,18,Customer.CustomerID) AS concat_value FROM Customer
it will bring return a value without errors but still getting the same results (commas after every 3rd character), String did the same.  Using VarChar brings back less values and still has the commas.  I think I will close this out.  Thanks again.
0
 
LVL 3

Author Closing Comment

by:fjkaykr11
ID: 41809813
Thanks
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41810652
Why using CAST to text? Nobody posted that. text as data type is obsolete in SQL server you what I posted, CAST to VARCHAR(size)...
0
 
LVL 3

Author Comment

by:fjkaykr11
ID: 41813344
I tried that method, it didn't work.    I am not using SSMS.   Thanks again.
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41816021
Can you be a bit more specific? Didn't work is not enough, code sample that you ran with error massage would be.
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41816024
Sorry, I just saw that the question had been answered.
0
 
LVL 3

Author Comment

by:fjkaykr11
ID: 41817092
no worries, thanks for the assistance.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

760 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

22 Experts available now in Live!

Get 1:1 Help Now