In Telerik radrotator(legand) date is binding as system.byte:
Dim mssQL=" case when a.log_type='Schedule' then" & _
" (select case when e.schedule_type='Call Log' then cast( concat(d.user_firstname,' ',d.user_lastname,' ','Scheduled a Call On',' ',(DATE_FORMAT(e.schedule_date,'%d-%m-%Y') ) ) as char )" & _
" when e.schedule_type='Meeting' then cast(concat(d.user_firstname,' ',d.user_lastname,' ','Scheduled a Meeting On', ' ',(DATE_FORMAT(e.schedule_date,'%d-%m-%Y') )) as char )" & _
" when e.schedule_type='Mail Log' then cast (concat(d.user_firstname,' ',d.user_lastname,' ','Scheduled Mail On',' ',(DATE_FORMAT(e.schedule_date,'%d-%m-%Y'))) as char) end from crm_trn_tschedulelog e where e.log_gid=a.log_gid group by a.log_gid)"
The problem is with your CAST() and concat() functions. The concat() will give you result as system.byte when the input parameters to the function are of different types, here you are concatenating string and DATE Together. you you need to cast the Formatted Date also and remove the cast before the Concat(). Hence your query will be like the following:
concat(d.user_firstname,' ',d.user_lastname,' ','Scheduled Mail On',' ',cast (DATE_FORMAT(e.schedule_date,'%d-%m-%Y') as char)))
Related
I am trying to use the query below to insert a concatenated converted set of integers to string for use on a datetime field in my table.
TABLE
Field Type
empID int(11)
time_stamp datetime
in_out char(3)
am_pm char(2)
QUERY
Dim query As String = "INSERT INTO attendance VALUES(" & empID.Text & _
"STR_TO_DATE(CONCAT("& empYear.Text & ",'-'," & empMonth.Text & ",'-'," & _
empDay.Text & ",' '," & empHour.Text & ",':'," & empMin.Text & ",':'," & _
empSec.Text & ",'%Y-%m-%d %H:%i:%s'),'out','pm')"
There is no problem with the connection and the values. I have tried to insert the values into a test column of string type and the output is this:
133201712311827
I am pretty sure it's with how I use these characters: '' "" "," - :. I just can't figure out how.
First problem I see, here
& empID.Text & "STR_TO_DATE(. . . .
you're missing comma after first value
& empID.Text & "***,*** STR_TO_DATE(. . . .
Second issue, I identified when I've replaced your text values with hard coded values - You are missing closing parenthesis for str_to_date. Here ,'%Y-%m-%d... should be ), '%Y-%m-%d...
STR_TO_DATE(CONCAT(1999,'-',01,'-',01,' ',10,':',25,':',30***)***,'%Y-%m-%d %H:%i:%s')
As you see- my replacement shows that you have no issues with concatenation, single quote and :. Theo only other variable here is quality of data in text boxes.
Update
This answer (above) is correct. Using sql fiddle I created schema and when replaced text box values with hard-coded ones - all worked. My suggestions to add comma and parenthesis hold true. Your claim about problems with single quotes are false.
create table xxx (empID int(11), time_stamp datetime, in_out char(3), am_pm char(2));
INSERT INTO xxx VALUES(123,
STR_TO_DATE(CONCAT('2017','-','1','-','23',' ','10',':','35',':','40'),'%Y-%m-%d %H:%i:%s'),
'out','pm');
commit;
Select * from xxx
empID | time_stamp | in_out | am_pm
123 | January, 23 2017 10:35:40 | out | pm
End Update
On top of that, you could do it much better by parameterizing, which will look like something like this
command.CommandText = "insert into ... values (#1, #2, #3, #4)"
command.Parameters.AddWithValue("#1", Convert.ToInt32(empID.Text))
dim date as new DateTime(Convert.ToInt32(empYear.Text), Convert.ToInt32(empMonth.Text), . . . . )
command.Parameters.AddWithValue("#2", date)
. . . . . .
command.ExecuteNonQuery()
Parameterizing will make it easy to work with dates and strings
When I set up Allen Browne's ConcatRelated in a query to use a date range, I get ever comment in that range in each comment field. I want to group by an assembly line name but I get each line's comments. My SQL query is below. Any help would be greatly appreciated.
I have tried a sub query but I still get the same result or a syntax error depending on how I format.
SELECT
Asm_Equipment_Rate.Line_Name,
Avg(Asm_Equipment_Rate.Std_Pnls_Lbr_Hr) AS RAsm_Line_Std_Hrs,
Sum(Asm_Prod_Data.Lbr_Hrs) AS RAsm_Line_Total_Hrs,
([RAsm_Line_Std_Hrs]*[RAsm_Line_Total_Hrs]) AS RT100_Pct_Target,
([RT100_Pct_Target]*0.9) AS RT90_Pct_Target,
Sum(Asm_Prod_Data.Produced) AS RTotal_Produced,
Sum(Asm_Prod_Data.Backflushed) AS RTotal_Backflushed,
[RTotal_Produced]/[RT100_Pct_Target] AS RAsm_Line_EFF,
Sum(Asm_Prod_Data.Scrap_Qty) AS RAsm_Scrapped_Panels,
Sum(Asm_Prod_Data.Reworked) AS RAsm_Reworked_Panels,
IIf(([RAsm_Scrapped_Panels]+[RAsm_Reworked_Panels])=0,1,1-
([RAsm_Scrapped_Panels]+[RAsm_Reworked_Panels])/([RAsm_Scrapped_Panels]+
[RAsm_Reworked_Panels]+[RTotal_Produced])) AS RFYP,
ConcatRelated
('Comments',
'Asm_Prod_Data',
'PA_date Between ' & Format([Forms]![Date Prompt]!
[txtBDate],'\#yyyy-m-d\#') & ' And ' & Format([Forms]![Date Prompt]!
[txtEDate],'\#yyyy-m-d\#'),
'Comments',
', ') AS RConCat_Comments
FROM Asm_Equipment_Rate INNER JOIN Asm_Prod_Data ON
Asm_Equipment_Rate.Equipment = Asm_Prod_Data.P_Line
WHERE (((Asm_Prod_Data.PA_Date) Between [Forms]![Date Prompt]![txtBDate]
And [Forms]![Date Prompt]![txtEDate]))
GROUP BY Asm_Equipment_Rate.Line_Name;
Regards,
Bill
change the WHERE clause to
'PA_date Between ' &
Format([Forms]![Date Prompt]![txtBDate],'\#yyyy-m-d\#') &
' And ' & Format([Forms]![Date Prompt]![txtEDate],'\#yyyy-m-d\#') &
' And P_Line=' & Asm_Prod_Data.P_Line,
You'll need to quote the value if P_Line is text.
Also, I think when you call a function you need to use double quotes inside the function call, although if what you have there is working, that's not true. Maybe it's changed for later Access versions.
EDIT: I mean the WHERE clause in the function call
Try this
SELECT
Asm_Equipment_Rate.Line_Name,
ConcatRelated
('Comments',
'Asm_Prod_Data',
'PA_date Between '
& Format([Forms]![Date Prompt]![txtBDate],'\#yyyy-m-d\#')
& ' And ' & Format([Forms]![Date Prompt]![txtEDate],'\#yyyy-m-d\#')
& ' And P_Line=' & Asm_Prod_Data.P_Line,
'Comments',
', ') AS RConCat_Comments
FROM Asm_Equipment_Rate INNER JOIN Asm_Prod_Data
ON Asm_Equipment_Rate.Equipment = Asm_Prod_Data.P_Line
WHERE (((Asm_Prod_Data.PA_Date) Between [Forms]![Date Prompt]![txtBDate]
And [Forms]![Date Prompt]![txtEDate]))
GROUP BY Asm_Equipment_Rate.Line_Name,
ConcatRelated
('Comments',
'Asm_Prod_Data',
'PA_date Between '
& Format([Forms]![Date Prompt]![txtBDate],'\#yyyy-m-d\#')
& ' And ' & Format([Forms]![Date Prompt]![txtEDate],'\#yyyy-m-d\#')
& ' And P_Line=' & Asm_Prod_Data.P_Line,
'Comments',
', ');
It's unfortunate but the GROUP BY expression has to be identical to the one in the SELECT clause, no matter how complex.
I have a field called Product_Id(type string), which has length of 7 and starting with 0. But while inserting through VBA into a table field of type text the zeros is not getting inserted.
This is the insert query:
dbs.Execute "INSERT INTO tablename (PROD_NBR)VALUES (" & prodID & ");"
I think I have fixed the error - you need to declare the value in single quotes.
The PROD_NBR is a string type and field in the table is text type, then the inserting variable should be declared inside single quotes then double quotes and between two & symbols:
dbs.Execute "INSERT INTO tablename (PROD_NBR)VALUES ('" & prodID & "');"
Responding to #Cherry's answer, the following method is less tedious than a parameterized query, aka prepared statement. prodID can safely contain quotes and other special characters.
With dbs.OpenRecordset("tablename")
.AddNew
.Fields("PROD_NBR") = prodID
.Update
End With
Regarding the "starting with zero" part of your question, do you want PROD_NBR to always be 7 characters, padded with leading 0's? Then replace prodID with:
Right("0000000" & prodID, 7)
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
In ssis package I have String type variable V2 inside expression property i'm writing following sql query
"select * from mytable where date = " + #[System::StartTime]
But it is giving me an error :
The data types "DT_WSTR" and "DT_DATE" are incompatible for binary operator "+". The operand types could not be implicitly cast into compatible types for the operation. To perform this operation, one or both operands need to be explicitly cast with a cast operator.
Attempt to set the result type of binary operation ""select * from table where date = " + #[System::StartTime]" failed with error code 0xC0047080.
Even I have also tried with (DT_WSTR) #[System::StartTime]
still no luck any advice ?
You need to change data type of both StartTime variable and [date] field from the query to string.
Try this:
"select * from mytable where convert( varchar(10), [date], 120) = '" + SUBSTRING((DT_WSTR,50)#[System::StartTime],1, 10) + "'"
Which should return a proper query:
select * from mytable where convert( varchar(10), [date], 120) = '2013-05-22'
convert() will give you string like "2013-05-22". In my system (DT_WSTR) cast on #[System::StartTime] is returning string "2013-05-22 16:14:43", but if you have other settings, you'd have to construct the string from dateparts, if your default result would be for example "05/22/2013 16:14:43" due to other regional setting.
What is the verion of sql server you are using? and [date] field type exactly?