I have a table column filled with values. The Column is set to Varchar(100). The problem I have is that I need to take whatever data is there and expand it out with "Padding" until each one is 100 characters.
All of this goes into a fixed width flat file.
Each entry in the column is between 20 and 30 characters right now. The padding I will be adding is "blank spaces"
My problem is that I am not sure how to write an update statement to handle updating the existing data. I have already fixed the future data.
Any suggestions?
You can do this with an update. The simplest method, though, is probably to change the type to char(100):
alter table t alter column col char(100);
char() pads values with spaces on the right.
Alternatively, you can just do:
update t
set col = col + replicate(' ', 100);
I don't think SQL Server complains about the size difference. If you are concerned about a potential error, you can do:
update t
set col = col + replicate(' ', 100 - len(col));
Related
In a column in a SQL Server database table, the value has a format of X=****;Y=****;Z=5****, where the asterisks represent strings of any lengths and of any values. What I need to do is to change that 5 to a 4 and keep the rest of the string unchanged.
Is there a way to use something like regular expressions to achieve what I want to do? If not using regular expressions, can it be done at all?
MS SQL sadly doesn't have any built in regex support (although it can be added via CLR) but if the format is fixed so that the part you want to change isZ=5toZ=4then usingREPLACEshould work:
REPLACE(your_string,'Z=5','Z=4')
For example:
declare #t table (str varchar(max))
insert #t values
('X=****;Y=****;Z=5****'),
('X=****;Y=**df**;Z=3**sdf**'),
('X=11**;Y=**sdfdf**;Z=5**')
update #t
set str = replace(str,'Z=5','Z=4')
-- or a slightly more ANSI compliant and portable way
update #t
set str = SUBSTRING(str,0, CHARINDEX('Z=5', str)) + 'Z=4' + SUBSTRING(str, CHARINDEX('Z=5', str)+3,LEN(str))
select * from #t
str:
X=****;Y=****;Z=4****
X=****;Y=**df**;Z=3**sdf**
X=11**;Y=**sdfdf**;Z=4**
We need more information. Under what circumstances should 5 be replaced by 4? If it's just where it occurs as the first character after the Z=, then you could simply do...
set Col = Replace(Col,'Z=5','Z=4')
Or, do you just want to replace 5 with 4 anywhere in the column value. In which case you'd obviously just do...
set Col = Replace(Col,'5','4')
Or possibly you mean that 5's should be replaced by 4's anywhere within the value after Z= which would be a lot harder.
update Table set Field = replace(Field, ';Z=5', ';Z=4')
And let's hope that your asterisked data doesn't contain semicolons and equality signs...
We are updating column A with the exact value from column B. The length of column B is 255 and column A is 4. The data in column B has been verified by LEN(REVERSE(colB)) to be only 4. When we try to update the error message says:
'String or binary data would be truncated.'
here is the query:
update table
set columnA=columnB
where Column B in ('ABC','ABCD','AB')
we have also verified that this works:
update table
set columnA=left(columnB,4)
where Column B in ('ABC','ABCD','AB')
any guesses as to what could be wrong?
thanks
It could be that you are using a char (not varchar) data type for column B, in which case your database engine may consider the width of the column, regardless of the width of the data. A trim function may then get rid of the error.
SQL doesn't check the actual length of the string - it just detects that the string value COULD overflow the new container and gives you an error message. Try this:
update table set ColumnA=(select LEFT(ColumnB, 4) from table where ColumnB in ('ABC', 'ABCD', 'AB'));
This selects only the first four characters of the data from columnB.
This query works And I am ok with it.
update table
set columnA=columnB
where Column B in ('ABC','ABCD','AB') and len(columnb)<=4
I am still curious as to why
I have a table DomainDetail having a column fieldID.
It has values like A1,B22,A567,D7779,B86759 .. i.e. from two characters to max six characters.
I want these values have equal number of characters
A000001,B000022,A000567,D07779 and B86759 .
This is how I think I should proceed
Estimate size of field value = LEN(fieldID)
Insert number of zeros equal to (6 - number of characters) .
How can I insert 0's sandwiched inside original value . How can do in SQL ?
like this
select
left(fieldID, 1) +
right('000000' + right(fieldID, len(fieldID) - 1), 5)
from DomainDetail
take a look at SQL FIDDLE example
It sounds like a problem better solved by business logic, i.e. the layer of code above your database. Whenever you insert, do the padding in that code - then always use that code/layer to insert into the table.
It seems to be a business logic requirement anyway - "ID must have a maximum 6 characters". Because a database wouldn't impose such a limit.
(unless you are using stored procedures as your business logic layer? in which case, PadLeft function in T-SQL)
select
stuff(fieldId,2,0,
LEFT('0000000000000000000000000',
(select max(LEN(FieldID)) from DomainDetail)
-LEN(fieldId)))
from DomainDetail
If you need a fixed output length just replace inner select (select max(LEN(FieldID)) from DomainDetail) with for example 6
Here is a SQLFiddle demo
If you want to UPDATE, then use this
UPDATE DomainDetail
SET fieldId=
SUBSTRING(fieldId,1,1)+
REPLICATE('0',6-LEN(id))+
SUBSTRING(fieldId,2,LEN(id)-1)
If you want to just SELECT without altering the values in the table, then this should work
SELECT SUBSTRING(id,1,1)+
REPLICATE('0',6-LEN(id))+
SUBSTRING(id,2,LEN(id)-1)
FROM DomainDetail
Hope this helps,
Raj
select stuff(fieldid, 2, 0, replicate('0', 6-len(fieldid)))
from DomainDetail
I import Excel files via SSIS to SQL-Server. I have a temp table to get everything in nvarchar. For four columns I then cast the string to money type and put in my target table.
In my temp table one of those four columns let me call it X has a comma as the delimiter the rest has a dot. Don't ask me why, I have everything in my SSIS set the same.
In my Excel the delimiter is a comma as well.
So now in my target table I have everything in comma values but the X column now moves the comma two places to the right and looks like this:
537013,00 instead of 5370,13 which was the original cell value in the temp and excel column.
I was thinking this is a culture setup problem but then again it should or shouldn't work on all of these columns.
a) Why do I receive dot values in my temp table when my Excel displays comma?
b) how can I fix this? Can I replace the "," in the temp table with a dot?
UPDATE
I think I found the reason but not the solution:
In this X column in excel the first three cells are empty - the other three columns all start with 0. If I fill these three cells of X with 0s then I also get the dot in my temp table and the right value in my target table. But of course I have to use the Excel file as is.
Any ideas on that?
Try the code below. It checks whether the string value being converted to money is of numeric data type. If the string value is of numeric data type, then convert it to money data type, otherwise, return a NULL value. And it also replaces the decimal symbol and the digit grouping symbol of the string value to match the expected decimal symbol and digit grouping symbol of SQL Server.
DECLARE #MoneyString VARCHAR(20)
SET #MoneyString = '$ 1.000,00'
SET #MoneyString = REPLACE(REPLACE(#MoneyString, '.', ''), ',', '.')
SELECT CAST(CASE WHEN ISNUMERIC(#MoneyString) = 1
THEN #MoneyString
ELSE NULL END AS MONEY)
As for the reason why you get comma instead dot I have no clue. My first guess would be cultural settings but you already checked that. What about googling, did you get some results?
First the "separator" in SQL is the decimal point: its only excel that is using the comma. You can change the formatting in excel: you should format the excel column as money and specify a decimal point as the separator. Then in the SSIS import wizard split out the transformation of the column so it imports to a money data type. Its a culture thing, but delimiter tends to be used in the context of signifying the end of one column and the start of the next (as in csv)
HTH
Well thats a longstanding problem with excel. It uses the first 30 or so rows to infer data type. It can lead to endless issues. I think your solution has to be to process everything as a string in the way Yaroslav suggested, or supply an excel template to have data predefined and formatted data type columns, which then have the values inserted. Its a pita.
I have a gridview and when populated the length of the returned field is greater than the column width making things look a bit messy. How can I display only say the first 20 characters in the column or of the field returned.
If I can't do it in Gridview then how can I tell the sql select statement to return only a certain amount of characters?
Any ideas?
I assume you are using SQL 2005 then
You can use Left function
SELECT LEFT('ColumnName', 20)
Also can find many such useful functions here
I've used this type of technique before to display an ellipsis on truncated text
SELECT CASE
WHEN Len(col) > 20 THEN LEFT(col, 19) + N'…'
ELSE col
END AS col
FROM t