Question

I have some problem with removing spaces in my DB response... i.e data in table is second - ____________________1 by underline I mean spaces... but I can't remove those spaces when I display my data... How can I do it?

I have been tryed TRIM(), LTRIM(), ALLTRIM(), REPLACE(), STRREPLACE(), but they are not working with my visual fox pro database...

Can anyone help me with my problem?

UPD: Now I just display with print_r() command... but here is some code:

$result = odbc_exec($connect, "select
                                        pacienti.pacientid,
                                        pacienti.f_name,
                                        pacienti.s_name,
                                        napravlenie.rtgapparat,
                                        napravlenie.doctor,
                                        opisanie_rtg.opis,
                                        apparatura.prefiks
                                    from
                                        pacienti left outer join napravlenie on pacienti.pacientid = napravlenie.pacientid
                                            left outer join opisanie_rtg on pacienti.pacientid = opisanie_rtg.pacientid
                                                left outer join apparatura on napravlenie.rtgapparat = STUFF(apparatura.gruppa, 0, ' ', '')
                                    where
                                        pacienti.pacientid = '".$pk_full."'") or die ("Error in Database: ".odbc_errormsg());

    $all = array();
    while($row = odbc_fetch_array($result))
    {
        $all[] = $row;
    }

    echo "<pre>";
    print_r($all);
Was it helpful?

Solution

Try STRTRAN(string_field, ' ','')

OTHER TIPS

.DBF (dBase/Foxpro table) character fields are padded with spaces so I would recommend that you use select alltrim(pacienti.f_name) as f_name,... to remove leading and trailing spaces.

With most database systems these days, you have the luxury of variable width fields. The issue is that the native VFP behavior is to allocate a fixed amount of space for each column in the table. When you execute a SQL statement in VFP, it looks at the first record to determine what the type and size of each field will be, and creates the resulting cursor/table structure accordingly.

For example if you have a table with the first column defined as Char(50), then VFP will allocate 50 spaces (not null characters) in that field so that your string will be exactly 50 characters long on every row.

When you tried to use Select Alltrim(field), ... from table, if the first column of the first record it finds the string "now is the time" then the width of the first column will be 15 characters and all subsequent rows will be padded or trimmed, but all rows will be 15 characters.

You can experiment with this by creating a table of few rows and a single column that contains different amounts of text/white space. then try

SELECT alltrim(field1) FROM mytable ORDER BY len(alltrim(field1)) INTO CURSOR result

And the result will be that all the rows will only be as wide as the shortest string in the source data.

This means that all your trimming needs to happen after you run your query and cannot be done as part of the query.

I know this is a very old question but i just discovered a way to do it

SELECT CAST(columnName as varchar(len(TRIM(columnName)))) FROM table

It's not a nice way to write the query but it does appear to crop it correctly.

As mentioned above STRTRAN replaces ALL spaces in the column, so if you have

"John Smith    "

It becomes

"JohnSmith"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top