What I get on Google's search results are on creating a query.
What I am looking for is an excel like function, though I use MS Access.
How am I able to automatically get a concatenated Week & SowOrder appear on EN?
0101
0102
I use 2010 Access but 2003 format.
You can concatenate those fields in a query.
SELECT [Week], SowOrder, [Week] & SowOrder AS EN
FROM YourTable;
Then you can use the query anytime you need to see EN.
If you need to store those concatenated values in your table, you can use an UPDATE query.
UPDATE YourTable
SET EN = [Week] & SowOrder;
However, storing the values means you need to remember to execute the UPDATE again any time the Week or SowOrder values change.
Note you could use + instead of & for concatenation. The difference between those two operators is how they behave with Null:
"foo" & Null yields "foo"
"foo" + Null yields Null
I'm not sure how you'd get the leading zeros from each field to display if they are of number types, or if your data would change to '10', '11', '12'...'41','42','43.. and so on.
However, if Fields 1 and 2 are text, Field three would be a Calculated field as
=[week]&[SowOrder]
Check this, use + sign to concatenate in your case
Calculated field
Building on HansUp's answer, you probably want
SELECT [Week], [SowOrder], Format([Week],"00") & Format([SowOrder],"00") AS EN
The Format functions will force the inclusion of leading zeroes on any single-digit numbers.
Related
I am new to the community so please bear with me. I am working on a sum function that will take the values of 3 columns (Exchange, Commission, Otherfees) and give me that total based on row. The datatypes for these 3 fields are VARCHAR. I started by using a CONVERT function and then addressed any NULLs. Please see the query below:
SELECT SUM(
(SELECT(SELECT
CONVERT(decimal(18,4), isnull(ExchangeFee,0)) AS decimal
FROM T_TABLE) as EXCHANGE_VALUE) +
(SELECT(
SELECT
CONVERT(decimal(18,4), isnull(Commission,0)) AS decimal
FROM T_TABLE) AS COMMISSION_VALUE) +
(SELECT(
SELECT
CONVERT(decimal(18,4), isnull(OtherFees,0)) AS decimal
FROM T_TABLE) AS OTHERFEES_VALUE) AS decimal) AS SUMMED_VALUE
When running this query, I get the message
'SUM' is not a recognized built-in function name.
Please let me know your thoughts.
You could start by using the correct data types for your fields.
ExchangeFee, Commission and OtherFees are all numeric, so why store them in a varchar?
If the values should never be NULL, and here these look like they probably probably shouldn't, set them as NOT NULL and default them to 0.
That said, mysql will convert strings to numbers in a numerical context so you only need to worry about any NULL values which COALESCE or IFNULL will deal with.
As for the query which you want to sum the rows, all of the data is coming from T_TABLE so the general structure of the query should be:
SELECT COALESCE(ExchangeFee,0) + COALESCE(Commission,0) + COALESCE(OtherFees,0) AS SUMMED_VALUE
FROM T_TABLE;
I have 2 tables with "like" fields and am simply trying to "flag" all records where the "like" fields are different. For example, I have a description field in table #1 and a description field in table #2.
I created a new field titled Description_Diff with the formula Descr_diff: IIf([Tbl 1 items].[description]<>[Tbl 2 items].[description],"diff","").
The issue I am having is that if one of the two values is Null/Blank it does not return a "diff". I apologize in advance if this answer had already been covered, but upon searching the site was unable to find the answer.
Try with Nz:
Descr_diff: IIf(Nz([Tbl 1 items].[description])<>Nz([Tbl 2 items].[description]),"diff",Null)
The real answer is that you can't compare Null using any logical operator.
So If 1 <> Null doesn't work as a comparison.
Have a read here http://allenbrowne.com/casu-11.html and http://allenbrowne.com/casu-12.html for a fuller explanation.
use coalesce
usage:
coalesce(value, defaultValue)
when comparing if it's null, throw in "" or -1 as a default value, then comparing with blank or default would return false
Use the coalesce in your query in the select
Select coalesce(id,-1), coalesce(firstname,"") From myTable
In a MySQL table i have a field, containing this value for a given record : "1908,2315,2316"
Here is my sql Query :
SELECT * FROM mytable WHERE 2316 IN (myfield)
I got 0 results!
I tried this :
SELECT * FROM mytable WHERE 2315 IN (myfield)
Still 0 results
And then i tried this :
SELECT * FROM mytable WHERE 1908 IN (myfield)
Surprisingly i obtained the record when searching with 1908! What should i do to also obtain the record when searching with 2315 and 2316 ? What am i missing ?
Thanks
You appear to be storing comma delimited values in a field. This is bad, bad, bad. You should be using a junction table, with one row per value.
But, sometimes you are stuck with data in a particular structure. If so, MySQL provides the find_in_set() functions.
SELECT *
FROM mytable
WHERE find_in_set(2316, myfield) > 0;
You can't use IN() over comma separated list of no.s its better to normalize your structure first for now you can use find_in_set to find results matching with comma separated string
SELECT * FROM mytable WHERE find_in_set('1908',myfield) > 0
This question has been asked and answered before, but I don't want to hunt for it; this question should be closed as a duplicate. But, to answer your question:
The commas in the string, the column value, are just characters. Those are part of the string. They aren't seen as "separators" between values in the SQL text. The way SQL sees it, the column contains a single value, not a "list" of values.
So, in your query, the IN (field) is equivalent to an equals comparison. It's equivalent to comparing to a string. For example:
... WHERE 2316 = '1908,2315,2316'
And those aren't equal, so the row isn't returned. The "surprisingly" finding of a match, in the case of:
... WHERE 1908 IN ('1908,2315,2316')
that's explained because that string is being evaluated in a numeric context. That is, the comparison returns true, because all of these also true:
... WHERE 1908 = '1908,2315,2316' + 0
... WHERE 1908 = '1908xyz' + 0
... WHERE 1908 = '1907qrs' + 1
(When evaluated in a numeric context, a string gets converted to numeric. It just happens that the string evaluates to a numeric value that equals the integer value it's being comparing to.)
You may be able to make use of the MySQL FIND_IN_SET function. For example:
... WHERE FIND_IN_SET(2316,'1908,2315,2316')
But, please seriously reconsider the design of storing comma separated list. I recommend Bill Karwin's "SQL Antipatterns" book...
http://www.amazon.com/SQL-Antipatterns-Programming-Pragmatic-Programmers/dp/1934356557
In mysql IN clause is utilized as
SELECT * FROM mytable WHERE column_name IN (set_of_values) ;
Mention column name instead of values
Please try
SELECT * FROM mytable WHERE LOCATE(CONCAT (',', 2316 ','), CONCAT (',',myfield,',' ) ) <>0
What I'm Using: The most recent MySQL on Ubuntu 12.
The Set Up: Suppose I have a table "EmployeePayment" with "Name" and "Hours" for each employee. Suppose I already have it populated with values.
The Question: When I use the command
select * from EmployeePayment where Name in ('');
I get the empty set, as I'd expect. But, when I use
select * from EmployeePayment where Name in ('' or '');
I get the entire table returned. Moreover, if I'm picky and put in the command
select Name, SUM(Hours) from EmployeePayment where Name in ('' or '');
then it only returns whatever is the top name from the table. What's happening with this "in" command?
First off, you need to get rid of the or, the proper syntax for the in clause uses commas to separate the possibilities, such as:
sql> select name from people where status in ('intelligent', 'good looking')
pax
1 row returned
What your current variant is doing is applying the or operator to give you a one-element in-list. See here for more detail.
The reason why you're only getting one row for the aggregated query is because you have no group by clause, so you're grouping all rows. Most DBMS' would then complain about having a non-aggregated column that isn't part of the grouping, but MySQL is a bit fancy-free and footloose with the rules in that regard.
It's obviously grouping over the whole table (as it should) but applying some default aggregating function to the name (which it probably shouldn't, but does according to its documentation).
This MySQL extension is covered here but heed the warning: MySQL can choose any of the myriad possible values for these non-aggregated, non-group-by columns, so it's more useful when you know that all the rows in a given group share the same value for the column.
You're effectively doing this:
select * from EmployeePayment where Name in (0);
The OR expression evaluates to 0, and WHERE Name IN (0); returns all rows. You have to use the proper IN syntax as suggested in the other answers:
SELECT * FROM EmployeePayment WHERE Name IN ('foo', 'bar');
IN uses comma separated values, for example: WHERE Name IN ('tim','beth')
So try WHERE Name IN ('','');
But more importantly, why would you want to check where a value is empty or empty? Or was that just to get the question across?
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in
I have a query that looks at two times and calculates the difference and puts the value (in seconds) in another field. I then sort that field. The problem is when one of the times is empty the row in the new field in not populated so when I do the sort the rows that nothing in them are pushed to the top.
What I would like to do is replace the null with another value. Something like 9999999. That way when I perform the sort the rows that now have 999999 will be place at the bottom of the sort.
Here is the SQL Expression for the query.
SELECT FOCFClassic.FirstName, FOCFClassic.LastName, FOCFClassic.[Bib#], FOCFClassic.[2011SDStartTime], FOCFClassic.[2011SDFinishTime], Diff2Dates("ns",[FOCFClassic].[2011SDStartTime],[FOCFClassic].[2011SDFinishTime]) AS 2011SDRunTime, FOCFClassic.SDCategory, FOCFClassic.Team, FOCFClassic.SDRank, DateDiff("s",[2011SDStartTime],[2011SDFinishTime]) AS TotalTime
FROM FOCFClassic
WHERE (((Diff2Dates("ns",[FOCFClassic].[2011SDStartTime],[FOCFClassic].[2011SDFinishTime]))<>"") AND ((DateDiff("s",[2011SDStartTime],[2011SDFinishTime])) Is Not Null))
ORDER BY FOCFClassic.SDRank, DateDiff("s",[2011SDStartTime],[2011SDFinishTime]);
I'm a noob at this so a little hand holding my be needed.
Thanks is advance!
Gordon
You can either use the NZ() function: NZ(MyDate, #1/1/1950#) will return jan 1st 1950 if the field is null, the field value otherwise. You can achieve the same result using IIF() and ISNULL() functions or IS NULL condition.
In terms of performance, using IIF(myDate IS NULL, #1/1/1950#, myDate) should be the fastest.