CDate Function is being reformatted and creates an Error - ms-access

I have a select query which has a date stored as string e.g. 20211231 (Dec 31, 2021). I am trying to use the CDate function to convert the string. If I enter the following in the SQL view
SELECT CDate(Format([XLCustOrdDtl]![DATE_ADDED],"####/##/##")) AS DteAdded
FROM XLCustOrdDtl;
The result are as expected, however when I go back to Design view and run the query, Access reformats the string to
SELECT CDate(Format([XLCustOrdDtl]![DATE_ADDED],"#\/#\/#")) AS DteAdded
FROM XLCustOrdDtl;
This results in #Error
Any thoughts on why this is happening and how I might correct the issue?

As your date is text, let Format know this and use #.
Also be aware, that to Format a slash is not a slash but the local date separator. To force a literal slash, escape it with a backslash:
SELECT CDate(Format([XLCustOrdDtl]![DATE_ADDED],"####\/##\/##")) AS DteAdded
FROM XLCustOrdDtl;

Apparently, lack of hyphen or slash punctuation in string means Access cannot recognize date parts and # is a date delimiter character and not valid as date format placeholder.
Assuming there are no Null fields and there are always 8 digits including placeholder zeroes, consider:
DateSerial(Left([DATE_ADDED],4), Mid([DATE_ADDED]5,2), Right([DATE_ADDED],2))

Related

Extract and convert Datetime field from JSON in SNOWFALKE

I am receiving a Datetime field in my JSON which looks like the below screenshot
where first 4 characters are year, next two characters are month, next two characters are date, next two characters are hour, next two characters are minutes and last two characters are seconds.
I extracted the date successfully and tried to convert using to_timestamp function in SNOWFLAKE but returns me the result like
"2606-08-31 03:17:04.416"
code that I am working with to convert into Timestamp
select distinct to_timestamp(a.value) within group(order by to_timestamp(a.value)) from orders a
Could someone please help me with this?
The default format for conversion is AUTO, which will attempt to guess. It appears as though it is guessing incorrectly.
To specify a format, add a second parameter to your call to to_timestamp that is a string of the format. Here are the docs.
to_timestamp(a.value, 'YYYYMMDDHH24MISS')

Why does MySQL ignore mutiple dash delimiters in DATE columns?

Why does the below condition work? Does MySQL convert multiple dashes '---' to a single '-' dash. I tried finding an explanation for this but I couldn't find anything.
WHERE DATE(login_at) = '2019---04---30'
Can anyone explain to me how this works?
I'm using MySQL Version : 5.7.18
MySQL allows a flexibility, by auto-interpreting various delimiters, and converts them to proper date[time] format. It is well explained in documentation:
MySQL permits a “relaxed” format for values specified as strings, in
which any punctuation character may be used as the delimiter between
date parts or time parts. In some cases, this syntax can be deceiving.
For example, a value such as '10:11:12' might look like a time value
because of the :, but is interpreted as the year '2010-11-12' if used
in a date context. The value '10:45:15' is converted to '0000-00-00'
because '45' is not a valid month.
The only delimiter recognized between a date and time part and a
fractional seconds part is the decimal point.
So, in your case, MySQL is considering the delimiter as ---, and thus implicitly converting to Y-m-d format.
However, it is always better and safe to use the standard Y-m-d h:i:s format, to represent a date/datetime value.

questions about the datetime type values

I have several things which I want to discuss with you guys.
Since, they were just simple questions, so no dataset here.
Suppose I have a datetime type column which called started_date, the value in it was like:
yyyy-mm-dd hh:mm:ss. So, if I want to select some IDs which were larger than one specified day (let's say June/01/2017), can I just using
select ID, started_date
from table1
where started_date>"2017-06-01";
Does this work?
I tried some samples, and it worked indeed in the mysql. However, someone told me that I cannot compare the datetime column with string values without converting their format. And it confused me. Because I thought the value "2017-06-01" here was date type value, so it does not need convert. Or am I thinking wrong?
Another thing was about the double quote and single quote, I understand that the single quote was used for string values. However, in this case, when I used double quote to quote "2017-06-01", it works. So, does it mean the double quote can quote date values?
I am just asking, so any response is welcome.
Thanks.
Your query is fine. You are using a safe date/time format for the string. In other words, if you have to store the value as a string, then use that format.
I would write the code as:
where started_date >= '2017-06-01'
I see no reason to exclude midnight on 2017-06-01 (although you might have a reason). Second, single quotes are the standard delimiter for strings.
That said, you can store the value as a string.
As a best practice, I stay away from comparing time-stamps to date-stamps. In this case you can be explicit and truncate the start date. And yes, use single quotes instead.
where SUBSTR(started_date, 1, 10) > '2017-06-01'
To make sure it works you could just convert the date time to a string first and compare the two strings:
to_char(started_date,'YYYY-MM-DD') >= '2017-06-01'
The Strings will compare just fine in that format.

Format Function returns wrong value

We have a field in a query that should be left-padded with zeroes if it is too short, and we accomplish this using the Format() function. However, there are some values that produce bizarre results.
Format("14425112-8","00000000-00")
Returns the value "00019330-78"
For most inputs, the string gets formatted as expected, 8 digits, hyphen, two digits. But in a few rare cases, the value is modified. Is this repeatable for anyone else? Does anyone have an explanation?
Thanks for your help.
This is an example of access trying to be too helpful. It looks like it is interpreting these values as dates, but since you didn't use any date indicators in the format e.g: (dd,mm,yyyy), it converted 1-1 to a date, and then tried to display it in decimal form:
debug.print Format("1-1","000000-00")
returns 000427-36 which is the decimal value 42736 which, if you convert to a date, becomes 1/1/2017. This is what access interpreted "1-1" as.
it seems that access has reserved the - character as symbolizing a date format, despite what their website says. This function is only useful for formatting actual dates, or numeric values, such as prices. If you are set on using the format function, you will have to change you separator to a decimal point, which apparently is the only character that will get you what you want with the leading and trailing zeros.
Otherwise, you may have to build your own function for this.
You cannot format a string like a number this way. Try this:
PaddedNumber = Right(String(8, "0") & "14425112-8", 10)

Why i got null from this query

Following is my sql query kindly let me know why is it returning null
Select STR_TO_DATE ('11-APR-74','%e%b%Y')
OR
Select DATE_FORMAT ('11-APR-74','%e%b%Y')
From MySQL STR_TO_DATE function:
The server scans str attempting to match format to it. ... Scanning
starts at the beginning of str and fails if format is found not to
match.
This is why your first query fails: 11-APR-74 does not look like %e%b%Y, so date cannot be parsed. You should do instead
SELECT STR_TO_DATE ('11-APR-74','%e-%b-%Y')
From MySQL Date and Time types:
Although MySQL tries to interpret values in several formats, date
parts must always be given in year-month-day order (for example,
'98-09-04'), rather than in the month-day-year or day-month-year
orders commonly used elsewhere (for example, '09-04-98', '04-09-98').
This is why your second query fails: 74 is not a valid day of month, you should do instead
SELECT DATE_FORMAT ('74-APR-11','%e%b%Y')
Note, that DATE_FORMAT is usually used on DB values, not string literals as you do - to get an output different from the default one.
If you want to convert from string to date
Select STR_TO_DATE ('11-APR-74','%d-%b-%y')
use it like ::
Select STR_TO_DATE ('11-APR-74','%e-%b-%Y')
Because '%e%b%Y' format does not correspond to '11-APR-74' string value (as STR_TO_DATE function expects), and because '11-APR-74' value is of type CHAR, but not DATETIME (as DATE_FORMAT function expects).
If you want to reformat a date represented by a CHAR value, convert it from its original format to DATETIME first, and then convert it to a string of desired format:
SELECT DATE_FORMAT(STR_TO_DATE('11-APR-74','%e-%b-%Y'),'%e%b%Y');
By the way, you could strip dashes with a plain string sunction:
SELECT REPLACE('11-APR-74','-','');