SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


DATALENGTH function


DATALENGTH function

Author
Message
NewBornDBA2017
NewBornDBA2017
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4104 Visits: 924
I have 149 tables in our DBs with State column. Values I see in that State column are like MI, Michigan, ILL, IL, Illinois etc., so there is no consistency on how we store the data. Even the column name is like State, STATE, state. This is what I used to build a sql
select 'select top 5 * from '+s.name+'.'+t.name+' where DATALENGTH('+c.name+')>3'
from sys.schemas s
INNER JOIN sys.tables t on s.schema_id=t.schema_id
INNER JOIN sys.columns c on t.object_id=c.object_id
where c.name = 'State'

Now I am trying to find all the records of all state where length of a state column is more than 3 character long so I can change it to use abbreviation but when I run the below query, I am still getting OR or WI.
select top 5 * from DBQ412v3 where DATALENGTH(STATE)>3

GilaMonster
GilaMonster
SSC Guru
SSC Guru (919K reputation)SSC Guru (919K reputation)SSC Guru (919K reputation)SSC Guru (919K reputation)SSC Guru (919K reputation)SSC Guru (919K reputation)SSC Guru (919K reputation)SSC Guru (919K reputation)

Group: General Forum Members
Points: 919182 Visits: 48849
What's the data type of the State column?

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


NewBornDBA2017
NewBornDBA2017
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4104 Visits: 924
GilaMonster - Thursday, March 8, 2018 8:43 AM
What's the data type of the State column?

It is varchar(50). I am guessing that the ideal situation would have been CHAR(2) and the ability to redirect rows during ETL process but unfortunately that's not the case.

Luis Cazares
Luis Cazares
SSC Guru
SSC Guru (165K reputation)SSC Guru (165K reputation)SSC Guru (165K reputation)SSC Guru (165K reputation)SSC Guru (165K reputation)SSC Guru (165K reputation)SSC Guru (165K reputation)SSC Guru (165K reputation)

Group: General Forum Members
Points: 165915 Visits: 22823
You could try something like this:

select 'select top 5 * from '+s.name+'.'+t.name+' where '+c.name+' NOT LIKE ''__'''
from sys.schemas s
INNER JOIN sys.tables t on s.schema_id=t.schema_id
INNER JOIN sys.columns c on t.object_id=c.object_id
where c.name = 'State'


Or use the LEN() function that would ignore trailing spaces


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum









































































































































































SQLServerCentral


Search