Unix Technical Forum

Functions and NULL with standard install

This is a discussion on Functions and NULL with standard install within the MySQL General forum forums, part of the MySQL category; --> I am running a basic install of MySQL 5.0 with "strict mode" turned on I would like to use ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:03 AM
Lucas.CTR.Heuman@faa.gov
 
Posts: n/a
Default Functions and NULL with standard install

I am running a basic install of MySQL 5.0 with "strict mode" turned on I
would like to use RPAD however at times a NULL var will be sent to the
function. My goal is to have a function that will convert the NULL to a
blank string to get the result of EXAMPLE3 if a NULL is returned.


EXAMPLE 1

mysql> select rpad(null,5,'1');
+------------------+
| rpad(null,5,'1') |
+------------------+
| NULL |
+------------------+
1 row in set (0.00 sec)

EXAMPLE 2
mysql> select rpad('0',5,'1');
+-----------------+
| rpad('0',5,'1') |
+-----------------+
| 01111 |
+-----------------+
1 row in set (0.00 sec)

mysql>

EXAMPLE 3

mysql> select rpad('',5,'1');
+----------------+
| rpad('',5,'1') |
+----------------+
| 11111 |
+----------------+
1 row in set (0.02 sec)


Is there a function I can use to convert the null string to a blank string
before it is sent to RPAD in a basic installation of MySQL or will I need
to create a function?


Wishing you the best you know you deserve,

______________________
Lucas Heuman
CM Web Developer
SRA International, Inc.
FAA, WJHTC/Bldg 300, 2nd Fl., H33
Atlantic City Int'l Airport, NJ 08405
Phone 609.485.5401
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:03 AM
Joshua Marsh
 
Posts: n/a
Default Re: Functions and NULL with standard install

On 3/21/07, Lucas.CTR.Heuman@faa.gov <Lucas.CTR.Heuman@faa.gov> wrote:
>
> I am running a basic install of MySQL 5.0 with "strict mode" turned on I
> would like to use RPAD however at times a NULL var will be sent to the
> function. My goal is to have a function that will convert the NULL to a
> blank string to get the result of EXAMPLE3 if a NULL is returned.
>
>
> EXAMPLE 1
>
> mysql> select rpad(null,5,'1');
> +------------------+
> | rpad(null,5,'1') |
> +------------------+
> | NULL |
> +------------------+
> 1 row in set (0.00 sec)



Try this:

SELECT RPAD(CASE WHEN NULL IS NULL THEN '' ELSE '2' END, 5, '1');

If you are using it on an actual field, you would replace the first NULL and
the '2' with the field to be checked.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 06:03 AM
Francesco Riosa
 
Posts: n/a
Default Re: Functions and NULL with standard install


select rpad(|IFNULL(|null, ''),5,'1');

|/||/|
Lucas.CTR.Heuman@faa.gov ha scritto:
> I am running a basic install of MySQL 5.0 with "strict mode" turned on I
> would like to use RPAD however at times a NULL var will be sent to the
> function. My goal is to have a function that will convert the NULL to a
> blank string to get the result of EXAMPLE3 if a NULL is returned.
>
>
> EXAMPLE 1
>
> mysql> select rpad(null,5,'1');
> +------------------+
> | rpad(null,5,'1') |
> +------------------+
> | NULL |
> +------------------+
> 1 row in set (0.00 sec)
>
> EXAMPLE 2
> mysql> select rpad('0',5,'1');
> +-----------------+
> | rpad('0',5,'1') |
> +-----------------+
> | 01111 |
> +-----------------+
> 1 row in set (0.00 sec)
>
> mysql>
>
> EXAMPLE 3
>
> mysql> select rpad('',5,'1');
> +----------------+
> | rpad('',5,'1') |
> +----------------+
> | 11111 |
> +----------------+
> 1 row in set (0.02 sec)
>
>
> Is there a function I can use to convert the null string to a blank string
> before it is sent to RPAD in a basic installation of MySQL or will I need
> to create a function?
>
>
> Wishing you the best you know you deserve,
>
> ______________________
> Lucas Heuman
> CM Web Developer
> SRA International, Inc.
> FAA, WJHTC/Bldg 300, 2nd Fl., H33
> Atlantic City Int'l Airport, NJ 08405
> Phone 609.485.5401
>


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 06:03 AM
Lucas.CTR.Heuman@faa.gov
 
Posts: n/a
Default Re: Functions and NULL with standard install

I don't see how to use this here, I will have to research the |/||/|

select rpad(|IFNULL(|null, ''),5,'1');

|/||/|


but this below works thank you

SELECT RPAD(CASE WHEN NULL IS NULL THEN '' ELSE '2' END, 5, '1');

If you are using it on an actual field, you would replace the first NULL
and
the '2' with the field to be checked.



On 3/21/07, Lucas.CTR.Heuman@faa.gov <Lucas.CTR.Heuman@faa.gov> wrote:
>
> I am running a basic install of MySQL 5.0 with "strict mode" turned on I
> would like to use RPAD however at times a NULL var will be sent to the
> function. My goal is to have a function that will convert the NULL to a
> blank string to get the result of EXAMPLE3 if a NULL is returned.
>
>
> EXAMPLE 1
>
> mysql> select rpad(null,5,'1');
> +------------------+
> | rpad(null,5,'1') |
> +------------------+
> | NULL |
> +------------------+
> 1 row in set (0.00 sec)



Try this:


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 06:04 AM
Shawn Green
 
Posts: n/a
Default Re: Functions and NULL with standard install

Hello Lucas,

Lucas.CTR.Heuman@faa.gov wrote:
> I don't see how to use this here, I will have to research the |/||/|
>
> select rpad(|IFNULL(|null, ''),5,'1');
>
> |/||/|
> <snip>
>


The function you want to use is IFNULL() documented
here:http://dev.mysql.com/doc/refman/5.0/...functions.html

Try it as he wrote it just without the pipes (|):

SELECT RPAD(IFNULL(column_name, ''),5,'1');

Another option to the IFNULL and CASE is the COALESCE function:
http://dev.mysql.com/doc/refman/5.0/...operators.html

--
Shawn Green, Support Engineer
MySQL Inc., USA, www.mysql.com
Office: Blountville, TN

Join us at the 2007 MySQL Conference and Exposition
April 23 to 26 - Santa Clara, California, USA
http://www.mysqlconf.com/



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:04 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com