| Author |
Topic |
|
zeeshan13
Constraint Violating Yak Guru
347 Posts |
Posted - 2008-04-22 : 10:04:50
|
| Hi All,I have a table called Table1 which has a field called ProductID (nvarchar type). The records are numeric, and each record may have different length of characters. The maximum length of character is 13. The records are like thisProductID78300056027378649752595991380010072As you can see it has records of different length. In my SELECT a) first I want to make the ProductID length of 13 characters by adding leading zero's, if lenght of characters is already 13 then I will not add any leading zeros. b) Once all records have a length of 13 character then I want to put a dash "-" after first 8 characters. This way the final lenght of characters would be 14. I want to do a) & b) in one single SELECT statement.So my final output for above example would be,00078300-0560200000000-0737800649752-5959900013800-10072Any quick help would be highly appreciated.Thanks,Zee |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-04-22 : 10:10:56
|
| select left(right('0000000000000'+ productID,13),8)+'-'+right('0000000000000'+ProductID,5)Em |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-22 : 10:15:03
|
[code]DECLARE @Sample TABLE (ProductID VARCHAR(200))INSERT @SampleSELECT '7830005602' UNION ALLSELECT '7378' UNION ALLSELECT '64975259599' UNION ALLSELECT '1380010072'SELECT ProductID AS Old, STUFF(RIGHT('0000000000000' + ProductID, 13), 9, 0, '-') AS NewFROM @Sample[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
zeeshan13
Constraint Violating Yak Guru
347 Posts |
Posted - 2008-04-22 : 10:17:39
|
| Thasnks guys. It wroked :).... |
 |
|
|
zeeshan13
Constraint Violating Yak Guru
347 Posts |
Posted - 2008-04-22 : 10:18:00
|
| I am using this one which is also working...select left( right ( replicate('0', 14) + cast(Productid as varchar(14)), 14), 8 ) + '-' + right ( replicate('0', 14) + cast(Productid as varchar(14)), 5) |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-22 : 10:24:19
|
Why CAST?You wrote in orignal post that ProductID is NVARCHAR...And why use 4 functions and 4 concatenations, when Elancaster suggested to use 3 functions and 4 concatenations and I suggested to use 2 functions and 3 concatenations? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
zeeshan13
Constraint Violating Yak Guru
347 Posts |
Posted - 2008-04-22 : 10:38:31
|
| Peso,I aggree...Thanks,Zee |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-04-23 : 03:22:58
|
| Where do you want to show data?Seems you should do this formation in front end applicationMadhivananFailing to plan is Planning to fail |
 |
|
|
dass05555
Yak Posting Veteran
55 Posts |
Posted - 2008-04-23 : 08:48:43
|
| My dear friend,chk it out this query..select left(right('0000000000000'+ convert(varchar,7830005602),13),8)+'-'+right('0000000000000'+ convert(varchar,7830005602),5) |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-23 : 08:54:31
|
Which is different from what Elancaster suggested? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
dass05555
Yak Posting Veteran
55 Posts |
Posted - 2008-04-23 : 09:15:44
|
| As per elancaster query,he has not converted the datatype int to varchar.but his logic is correct. |
 |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-04-23 : 09:33:27
|
quote: Originally posted by dass05555 As per elancaster query,he has not converted the datatype int to varchar.but his logic is correct.
but OP said it's already varchar (as repeated in earlier replies too)....and HE is a SHE actuallyEm |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-23 : 11:01:17
|
Really?I thought we were only DBAs and DEVs here  E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|