简体   繁体   中英

Display all records with leading or trailing spaces

I am looking for a SQL query (Using SQL 2008) to search various fields that I specify (or even all fields in particular tables) for leading or trailing spaces. I would like the output to be two columns with the following data:

ID Number, Data (that has the space), Field Name

Thanks for the help as usual!

You can use DATALENGTH

SELECT ID, Data, FieldName
FROM table
WHERE DATALENGTH(RTRIM(LTRIM(Data))) <> DATALENGTH(Data)

尝试:

select `ID Number`, `Data (that has the space)`, `Field Name` from tbl WHERE data like ' %' or data like '% '

I guess it depends exactly what you mean,by list of fields, the below code will do one at a time. However, if you want to give a list of 20 columns and check each, and output to a single list, that would take some recursion or some copy/paste unions.

select ID, DATA,Field_Name
from table
where DATA like '[ ]%'
        or DATA like '%[ ]'

The below will get you everything you are looking for with the exception of the ID. I wasn't sure if this was the column name or row or an Auto_ID or what. If its different in every table its a little tricky, but if its the same, you should be able to amend the below.

Declare @Tables Varchar(8000)
        ,@Columns Varchar(8000)
        ,@Dynamic_SQL NVARCHAR(MAX)


IF OBJECT_ID('tempdb..#TempTables') IS NOT NULL
    DROP TABLE #TempTables

Create Table #TempTables
(TableNames Varchar(8000))

Insert Into #TempTables
Values ('Finance_LTD_Summary_Data')
        ,('Golf_TX_TMP')


IF OBJECT_ID('tempdb..#Results') IS NOT NULL
    DROP TABLE #Results

Create Table #Results
(TableNames Varchar(8000)
    ,ID Varchar(8000)
    ,Data Varchar(8000)
    ,FieldName Varchar(8000))

IF OBJECT_ID('tempdb..#Fields') IS NOT NULL
    DROP TABLE #Fields

Create Table #Fields
(COLUMN_Name Varchar(8000))

DECLARE KeyTables_cursor CURSOR FOR 
        SELECT TableNames
            from #TempTables

        OPEN KeyTables_cursor

        FETCH NEXT FROM KeyTables_cursor
        INTO @Tables


        WHILE @@fetch_status = 0
        BEGIN   


                Set @Dynamic_SQL = 'truncate table #Fields

                                    Insert into #Fields(COlumn_Name)
                                    select COLUMN_NAME
                                    from INFORMATION_SCHEMA.COLUMNS
                                    where TABLE_NAME =''' + @Tables + ''''

                print convert(text,@Dynamic_Sql)                        
                exec sp_executesql @Dynamic_Sql


                DECLARE KeyColumns_cursor CURSOR FOR 
                SELECT COLUMN_Name
                    from #Fields

                OPEN KeyColumns_cursor

                FETCH NEXT FROM KeyColumns_cursor
                INTO @Columns


                WHILE @@fetch_status = 0
                BEGIN   

                    Set @Dynamic_SQL = 'Insert into #results(TableNames,Data,FieldName)
                                            Select ''' + @Tables + ''' , ' + @Columns + ' , ''' + @Columns + ''' 
                                                From ' + @Tables + '
                                                Where ' + @Columns + ' like ''[ ]%''
                                                        or ' + @Columns + ' like ''%[ ]'''


                    print convert(text,@Dynamic_Sql)
                    exec sp_executesql @Dynamic_Sql

                FETCH NEXT FROM KeyColumns_cursor into @Columns

                END

                CLOSE KeyColumns_cursor;
                DEALLOCATE KeyColumns_cursor;   


        FETCH NEXT FROM KeyTables_cursor into @Tables

        END

        CLOSE KeyTables_cursor;
        DEALLOCATE KeyTables_cursor;

试试这个..这对我有用..它将找到以特殊字符开头的记录

SELECT * FROM ITEM_MASTER WHERE BARCODE LIKE '[^0-9a-zA-Z ]%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM