MS Access If Function - sql

I'm not using SQL at this point, I'm just trying to define a function using query design function in MS Access 2010.
What i'm trying to do:
So turns out that I have a 5 month spread (Jan,Feb..May) where each month is a column. Turns out that at times May has a value and January does not, but it should. All the values are either one or null.
What I'm trying to do is write an if function of this sort:
Jan15new: Iff([May-15]=1,[Jan-15]=1,[Jan-15])
However, when I run the query with this iff function I got a column full of negative ones that doesn't abide by the rules of this if function.
If you can shed somelight that would be great!
thanks,

This formula returns a 1 if May-15 = 1, otherwise returns whatever value is in Jan-15:
Jan15new: IIf([May-15]=1,1,[Jan-15])

If the values in your formula are Boolean values, you do not need to compare them to anything, and you should not be comparing them to numbers:
Jan15new: IIf([May-15]=True,[Jan-15]=True,[Jan-15])
That's actually meaningless, because it is equivalent to this:
Jan15new: IIf([May-15],[Jan-15],[Jan-15])

Related

Why Column Viti Is null?

I want Viti column to have value 1 if #semestri is < 2 and so on but instead of that I got nulls
I am new in sql server Thanks for answers
This doesn't answer your question, but your code has lots of issues.
A function returns varchar without a length. Always include a length with strings in SQL Server.
The function returns a string but the return values are all ints.
#viti is passed in, but is never used.
The subquery will never have a match, because the where clause has an uninitialized variable. This is where your NULL values comes along.
In addition, I'm pretty sure you can do whatever you want with a case expression, which would simplify the code.

ADO MAX Time query

I'm using an ADO\ACE.OLEDB query to access data in an excel sheet. One of the columns in this sheet is a time stamp in the 00:00:00 format.
My select max query for this column works OK but it returns the wrong "max" time. For example, if I have the following time stamps:
08:15:00
09:45:00
10:00:00
The SELECT MAX query will return 09:45:00, and ignore any subsequent timestamps. If I go to the queried sheet, and convert all the cells in that column to "Time" using text-to-columns, the query will then return the correct value of 10:00:00.
The timestamp that is recorded in the excel sheet is updated very often by many different people throughout the day using their own ADO Update\Insert query so it's not feasible for me to open the sheet and convert the cells to the correct type manually. The update\insert queries have the cell types set to General.
So my question is this.
Is there a way to either
A) Specify the data type in the update\insert into ADO query so the cell has the correct data type? Or
B) Force the SELECT MAX query to interpret the column as a timevalue rather than text?
If not any suggestions on how to procede?
EDIT: I looked closer at the excel sheet i'm querying. I noticed that each cell has a singlequote preceding it. I think if I can get that removed when entries are recorded excel might properly format the cell as time.
Here is a basic example of wha is being used to write entries to the sheet.
Update [Sheet1$] Set StopTime='09:45:00' WHERE Date= '3/12/2014' AND StartTime='9:30:00'
Can this be rewritten so a single quote ins't included? Note there is no end quote.
The single quote at the beginning of a cell in Excel forces Excel to interpret the cell value as text. The single quote is probably being inserted because the update statement uses single quotes, which indicate strings. You may be able to use ## delimiters instead, which specifically indicate date/time values (I say "may" because I haven't tested this). For example:
UPDATE [Sheet1$]
SET StopTime = #09:45:00#
WHERE [...]
On the other side, you can force a column to be interpreted as a date/time by using the CDate function:
SELECT MAX(CDate(StopTime)) AS MaxTime
FROM [Sheet1$]
You just have to be careful in this case about values that cannot be converted to a valid time, which will cause the query to error out.
I didn't even realise this but the examples I gave were flawed. What is logged does NOT include a leading zero. Since I'm using a 24hr time I was able to correct this issue by writing a leading zero into the time that gets recorded.
Basically it's an, if LEN(time)=7 then time = 0 & time.
This appears to be working so far.

Microsoft Access 2010 SQL Split String at "X" and Multiply

I have a table with a package size column with a data type of text that I need to convert to an integer for mathmatical reasons. The values in this column typically look something like "100ML","20GM","UD 20","13OZ" here is where it gets tricky there are occasionally values like "6X12ML","UD 5X6ML". The ones with the "X" in them I need to remove the "ML" I'm currently doing this with
Replace([TABLE_NAME].[COLUMN_NAME],"ML","")
in an expression column in a query. I can use nested Replace functions to remove the "ML","GM","OZ" and "UD ". All of my attempts to do this have failed, I figured the end solution would be something like
IIf([TABLE_NAME].[COLUMN_NAME] Like "X", (CInt(Left([TABLE_NAME].[COLUMN_NAME],InStr(1,[TABLE_NAME].[COLUMN_NAME],"X")-1))*CInt(Right([TABLE_NAME].[COLUMN_NAME],InStr(1,[TABLE_NAME].[COLUMN_NAME],"X")+1))),[TABLE_NAME].[COLUMN_NAME])
I have tried using a variation of the code above with no avail. All suggestions are appreciated, I would preffer to get this knocked out in one query but I do realize I can use and expression and just split the text before and after the "X" into two differenct expression columns. Then use another query to multiply the values.
QTY_ORDERED: IIf(InStr(1,Replace(Replace(Replace(Replace([STANDARD_PRICING].[PACKAGE_AMOUNT],"GM",""),"ML",""),"UD","")," ",""),"X")>1,[CRX_HISTORIC_PO].[QUANTITY]/Left(Replace(Replace(Replace(Replace([STANDARD_PRICING].[PACKAGE_AMOUNT],"GM",""),"ML",""),"UD","")," ",""),InStr(1,Replace(Replace(Replace(Replace([STANDARD_PRICING].[PACKAGE_AMOUNT],"GM",""),"ML",""),"UD","")," ",""),"X")-1)*Right(Replace(Replace(Replace(Replace([STANDARD_PRICING].[PACKAGE_AMOUNT],"GM",""),"ML",""),"UD","")," ",""),Len(Replace(Replace(Replace(Replace([STANDARD_PRICING].[PACKAGE_AMOUNT],"GM",""),"ML",""),"UD","")," ",""))-InStr(1,Replace(Replace(Replace(Replace([STANDARD_PRICING].[PACKAGE_AMOUNT],"GM",""),"ML",""),"UD","")," ",""),"X"))*-1,[CRX_HISTORIC_PO].[QUANTITY]/Replace(Replace(Replace(Replace([STANDARD_PRICING].[PACKAGE_AMOUNT],"GM",""),"ML",""),"UD","")," ","")*-1)
The code above is what I used to complete the task at hand.

Excel 2007 (Conditional Formatting) AND & IF

I hope you can help me on this issue.
I am currently using Excel 2007 and I am creating a dynamic Planning/Time Sheet for our Team.
So far everything is going well.
Now unfortunately I am having an issue with the Conditional Format.
I am formatting the Cells in order to graphically show the current Status of the Person working. I am using the Conditional Format with a Formula example: =OFFSET(DataStart17D;COLUMN();ROW()-49;1;1)="PM"
Now I am trying to applying 2 conditions with a gradient fill of 2 colors like example: =AND(IF(OFFSET(DataStart17D;COLUMN();ROW()-49;1;1)="PM";TRUE;FALSE);IF(OFFSET(DataStart17D;COLUMN();ROW()-52;1;1)="AM";TRUE;FALSE)
Problem is, as soon as I use the IF or the AND Statement no condition is applied at all.
I have applied the above formula to the Cell itself and have received "TRUE" as the condition.
What is odd too is that if I apply =OFFSET(DataStart17D;COLUMN();ROW()-49;1;1)="PM" it works fine, if I use =IF(OFFSET(DataStart17D;COLUMN();ROW()-49;1;1)="PM";TRUE;FALSE) once again no condition applies.
I have searched the web for a Solution and could not find one yet :(
Would really be pleased if someone could help me on this one :)
Best Regards,
Richard J. Dana
You don't need IF Statment and TRUEs and FALSEs withih a Conditional Formatting formula. By its nature the formula is conditional.
Try something like:
=AND(OFFSET(DataStart17D;COLUMN();ROW()-49;1;1)="PM";OFFSET(DataStart17D;COLUMN();ROW()-49;1;1)="AM")
Once you do so, you'll see an additional problem. The two statements in your AND function are mutually exclusive, so it will never evaluate to TRUE.
EDIT:
There does seem to be an issue with the AND statement and multiple OFFSET statements that use ROW or COLUMN
Please note that I'm going to use commas instead of semicolons as function parameter separators here, otherwise it's too hard to convert. You'll have to change the commas back to semicolons.
Also note that you didn't need the last two arguments in either the ROW or COLUMN function in your original question. You had them set to a height and width of 1, which is the default, and is optional.
Here's a simplified example:
If you do something like:
OFFSET(DataStart17D,ROW()+1,COLUMNS())="PM"
it will evaluate to TRUE in the worksheet and also in the conditional formatting
If you do something like:
=AND(OFFSET(DataStart17D,ROW()+1,COLUMNS())="PM",OFFSET(DataStart17D,ROW()+1,COLUMNS())="PM")
which is just repeating the same statement twice, it will evaluate to TRUE in the worksheet, but won't trigger the conditional format.
All of the above is just as you stated in your question. The answer that I think works is to use ROWS and COLUMNS instead, like this:
=AND(OFFSET(DataStart17D,ROWS($1:2)+1,COLUMNS($A:A))="PM",OFFSET(DataStart17D,ROWS($1:2)+1,COLUMNS($A:A))="AM")
The above would be the formula for A2. Note that the first row or column inside the parentheses is anchored with a dollar sign. This gives you the count of rows or columns from A1, effectively the same thing as the ROW or COLUMN function.

SQL find-and-replace regular-expression capturing-group limit?

I need to convert data from a spreadsheet into insert statements in SQL. I've worked out most of the regular expressions for using the find and replace tool in SSMS, but I'm running into an issue when trying to reference the 9th parenthesized item in my final replace.
Here is the original record:
Blue Doe 12/21/1967 1126 Queens Highway Torrance CA 90802 N 1/1/2012
And this is what I need (for now):
select 'Blue','Doe','19671221','1126 Queens Highway','Torrance','CA','90802','N','20120101'
Due to limitations on the number of parenthesized items allowed I have to run through the replace three times. This may work into a stored procedure if I can make first make this work as a POC.
This is the first matching expression:
^{:w:b:w:b}{:z}/{:z}/{:z:b[0-9A-Za-z:b]+:b:w:b[A-Z]+:b:z:b:w:b}{:z}/{:z}/{:z}
And the replace: \10\2/0\3/\40\5/0\6/\7
This adds zeros to the months and days so that they have at least two characters.
The next match reformats the dates into the format required in the query (no comments about not using a date field. This is a client requirement for the database).
Matching expression:
^{:w:b:w:b}[0-9]*{[0-9]^2}/[0-9]*{[0-9]^2}/{:z}{:b[0-9A-Za-z:b]+:b:w:b[A-Z]+:b:z:b:w:b}[0-9]*{[0-9]^2}/[0-9]*{[0-9]^2}/{:z}
And the replace: \1\4\(2,2)\(2,3)\5\8\(2,6)\(2,7)
Finally, the final match inserts the results into the SQL statement that will get used in an insert statement.
Matching expression:
^{:w}:b{:w}:b{:z}:b{[0-9A-Za-z:b]+}:b{:w}:b{[A-Z]+}:b{:z}:b{:w}:b{:z}
And the replace: select '\1','\2','\3','\4','\5','\6','\7','\8','\9'
It all works except the last replacement. For some reason the \9 is NOT getting the data from the match. If I just replace the whole replace expression with \9 I get a blank space. If I use \8, I get N. If I eliminate the 8th parenthesized item, thus making my 9th item eighth, it returns what I want, 20120101.
So my question is, does SSMS / SQL allow for 9 tagged expressions when using find / replace and regular expressions? Or am I missing something here? I know there are other ways to do this. I'm just trying to get it done quickly as a POC before we move this into a sproc or application.
Thanks for any assistance.
-Peter
None of your matching expressions work with the record you provided in my MS SQL Server Management Studio 2008r2.
From your description it sounds like there is an issue with the Tagged Expression 9 since the desired result is returned when using Tagged Expression 8, but not 9. You may want to ask Microsoft or report it as a bug.
A quicker solution would be to move the text you are performing the Find/Replace on in SSMS to a spread sheet and use cell formulas to parse the data into insert commands. If you have MS Excel the CONCATENATE, FIND, and MID functions will probably be useful. Also, it helps to split the values into their own columns so you can format the date, then use one concatenate to build your insert.
Please let me know if you need an example.
Update: I tried your example in MS SQL Server Management Studio 2008r2, Visual Studio 2005, and Visual Studio 2010 with the same result you get, \9 returns an empty string. Checking around I found that others are also having this issue (see the community content from Henrique Evaristo) and that the whole system has been replaced in the new editors.
So in answer to your question, SSMS does not support 9 tagged expressions due to a bug.
If you are unable to use the Spreadsheet idea you could try splitting the action into two parts, setting the first 8 values, then swinging back again to do the last. For example:
^{:w}:b{:w}:b{:z}:b{[0-9A-Za-z:b]+}:b{:w}:b{[A-Z]+}:b{:z}:b{:w}:b:z
select '\1','\2','\3','\4','\5','\6','\7','\8','\0'
:w:b:w:b:z:b[0-9A-Za-z:b]+:b:w:b[A-Z]+:b:z:b:w:b{:z}
\1