Solved

Quick SQL Systax question

Posted on 2006-10-30
11
226 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]
Comment Utility
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
Comment Utility
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
Comment Utility
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 31

Accepted Solution

by:
awking00 earned 500 total points
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
>>Sorry, isnull and ifnull can get confusing. <<
I suspect you mean NULLIF
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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 31

Expert Comment

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

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

743 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

15 Experts available now in Live!

Get 1:1 Help Now