473,854 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert Null in Date field to String

Hi,

My SQL returns a NULL in a datefield if there is no date in that field.
If there is a NULL in this column, I want to replace it with spaces in
my SELECT statement when I am selecting these records. Can any of you
experts out there help me with a sample SQL.

Nov 20 '06
10 39228
Frank Swarbrick wrote:
>>You can use the built-in NULLIF function for that.

If you have a string consisting of whitespaces only and you want to have
this string interpreted as NULL date, then a CASE expression is needed:

CASE
WHEN TRANSLATE(str, '', ' ') = ''
THEN NULL
ELSE str
END

If it is permissable to remove whitespaces from the string even if the
string is not empty (and should not be stored as NULL), you can nest
TRANSLATE into NULLIF:

NULLIF(TRANSL ATE(str, '', ' '), '')

I'm not sure I understand why either of these is necessary. I just did
the following:
CREATE TABLE TEST2 (TIMESTAMP1 TIMESTAMP , NULL1 VARCHAR(100) );
EXEC SQL
BEGIN DECLARE SECTION
END-EXEC
01 SQL-FIELDS.
05 TIMESTAMP1 PIC X(26).
05 NULL1VC PIC X(100).
EXEC SQL
END DECLARE SECTION
END-EXEC

PROCEDURE DIVISION.
EXEC SQL
CREATE TABLE TEST2 (TIMESTAMP1 TIMESTAMP , NULL1 VARCHAR(100)
);
END-EXEC

MOVE '2006-11-24-11.58.53.012345 '
TO TIMESTAMP1
MOVE 'This is a test' TO NULL1VC
PERFORM INSERT-TEST2
MOVE SPACES TO TIMESTAMP1, NULL1VC
PERFORM INSERT-TEST2
.

INSERT-TEST2.
EXEC SQL
INSERT INTO TEST2 (
TIMESTAMP1,
NULL1
)
VALUES (
NULLIF(:TIMESTA MP1,''),
NULLIF(RTRIM(:N ULL1VC),'')
)
END-EXEC
.

I ended up with two rows, the first having the timestamp and varchar field
set appropriately, and the second having both set to NULL. Seems right to
me.
You should verify your results. If I read the above code correctly and
translate it to pure SQL, then this would be equivalent:

CREATE TABLE test2( t timestamp, n varchar(100) );
INSERT INTO test2
VALUES ( NULLIF('2006-11-24-11.58.53.012345 ', ''),
NULLIF(RTRIM('T his is a test'), '') );
INSERT INTO test2
VALUES ( NULLIF('2006-11-24-11.58.53.012345 ', ''),
NULLIF(RTRIM(' '), '') );

The result of the first insert cannot have NULL in the second column because
the strim 'This is a test' is not empty, even if trimmed:

SELECT * FROM test2

T N
-------------------------- ---------------------------
2006-11-24-11.58.53.012345 This is a test
2006-11-24-11.58.53.012345 -

Anyway, my whole point about using TRANSLATE (RTRIM is equally fine) for
converting from string to timestamp is that a string like ' ' is _not_ a
valid timestamp. You have to remove the spaces so that NULLIF will truly
get an empty string and convert that to NULL.

VALUES timestamp(' ')
SQL0180N The syntax of the string representation of a datetime value is
incorrect. SQLSTATE=22007
create table test2( t timestamp, n varchar(100) )
While we're on the subject of date/time stuff, is there a function that
takes in, say, a DECIMAL(8) or DECIMAL(9) field set to something like
20061103 and translates it into a DB2 DATE field? I see there's a DATE()
function, but that doesn't quite do what I'm looking for, as far as I can
tell. Of course it's simple enough to fiddle around using a separate
COBOL working-storage field to get it to work, but it would be nice if I
didn't have to.
Maybe the TO_DATE function works for you?

Other than that, you could apply some calculations and stick with the DATE
function:

CREATE FUNCTION getDate(d INT)
RETURNS DATE
RETURN DATE('0001-01-01') +
((d / 10000) - 1) YEARS +
(MOD(d / 100, 100) - 1) MONTHS +
(MOD(d, 100) - 1) DAYS;

VALUES getDate(2006112 7)

1
----------
11/27/2006

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 27 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
22710
by: Mark | last post by:
Hi I have been trying to convert the week number to a range of dates that I can use. It should be fairly simple for you guru's out there but for us mere mortals it is beyond our grasp. I know that there are different start days of the week so I would presume any function would provide that facility. Hope you can help Mark
8
10414
by: Lyn | last post by:
I am trying to get my head around the concept of default, special or empty values that appear in Access VBA, depending on data type. The Access Help is not much (help), and the manual that I have is not much help here either. Googling has given me a little help. This is my current understanding -- I would appreciate any comments or corrections... "" -- this means an empty string when applied to String data type, and also to Variant...
2
5210
by: Annie D via AccessMonster.com | last post by:
Hi, I am using the following SQL statement courtesy of access monster user: 'Terry Kreft ' to convert dates from a text format (20020331 00:00:00) to a recognised date/time format (31/03/2002). SELECT END_DTTM, Left(,4) AS END_DTTM_year, Mid(,5,2) AS END_DTTM_month,
6
15042
by: Paulb1us | last post by:
I want to set a DateTime field to Null before passing it to the DB //First I check to see if anything is in this datarow column, because sometimes we have no data. DateTime dt; if ( datarow.ToString().Length > 0) { //We assume the data is a date dt = (DateTime)datarow; }
5
10605
by: darrel | last post by:
I have the following right now to enter a date into SQL getting the data from some pull down menus: ------------------------------------------------- dim dateCCJApprovedDate as DateTime if cbx_ccjDateNone.Checked = True then dateCCJApprovedDate = ctype("", DateTime) else dateCCJApprovedDate = ctype(ddl_CCJDateMonth.SelectedValue.tostring &
6
13000
by: Dean Slindee | last post by:
I am looking for the "right" way to handle inserting and presenting null date values. Public Const c_NullDate As Date = #12:00:00 AM# If I set the value of a date variable in an SQL Server insert statement to c_NullDate, the insert statement works. When I re-read the row and display the inserted date value in a text box, the string "1/1/1900" is displayed. If one wanted the textbox to display an empty string, how should either the insert...
12
3007
by: Brian Henry | last post by:
first question... I have a flat file which unfortinuatly has columns seperated by nulls instead of spaces (a higher up company created it this way for us) is there anyway to do a readline with this and not have it affected by the null? because it is right now causes truncated data at wierd places... but as soon as i manually with a hex editor change char(00) to char(20) in the files it reads prerfectly... which leads me to my 2nd...
7
1757
by: Mike Howard | last post by:
I'm trying to convert the following (simplified) VB.Net code to C#, that makes use of some externally developed COM code written in VB6. VB.Net Code Sub Main() Dim oAPP As Object Dim oApp2 As APP2.Class Dim user As String
3
2947
by: Elliot | last post by:
How can to save a string("13/12/2007") in a field which datatype is date in the table? field = xxx?
0
9901
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
10684
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10371
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Duprť who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5744
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4563
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.