Format Text Field To Date In Access Query - ms-access

I have a form in Access that has two text boxes that are format to be a short date. I am now attempting to capture those values and use in a query in the WHERE clause. I tried this syntax
Between CDate([Forms]![Form1]![date1]) And CDate([Forms]![Form1]![date2])
This expression is typed incorrectly, or it is to complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to variables.
How should I capture & convert the entries from my form text boxes so that I can use them in my query?

Specify the parameters to free Access from guessing, then use these "as is":
Parameters
[Forms]![Form1]![date1] DateTime,
[Forms]![Form1]![date2] DateTime;
Select
<your select statement>
From
<your table/query>
Where
[YourDateField] Between
[Forms]![Form1]![date1] And
[Forms]![Form1]![date2]

Related

Expression in query treated as string

I am having a hell of a time trying to add two calculated fields in a query together. My first record has field1= 1, and field2= 5, and the field that is trying to add them as 15!
So it’s treating them as a string.
When I try to use the function of SUM() I get an error of some of the other fields not being used in expressions, which I don’t understand.
Subtracting the two fields works as does multiplication.
I am unable to change the format of either fields in the properties as the drop down menu is blank.
Please help!
Aggregate functions act on rows not fields. Sum(field1) adds the values of field1 for group of records. Use aggregate functions in an aggregate (GROUP BY) query.
Plus (+) character will concatenate text values but add numeric. Apparently, your two fields are providing text values. Either correct the field data type or use function to convert values to number. Convert at least one field and Access will perform arithmetic instead of concatenation on all terms of expression.
Val(field1) + field2
This assumes no fields are Null. Number conversion functions will error with Null. Also, arithmetic with Null returns Null. If Null is possibility, handle with Nz().
Val(Nz(field1,0)) + Nz(field2,0)

Change entire column format - phpMyAdmin

In a project I am working on, I have a column in my database named Amount. This is a money format, but there is no need for it to have a dollar sign in front.
I would like every value that is entered in here to be this format:
123456 -> 123,456.00
How can I achieve this?
No, the stored value should not be formatted, it should be stored as a plain number, otherwise you can't do any arithmetic calculations.
When the number is displayed, that's when you format its appearance either in the application layer (that's the preferred approach) or you can use MySQL's own format() function in the select statement to achieve this:
select format(amount) from your_table

Simple select issue with -(dash) in where clause

I need to search for a value like 1234-abc. The database doesn't have this particular value, but has another value 1234. Now the problem is when I write my query like
SELECT * FROM words WHERE tval='1234-abc'
instead of fetching an empty recordset, it fetches the 1234 value, it seems to ignore anything after the -, any idea what's going on?
http://sqlfiddle.com/#!2/9de62/3
You can use the BINARY keyword for the exact match
SELECT tval FROM words WHERE BINARY tval='1223-abc';
Binary is a built-in keyword that after your WHERE clause that forces a comparison for an exact case-sensitive match
Fiddle
The existing expression is implicitly converting the string expression to a number - you need to explicitly convert the number to a character strng, like so:
SELECT tval FROM words WHERE convert(tval,char(20))='1223-1ABCDE';
SQLFiddle here.

mysql calculation field stored in another field

I have fields in mysql that look like this:
constant1 , constant2, variable1, variable2, formula
The formula field stores a formula utilizing constant1, constant2, variable1 and variable2.
For example, the formula field may contain a calculation like this:
constant1 * variable1
When I use a select statement like this:
SELECT constant1, constant2, variable1, variable2, formula
FROM table
How do I retrieve the calculation result of constant1 * variable1 based on formula field?
You need one of two approaches:
Call some sort of eval function on your formula.
Parse the formula into an expression tree, substitute the values and then evaluate the expression.
Usig eval is quick to implement but dangerous because it could do something you didn't expect. Parsing the formula is best done with a dedicated library for building parsers, but if your expression syntax is very simple you could write a parser yourself without using a library. In either case SQL isn't the right language for this.
I'd recommend finding a library that can help you parse the formula. Whichever route you choose though, I'd suggest doing the bulk of the work on the database client, not in SQL. SQL was simply not designed for this sort of task.

Is there any way to modify a column before it is ordered in MySQL?

I have a table with a field value which is a varchar(255). The contents of the field can be quite varied:
$1.20
$2994
$56 + tax (This one can be ignored or truncated to $56 if necessary)
I have a query constructed:
SELECT value FROM unnamed_table ORDER BY value
However, this of course uses ASCII string comparison to order the results and does not use any numerical type of comparison.
Is there a way to truly order by value without changing the field type to DECIMAL or something else? In other words, can the value field be modified ('$' removed, value converted to decimal) on the fly before the results are sorted?
You could sort on an expression made to "parse" the text into decimal
SELECT value FROM unnamed_table ORDER BY expression_returning_decimal(value)
Where your expression uses MySQL functions to extract the number from the string depending on what form you expect it to take (something like CAST(value AS DECIMAL(10,2)), but you'll probably need to deal with the extraneous non-numeric characters somehow as I'm not sure what the CAST will give if you don't strip them).
Create a second column without the $-sign, sort on that one and use the data of the original column in your application.
In order to create the helper column and sort on it you would need something like this:
SELECT value, CAST(SUBSTR(value, 2) AS UNSIGNED) sort_col
FROM unnamed_table ORDER BY sort_col