I am trying to update some data within a table in ms-access using vb.net. However I am getting an error when trying to run the code. My Query is:
Query = "UPDATE tabDatafiveMinutely SET PAR0050=308.3925 WHERE TimeValue = #16 May 2012 14:30:00#"
The error I get is:
No value given for one or more required parameters.
Does anyone have anyone have any ideas on how to solve this?
EDIT:
The Syntax is Correct I had a problem with the Database
Promoted to answer as requested :)
This error means that some field names, specified in query, are not present in table (or are reserved words).
just dont make use of # instead of that us ' might resole your issue
UPDATE tabDatafiveMinutely SET PAR0050=308.3925
WHERE TimeValue = '16 May 2012 14:30:00'
use this
Query = "UPDATE tabDatafiveMinutely SET PAR0050=308.3925
WHERE TimeValue = CONVERT(DATETIME, '2012-05-16', 102)
for other date format
Date -100- MMM DD YYYY' -- Feb 5 2010
Date -101- MM/DDYYYY'
Date -102- YYYY.MM.DD'
Date -103- DD/MM/YYYY'
Date -104- DD.MM.YYYY'
Date -105- DD-MM-YYYY'
Date -106- DD MMM YYYY' --ex: 03 Jan 2007
Date -107- MMM DD,YYYY' --ex: Jan 03, 2007
Date -108- MMM DD YYYY' -- Feb 5 2010
Date -110- MM-DD-YYYY' --02-05-2010
Date -111- YYYY/MM/DD'
Date -112- YYYYMMDD' -- 20100205
Date -113- DD MMM YYYY' -- 05 Feb 2010
to convert date to text use like this
CONVERT(VARCHAR(12),getdate(),113)
The date must be between quotes, I think:
Query = "UPDATE tabDatafiveMinutely SET PAR0050=308.3925 WHERE TimeValue = '#16 May 2012 14:30:00#'"
Related
I have table with dob field as varchar datatype, i performed dateadd function on few record. Now i have few records with
May 25 1921 12:00AM format and some with yyyy-mm-dd format.
How do i set all records with same dateformat in the tableof varchar datatype?
Thanks
Simply do an update:
update your_table set dob=convert(varchar(25),cast(dob as datetime),120)
This will be the Temporary solution, I strongly recommend you to use datetime type for the datetime values. Otherwise, you will face many issues in the future.
alter table MyTable
add NewDate Date;
update MyTable
set NewDate = cast(Olddate as date);
Use Below code for convert varchar to date :
SELECT CONVERT(DATE,'May 25 1921 12:00AM',103)
Try This.
select convert(varchar(10), cast('May 25 1921 12:00AM' as date),120)
to Update :
update TableName set dob=convert(varchar(10),cast(dob as datetime),120)
from TableName
I have a text field in Access called "TempD"
which contains data like below
Mon, Oct 6, 2014
Mon, Nov 10, 2014
I need to convert this to proper date format using SQL and put it in Field "Emaildate"
I am using the sql query below:
UPDATE MissingT
SET MissingT.emaildate = format(cdate(Right([tempd],4) & "/" & Mid([tempd],6,3) & "/" & Mid([tempd],9,2)));
But the issue is that the TempD field varies in length if the day is single/double. When the day is in 2 digits
(Mon, Nov 10, 2014)
it is converting this text to 2014-11-01 instead of 2014-11-10
Any idea how can this be resolved?
Discard the abbreviated day name, comma, and space from the beginning of your date string ...
? Mid("Mon, Oct 6, 2014", 5)
Oct 6, 2014
The resulting substring can be converted directly to a Date/Time value using CDate() ...
? CDate(Mid("Mon, Oct 6, 2014", 5))
10/6/2014
Then if you want that Date/Time value as a string in yyyy/mm/dd format ...
? Format(CDate(Mid("Mon, Oct 6, 2014", 5)), "yyyy/mm/dd")
2014/10/06
If that is what you're after, you can do it in an UPDATE query like this ...
UPDATE MissingT
SET emaildate = Format(CDate(Mid(tempd, 5)), "yyyy/mm/dd");
It makes no sense to apply Format on a date value passed to a Date field.
This is how to solve it:
UPDATE
MissingT
SET
emaildate = DateValue(Mid([tempd], 5))
I am pulling data from SQL Server, and the date on our back end is formatted prtty ugly, like so:
2014-01-10 00:00:00.000
I was wondering if there is some sort of funciton that I can run in my query to display the date column differently in my output? I don't want to actually alter the way in which the Column is formatted on the back end, I just want to change how it is presented to the user in the results of a sigle query.
Thank you for your help!
Edit: I am using SQL server 2008 R2 as my RDBMS
SQL 2012: Has FORMAT():
SELECT FORMAT(Date_Field,'ddd, MM yyyy')
Here's a list of FORMAT() options
Prior to SQL 2012:
SELECT CONVERT(VARCHAR(12),Date_Field,109)
List of CONVERT() styles
If you just want to remove the time portion you can:
SELECT CAST(Date_Field AS DATE)
You can format datetime output using the CONVERT function. Review the MSDN documention and the W3Schools page for the Convert function for more details and examples.
Here is quick example of a few different formats available.
SELECT CONVERT(VARCHAR(19), GETDATE())
SELECT CONVERT(VARCHAR(10), GETDATE(),10)
SELECT CONVERT(VARCHAR(10), GETDATE(),110)
SELECT CONVERT(VARCHAR(11), GETDATE(),106)
SELECT CONVERT(VARCHAR(24), GETDATE(),113)
Results:
Feb 27 2014 11:54AM
02-27-14
02-27-2014
27 Feb 2014
27 Feb 2014 11:54:33:977
I have a varchar variable containing value 01 May 2013 and I need to obtain the integer of the Month part.
for eg if 01 May 2013 is input I should get the result as 5
The query I wrote was :
select DATEPART(MM,CONVERT(DATETIME,CONVERT(VARCHAR(15),'01 '+SUBSTRING(FISCAL_MONTH,1,3)+' 2013'),100))
FROM <table name>
Here FISCAL_MONTH is my column name from the table. However this query is showing me the result, for eg 11 for November but is also throwing the following error :
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
I have tried various combinations, bit in vain. Kindly note I need it in a query. Kindly help.
If that is the only date you want to convert then you can simplify the SQL statement to:
select datepart(mm,convert(datetime,'01 May 2013'))
I just used this to test and it worked:
declare #t as varchar(333)
set #t = '01 May 2013'
select datepart(mm,convert(datetime, #t))
Gives me
5
If you are still getting that conversion error, then you either have some NULL values OR some date values that are not in the correct format. Check the data in your table to see which it is.
I'll leave it to you to add the rest of the months.
select case substring('01 MAY 2013',4,3) when 'JAN' then 1
when 'FEB' then 2
when 'MAR' then 3
when 'APR' then 4
when 'MAY' then 5
when 'JUN' then 6 else 9999 end
It might just be that there is a 3 instead of a 2 in the substring argument:
SUBSTRING(FISCAL_MONTH,1,2)+' 2013'),100))
The concat might work also:
select DATEPART(MM, CONCAT('01/', substring(convert(varchar(15),'03/23/2013'),1,2),'/2013'));
Wow, it is real interesting to see all the weird code that people come up with. I use the KISS policy most of the time (keep it simple stupid).
How about the MONTH() function. It has been in the TSQL language forever. I even threw in a coalesce to handle any unexpected NULLS.
-- Simple date as text
declare #var_test varchar(20) = '01 may 2013';
-- Use coalesce to handle nulls, use month to return int
select month(coalesce(#var_test, '01 jan 1900')) as my_month_index
Select month ( cast ( date_col as datetime ))
From table
You have to convert with proper format, you have to pass the apprpriate format-style value on your conversion. As per MSDN, you are using "dd mon yy" format, so you have to supply the corresponding format-style-code 6 or 106
Declare #FISCAL_MONTH nvarchar(max) = '01 May 2013'
select datepart(mm,Convert(datetime, #FISCAL_MONTH, 6))
--5
Set #FISCAL_MONTH = '01 November 2013'
select datepart(mm,Convert(datetime, #FISCAL_MONTH, 6))
--11
I am Surprised, While i copy and paste 'Mon, 26 Aug 2013 5:32:44 GMT' in database table field of type DateTime in Editable mode it update that cell but while i am Executing query Like
declare #Manoj date
Set #Manoj = 'Mon, 26 Aug 2013 5:32:44 GMT'
update NK_News set PubDate =#Manoj where ID= 70
here PubDate is DateTime field
It generate error as Conversion failed when converting date and/or time from character string
Is there any way to find what exactly Query run at Editable Mode in SQL Server Management Studio.
Set #Manoj = stuff(replace('Mon, 26 Aug 2013 5:32:44 GMT', ' GMT', ''), 1,5, '')