× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



On 08 Aug 2012 09:56, Stone, Joel wrote:
The following doesn't zero-fill the MM and DD fields. Is it possible
to get SQL to zero-fill these values for months 1 thru 9 and also
days 1 thru 9?

YEAR(curDate()) || MONTH(curDate()) || DAY(curDate())

Purpose: I am trying to load the current date (20120808) into an old
column defined as YYYYMMDD alpha.


My preference for that had long been to use the CURRENT DATE special register instead of the CURDATE function, to cast the date value to the effective desired date-string format, and to then remove the separators:

replace(char(current_date, ISO), '-', '')

However the VARCHAR_FORMAT built-in scalar function eventually came along, and that is more succinct and obvious although requiring a timestamp instead of a date expression [optionally using NOW() to replace current timestamp]:

varchar_format(CURRENT_TIMESTAMP, 'YYYYMMDD')

To get the given expression to function correctly, the [implicit] cast to character must be changed to a cast to character that preserves the leading zero of a two-digit numeric; e.g. the DIGITS function. Because all of YEAR, MONTH, and DAY return large integer, that would require also first casting those to something like DEC(2); not very pretty IMO:

digits(dec( YEAR(curDate()),2))
|| digits(dec(MONTH(curDate()),2))
|| digits(dec( DAY(curDate()),2))

Regards, Chuck

As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.