SQL concatenation with prepending returned data

Status
Not open for further replies.
My SQL query is returning 'A' from one field '15' from another field in the same table.

The values need to be concatenated and then printed in a column in a report.

In that report, the two-digits need to have '00' immediately in front of them so that it appears as A0015.

The day will come when whenever new data is inserted into the database, it will be prepended with '00' at that time, but until then, I need to figure out how to prepend the returned data with '00' in my query.

I have tried the following:

-selecting as varchar
-BatchChar+'00'BatchNumber
-CONCAT(BatchChar, BatchNumber) just to see if I could perform a simple concatenation - I couldn't...

Clearly, I'm missing something.

Any help would be most appreciated.
 

J^M

Ars Scholae Palatinae
815
Subscriptor++
[url=http:/civis/viewtopic.php?p=25629081#p25629081:dvif0082 said:
Deathstar[/url]":dvif0082]
The day will come when whenever new data is inserted into the database, it will be prepended with '00' at that time, but until then, I need to figure out how to prepend the returned data with '00' in my query.
Will anyone remember to do this later or should you add a case statement that checks the length now? :)

What's the error? Which database?
 

Fristie Blade

Ars Tribunus Militum
1,722
Subscriptor++
I don't know which DBMS you're on, which would be helpful to deliver you some relevant syntax.

On SQL Server I d do it like this: cast your integer value (if a positive one) to a varchar, prefix it with 4 (your total length!) zeroes, then take the right 4 characters of that varchar.
Code:
SELECT field1 + right('0000' + cast(field2 as varchar(4)), 4) as NewField FROM Table

On Oracle I d do it like this:
Code:
SELECT field1 || to_char(field2, '0000') as NewField FROM Table
 
Thank you, brodie!!

I wonder if it is a limitation of Microsoft SQL 2005 (though, I really don't know why the following is returned when I try both of the suggestions):

Microsoft SQL Server Management Studio

SQL Execution Error.

Executed SQL Statement: SELECT {fn CONCAT(BatchChar, '00', BatchNumber)} AS NewField FROM tblInvoiceBatchNumbers
Error Source: .Net SqlClient Data Provider
Error Message: The CONCAT function requires 2 argument(s).

When you say that you "couldn't" perform a simple concatenation, what do you mean? You got an error? What was the error?

Pretty much the same as the above. was returned.

On the plus side, however, your suggestion, Fristie Blade, worked flawlessly!!

Thank you so much for the suggestions, everyone!

Is appreciated more than I can say.
 
Were you always specifying CONCAT as a canonical function (i.e. with the {fn CONCAT()})? From my (very) brief googling, it sounds like that may or may not cause an issue.

Weird, indeed.

Nope...only when I tried your code...usually, I can simply use the + and if I need to include any text I can just put single-quotes around it and I almost always get the desired result.

In this new position of mine, I'll be using sql alot, so the more quickly I can get back up to speed, the better...just for reference, the last time I used sql at all was when I learned it for the first time...an educational version of Oracle 8i...my co-worker tells me that SQL Server has much to offer...and I agree.
 
Status
Not open for further replies.