Force Short TextField To Always Be X Characters - ms-access

I am attempting to use a IIF() statement in a query to update a short text field to always be 4 characters. This is my syntax, but
SELECT DISTINCT IIf(Len([User ID] = 1), "000" & [User ID], IIf(Len([User ID] = 2), "00" & [User ID], IIf(Len([User ID] = 3), "0" & [User ID], [User ID]))) AS ST
FROM _TestData;
However it seems to be appending 0's to the data regardless of length?

It could be this simple:
SELECT DISTINCT Right("000" & [User ID], 4) AS ST
FROM TestData;

If [User ID] is number, try this:
SELECT DISTINCT Format([User ID], "0000") AS ST
FROM _TestData;
Update
Missed that the field is short text. In this case
SELECT DISTINCT Format(CLng(Nz([User ID],0)), "0000") AS ST
FROM _TestData;

Related

Create duplicate row for each item in column containing delimited string

I have a table with a column that stores time blocks separated by semi-colons (;). I would like to create a row for each time block, for example, given
I'd like to create a row for each time block
Please let me know if this possible in Access.
Edit
I tried using this query
SELECT * INTO ImportedData
FROM (
SELECT [SourceData].[Time block], [SourceData].[Work History Id],[SourceData].[Operation Code]
FROM SourceData
WHERE InStr([SourceData].[Time block], ';') = 0
UNION ALL
SELECT Left([SourceData].[Time block], InStr([SourceData].[Time block], ';') - 1),[SourceData].[Work History Id], [SourceData].[Operation Code]
FROM SourceData
WHERE InStr([SourceData].[Time block], ';') > 0
UNION ALL
SELECT Mid([SourceData].[Time block], InStr([SourceData].[Time block], ';') + 1), [SourceData].[Work History Id], [SourceData].[Operation Code]
FROM SourceData
WHERE InStr([SourceData].[Time block], ';') > 0) AS CleanedUp;
and I also tried this VBA code with no luck.
Public Sub addToTable()
Dim rstObj As DAO.Recordset, dbObj As DAO.Database
Dim InsertSQL As String
Set dbObj = CurrentDb()
Set rstObj = dbObj.OpenRecordset("Query1")
Do While Not rstObj.EOF
Dim memArr() As String
memArr = Split(rstObj.Fields("Time block"), ",")
For i = 0 To UBound(memArr)
InsertSQL = "SELECT*INTO ImportedData(Time block, Work History ID) VALUES(""" & rstObj.Fields("Time block") & """, """ & memArr(i) & """)"
DoCmd.RunSQL (InsertSQL)
Next
rstObj.MoveNext
Loop
End Sub
Found the answer here [enter link description here][1]
[1]: http://www.access-programmers.co.uk/forums/showthread.php?t=239727 for anyone that may b have the same requirements, i would hope people would actually help instead of just marking down answers or correcting the way the question is asked.

Combining 2 insert into in one command

I couldn't figure this out:
I want to add a row in a table-a and 3 columns of this row will come from table-b and other 2 columns will come from e.g textboxes...
This code didn't work...
SqlCommand cmd35 = new SqlCommand("INSERT INTO BTmr (Barcode,[Machine Name],[Machine ID]) SELECT Barcode,[Machine Name],[Machine ID] FROM BkmP WHERE barcode like '" + c13 + "%' UNION INSERT INTO BTmr([Repair Cost],[Repair Date],Barcode)values (#cst,#rprd)", connection);
cmd35.Parameters.AddWithValue("#cst", textBox10.Text);
cmd35.Parameters.AddWithValue("#rprd", dateTimePicker1.Text);
Just put the parameters as static values in the select statement.
SqlCommand cmd35 = new SqlCommand("INSERT INTO BTmr (Barcode,[Machine Name],
[Machine ID],[Repair Cost],[Repair Date]) SELECT Barcode,[Machine Name],
[Machine ID],#cst AS [Repair Cost],#rprd AS [Repair Date] FROM BkmP WHERE
barcode like '" + c13 + "%', connection);
cmd35.Parameters.AddWithValue("#cst", textBox10.Text);
cmd35.Parameters.AddWithValue("#rprd", dateTimePicker1.Text);
While your're at it, parameterize that where clause: https://stackoverflow.com/a/251380/123422

SELECT Between dates almost working

My problem is likely all about date formatting in a SELECT.
In an asp file I open an ADO Recordset wanting to retrieve rows of a MS SQL table that fall between date1 (08/15/2013) and date2 (08/22/2013) (i.e., the previous 7 days from today's date.)
The SELECT does retrieve the appropriate 2013 rows but also retrieves rows going back to 08/15/2012.
Here is the SELECT:
oRS.Source = "SELECT * FROM aTable WHERE entry_Date BETWEEN '" & resultLowerDate & "' AND '" & resultCurrentDate & "' AND entry_Status <> 'INACTIVE'"
resultLowerDate = 08/15/2013 and resultCurrentDate = 08/22/2013.
The table is set up as follows with resultCurrentDate = "08/22/2013":
entry_Status entry_Date (varchar) LastName FirstName SELECT Result
INITIAL 08/15/2012 Smith Jim YES
INACTIVE 08/21/2012 Green Tom no
INITIAL 08/22/2013 Jones Mary yes
FOLLOWUP 08/22/2013 Jones Mary yes
FOLLOWUP 08/22/2013 Brown Sally yes
FOLLOWUP 08/22/2013 Smith Jim yes
Any thoughts as to why the INITIAL 08/15/2012 row gets selected along with the other rows that meet the SELECT query?
STOP STORING DATES IN VARCHAR COLUMNS! And STOP CONCATENATING STRINGS, USE PROPER PARAMETERS.
Sorry to yell, but we are getting multiple questions a day where people use the wrong data type for some unknown and probably silly reason, and these are the problems it leads to.
The problem here is that you are comparing strings. Try:
"... WHERE CONVERT(DATETIME, entry_date, 101)" & _
" >= CONVERT(DATETIME, '" & resultLowerDate & "', 101)" & _
" AND CONVERT(DATETIME, entry_date, 101)" & _
" < DATEADD(DAY, 1, CONVERT(DATETIME, '" & resultCurrentDate & "', 101))"
Or better yet, set resultLowerDate and resultUpperDate to YYYYMMDD format, then you can say:
"... WHERE CONVERT(DATETIME, entry_date, 101) >= '" & resultLowerDate & "'" & _
" AND CONVERT(DATETIME, entry_date, 101) < DATEADD(DAY, 1, '" & resultCurrentDate & "'"
Note that I use an open-ended range (>= and <) instead of BETWEEN, just in case some time slips into your VARCHAR column.
Also note that this query could fail because garbage got into your column. Which it can, because you chose the wrong data type. My real suggestion is to fix the table and use a DATE or DATETIME column.
The fact that your entry_Date column is a varchar field and not an actual date is the problem. If you cast it to a datetime during the select, you'll get the results you expect.
select *
from aTable
where cast(entry_Date as datetime) between '08/15/2013' and '08/22/2013'
Sql Fiddle link

checking whether field in table has space or comma - MS-Access

I have table called FinalForgotten which only contains one field called aname. The field could either look like Smith John or Smith,John. So both last and first name are in same field and delimited by either space or comma. The defense field contains three fields: first_name,last_name,middle_initial. The first_name field will contain data that matches exactly a piece a data IN aname field (e.g. John). And the last_name field will contain data that matches exactly a piece of data IN aname field (e.g. Smith). I'm trying to get all the FinalForgotten aname records with a middle initial into a new table (e.g. Smith,John S). The defense table is what has this middle initial.
This would work:
SELECT left([aname],InStr(1,[aname],",")-1) & " "& right([aname],Len(aname)-InStr(1,[aname],",")) & " "& summary_judgment.middle_initial AS fullnameINTO FinalForgottenWithMiddle FROM FinalForgotten INNER JOIN summary_judgment ON((left(FinalForgotten.aname,InStr(1,FinalForgotten.[aname],",")-1))=summary_judgment.last_name) AND((right(FinalForgotten.aname,Len(FinalForgotten.aname)-InStr(1,FinalForgotten.[aname],","))=summary_judgment.first_name));
But it will return "invalid procedure call" should FinalForgotten contain a field that doesn't have a comma like:
Smith John.
Hence, to address this, I tried to factor whether a comma was in the field or not:
SELECT left([aname], IIF(instr([aname], ",") = 0, InStr(1,[aname]," ")-1),InStr(1,[aname],",")-1) & ", " & right([aname], IIF(instr([aname], ",") = 0,Len(aname)-InStr(1,[aname]," "),Len(aname)-InStr(1,[aname],",") & " " & defense_final.middle_initial AS fullname INTO FinalForgottenWithMiddle
FROM FinalForgotten INNER JOIN defense_final ON
((right(FinalForgotten.aname,IIF(instr([aname], ",") = 0,Len(FinalForgotten.aname)-InStr(1,FinalForgotten.[aname]," ")),Len(FinalForgotten.aname)-InStr(1,FinalForgotten.[aname],","))=defense_final.first_name))
AND
((left(FinalForgotten.aname,,IIF(instr([aname], ",") = 0,InStr(1,FinalForgotten.[aname]," ")-1)),InStr(1,FinalForgotten.[aname],",")-1))=defense_final.last_name);
This gives me a "missing operator syntax" error and highlights the word AS.
Thanks for response.
There seems to be quite a few missing parentheses.
SELECT left(
[aname],
IIF(instr([aname], ",") = 0,
InStr(1,[aname]," ")-1,
InStr(1,[aname],",")-1
)
)
& ", " &
right(
[aname],
IIF(instr([aname], ",") = 0,
Len(aname)-InStr(1,[aname]," "),
Len(aname)-InStr(1,[aname],",")
)
)
& " " &
defense_final.middle_initial AS fullname
INTO FinalForgottenWithMiddle
FROM FinalForgotten
INNER JOIN defense_final
ON
right(FinalForgotten.aname,
IIF(instr([aname], ",") = 0,
Len(FinalForgotten.aname)-InStr(1,FinalForgotten.[aname]," "),
Len(FinalForgotten.aname)-InStr(1,FinalForgotten.[aname],",")
)
)=defense_final.first_name
AND
left(FinalForgotten.aname,
IIF(instr([aname], ",") = 0,
InStr(1,FinalForgotten.[aname]," ")-1,
InStr(1,FinalForgotten.[aname],",")-1
)
)=defense_final.last_name

Microsoft Access Data Base .. Select Query

i am doing queries practice in Microsoft access. i want to concatenate the first name with the fax number.. the fax number is like (123)555-0103 ..
i`m doing that
select [first name] +' ''s Fax Number is' +str(fax number) as [Mariya`s Fax Number]
from employees where id =4;
but it is giving error..
That would be:
select [first name] & " ''s Fax Number is " & [fax number] as [Mariya`s Fax Number]
from employees where id =4
You should use & to concatenate
You should use '' for each single quote
You should use double quotes (") for strings.