Solved

Quick SQL Systax question

Posted on 2006-10-30
11
227 Views
Last Modified: 2008-03-04
I was wondering how to write a SQL statement:        

                UCASE all string
o      TRIM all white space
o      Replace ; with :
o      Replace & with AND
o      If value is blank or null set it to N/A

and seperatly how would I write this?

o      If value is blank or null set it to blank

and this is also sperate how would I write this?

o      If value is blank or null set it to 0

I would look it up but I am pressed for time and cant find any exact scripts...
Thanks!

Yaba
0
Comment
Question by:yabakingda
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17834392
UCASE(LTRIM(RTRIM(REPLACE(REPLACE( CASE WHEN field IS NULL THEN 'N/A' WHEN field = '' THEN 'N/A' ELSE field END  , '&', 'AND') , ';', ':'))))
0
 
LVL 10

Expert Comment

by:AaronAbend
ID: 17834407
update tablename set columnname = case when '' then 'N/A' when null then 'N/A' else replace(ltrim(rtrim(upper(columnname))),'&',' and ')  end case

update tablename set columnname = case when '' then 0 when null then 0 else replace(ltrim(rtrim(upper(columnname))),'&',' and ')  end case
update tablename set columnname = case when '' then '' when null then '' else replace(ltrim(rtrim(upper(columnname))),'&',' and ')  end case
 
0
 
LVL 10

Expert Comment

by:AaronAbend
ID: 17834427
sorry... didn't parse it ... missing the columnname after the word case  

update tablename set columnname = case columnname when '' then 'N/A' when null  then 'N/A' else replace(ltrim(rtrim(upper(columnname))),'&',' and ')  end  


0
 
LVL 32

Accepted Solution

by:
awking00 earned 500 total points
ID: 17835062
The following is for your first case:
replace(replace(upper(ifnull(trim(field),'N/A')),'&','AND'),';',':')
However, you said "TRIM all white space" and trim only deals with leading and trailing characters (default blanks). To remove any blanks "in the middle" add another replace statement -
replace(replace(replace(upper(ifnull(trim(field),'N/A')),'&','AND'),';',':'),' ','')

>>If value is blank or null set to blank<<
set field = (isnull(trim(field),' ')

>>If value is blank or null set to 0<<
set field = (isnull(trim(field),'0')

To avoid update of all field add where condition
where isnull(trim(field)) = 1
0
 
LVL 32

Expert Comment

by:awking00
ID: 17835105
Sorry, isnull and ifnull can get confusing. The last two should have been
>>If value is blank or null set to blank<<
set field = (ifnull(trim(field),' ')

>>If value is blank or null set to 0<<
set field = (ifnull(trim(field),'0')

The where condition is, however,
where isnull(trim)field)) = 1
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 17835979
>>Sorry, isnull and ifnull can get confusing. <<
I suspect you mean NULLIF
0
 
LVL 32

Expert Comment

by:awking00
ID: 17836555
acperkins,
I do not mean nullif. Nullif syntax is nullif(expr1, expr2) - returns null if expr1 = expr2, otherwise returns expr1. In this case we do not want to return null, we want to return blank or zero if the value of the expression is null.
0
 
LVL 32

Expert Comment

by:awking00
ID: 17836657
Maybe a literal interpretation of my query would help.
trim(field) removes leading and trailing blanks from field. If field is blank, then trim(field) is null and, if field is null, then trim(field) is also null. So ifnull(trim(field),'N/A/) in either of those cases returns 'N/A' otherwise it will return the value of field trimmed, which is then set to upper case, replaces any ampersand ('&') with the letters 'AND' and replaces any semi-colon with a colon. Furthermore, if there are any blanks between the first and last characters of field, the added replace(...,' ','') will remove them.

Likewise ifnull(trim(field),' ') returns a blank if field is blank or null and ifnull(trim(field),'0') returns a character zero if the field is blank or null. It may be that the latter case requires a conversion if a number is required.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 17837453
>> TRIM all white space <<

This will require a custom function.  Everything else can be done easily using standard SQL functions (as shown by earlier posts).
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 17839784
awking00,

>>I do not mean nullif. <<
Simply put, there is no IFNULL in T-SQL (the SQL dialect in this Topic Area), perhaps you are thinking of PostgreSQL.

In any case, it may be  moot point as the questioner is aparently using MS Access and not MS SQL Server.
0
 
LVL 32

Expert Comment

by:awking00
ID: 17841361
acperkins,
I apologize for that. I was thinking I was in the mysql section.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now