Format Function returns wrong value - ms-access

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)

Related

Convert random concatenated letter and number cells into a single numerical equivalent?

Please see a screen shot of my data:
Basically I would like to transform this concatenated resulting string sometextmoretext0.544728.822222222244728.823611111144728.825othertext (found in cell H6 in example) into whatever this data's numerical equivalent would be. However I can't seem to find a function to do this yet.
If anyone wants to know why:
I would like to make a unique ID column that is based on the unique combination of those columns. I figure all data has some kind of numerical equivalent to a computer, so there is likely to be a way!
Perhaps Apps Script can handle a task like this?
To convert each character of a string into an unicode number and concat them:
=JOIN(" ",ArrayFormula(CODE(SPLIT(REGEXREPLACE(H6,"(.)","$1♦"),"♦"))))
The formula work as follows:
Insert a special character ♦ between each characters of the original string
REGEXREPLACE(H6,"(.)","$1♦")
Split the generated string by the special character ♦, in order to get the array of the characters
SPLIT(...,"♦")
Convert each character into an unicode number
ArrayFormula(CODE(...))
Concat each unicode number with the specified string
JOIN(" ",...)

CDate Function is being reformatted and creates an Error

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))

How to convert date from csv file into integer

I have to send data from csv into SQL DB.
Problem starts when I try to convert data into Int. It wasnt my idea and I really cant do much with this datatype. When I'm trying to achieve this problem pop up:
Data Conversion 2: Data conversion failed while converting column
"pr_czas" (387) to column "C pr_dCz_id" (14). The conversion returned
status value 2 and status text "The value could not be converted
because of a potential loss of data.".
Tried already to ignore this problem but then another problems came up so there is no other way than solving this.
I have to convert this data from csv file which is str 50 into int 4
It must be int4. One of the requirements Dont know what t odo.
This is data I'm trying to put into int4. Look on pr_czas
This is data's datatype
Before I tried to do same thing with just DD.MM.YYYY but got same result...
Given an input column named [pr_czas] that contain string values that look like 31.01.2020 00:00 which appears to be a formatted date time represented in the format "DD.mm.YYYY HH:MM", I would like to express that as a whole number DDMMYYHHMM
Add a derived column to your data flow and call this new_pr_czas
The logic I'm going to use is a series of REPLACE statements and cast the final result to an integer. Replace the period, replace the colon and the space - all with nothing
(DT_I8)REPLACE(REPLACE(REPLACE([pr_czas], ".", ""), ":", ""), " ", "")
This is an easy case but things to note.
An integer/int32/I4 has a maximum value of 2 billion.
310120200000 is too large to fit into that space so you would need to make that an bigint/int64/I8. If I remember your previous question, you were having troubles with a lookup task so this data type mismatch might hurt you there.
The other thing to be aware of is that leading zeros will be dropped when converted to a number because they are not significant. If you need to retain the leading zeros, then you're working with string data type. This is an advantage to working with the ISO standard but if your data expects DD, then far be it for me to say otherwise.
If you need to slice your date into another format, then you'll want to have a few derived columns. The first one will generate a string column for each piece of pr_czas - year, month, day, hour and minute. You'll use the substring method for this and findstring to find the period space and colon.
The next data flow will be used to put those string pieces back into the new format and cast that to I8. Why? Because you can't debug doing it all in one shot but you can put a data viewer between two derived columns to figure out where a slice went awry.

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.

Search WHERE value is present in one of the JSON array using only regular expression

My column looks something like this
"{"a1":"{\"name\":[{\"id\":32,\"addId\":[28947,33268,48148,1594,57818,57819],\"hasPhone\":true}],\"phone\":[{\"phoneId\":5,\"selectionMode\":true}],\"phoneType\":[1],\"selectionMode\":\"GROUP\"}"}"
It is a JSON field but we are still on postgres 9.2 so there is no JSON support for search.
I have to get all records where addId is either 28947 or 57818
Please help
Here's a regular expression that matches
addId\":[ followed by any number of , and decimal digits with your magic numbers in the middle, ending in ].
SELECT regexp_matches(json_column, 'addId\\":\[[0-9,]*(28947|57818)[0-9,]*\]', 'g');
Tested it on your input data at this site. Did not test it with the actual regexp_matches function