Problem In get dates between 2 date (vb.net | OLE) - vb.net

I have an data base (.mdb) and it has a column with dates (dd/mm/yy) , some one give me a code to get all the dates in database between 2 dates , the code was :
Select * from table where date between 'StartDate' and 'EndDate'
but after I use the code , an error occurs told me that the types of data is not the same
System.Data.OleDb.OleDbException was
unhandled ErrorCode=-2147217913
Message="عدم تطابق نوع البيانات في
تعبير المعايير." Source="Microsoft
JET Database Engine"
although I convert the data type in the column of dates in database to (Date \ time) , and use OLE object to connect to data base
what is wrong , and what I have to do ?

You usually need to surround date/time types with # when working with Access, like so
#22/01/2009#

Instead of using dynamic SQL, instead use the Access Database Engine's CREATE PROCEDURE SQL DDL syntax to create a persisted object whose parameters have parameters strongly-typed as DATETIME with the NULL value as default. Handle the NULL value to use the DATETIME column's value instead e.g.
CREATE PROCEDURE GetStuff
(
arg_start_date DATETIME = NULL,
arg_end_date DATETIME = NULL
)
AS
SELECT lastvstart
FROM tb
WHERE lastvstart
BETWEEN IIF(arg_start_date IS NULL, lastvstart, arg_start_date)
AND IIF(arg_end_date IS NULL, lastvstart, arg_end_date);

Related

Datetime column cannot be pesisted

I have this SQL statement to create a table that stores the JSON string data and the event time found in that JSON string.
CREATE TABLE [dbo].[EventLog]
(
[EventID] INT NOT NULL IDENTITY(1,1),
[EventTime] AS CAST(JSON_VALUE(RawEvent, '$.EventTime') AS DATETIME ) PERSISTED,
[RawEvent] NVARCHAR(MAX) NOT NULL
)
However I get the following error below when I run this, I assume SQL Server does not know if the value fits DATETIME? is there a way to get this column defined?
Msg 4936, Level 16, State 1, Line 26
Computed column 'EventTime' in table 'Event' cannot be persisted because the column is non-deterministic.
You can use CONVERT with dates and have deterministic behavior as long as you specify certain date styles. As per the docs here, with the most common JavaScript date formats (since you are converting from JSON), you can use style 126 or 127, which are ISO8601 and ISO8601 with time zone. Your table, then, could be specified like this:
CREATE TABLE [dbo].[EventLog]
(
[EventID] INT NOT NULL IDENTITY(1,1),
[EventTime] AS CONVERT(DATETIME, JSON_VALUE(RawEvent, '$.EventTime'), 126) PERSISTED,
[RawEvent] NVARCHAR(MAX) NOT NULL
)
Alas, this is explained in the documentation:
CAST Deterministic unless used with datetime, smalldatetime, or sql_variant.
You may be able to parse the date and reconstruct the value using datefromparts() or datetimefromparts().

GetUtcDate in insert statement in SQL Server

I need to have the current date and time inserted into SQL Server.
I tried the following method but it didn't work.
insertstatement = "insert into tablename(sqldate) values (?)"
values = ('getutcdate()')
cursor.execute(insertstatement , values)
cnxn.commit()
I am getting the following error:
[Microsoft][SQL Server Native Client 11.0][SQL Server]Conversion failed when converting date and/or time from character string. (241) (SQLExecDirectW)')
I need to use the SQL Server time only because the code is getting executed at different servers and the timezones and need to use a standard time accurate to milliseconds.
As you are trying to insert SQL datetime, then the time should be got at SQL Server, so your code should looks like this
insertstatement = "insert into tablename(sqldate) values (getutcdate())"
cur.execute(insertstatement)
cnxn.commit()
Try to use GETUTCDATE() function. Let me show an example:
insert into tablename(sqldate) values (GETUTCDATE())
In addition, your type of table column should be DateTime. For example:
DECLARE #FooTable TABLE
(
StartDate DateTime
)
INSERT INTO #FooTable
(
StartDate,
)
VALUES
(
GETUTCDATE(),
)
SELECT StartDate FROM #FooTable
MSDN has a great description about GETUTCDATE() and possible use cases.

select data using stored procedure

I use the stored procedure to get multiple records which that condition satisfies
CREATE PROCEDURE [dbo].[Sp_GetAttendanceBwDates]
#datefrom datetime,
#dateto datetime,
#empid int
ASBEGIN`
select AM.employee_Id,CONVERT(varchar(10),AM.date,111) from
tblAttendanceMaster AM where AM.employee_Id=#empid and
CONVERT(varchar(10),AM.date,111)<=CONVERT(varchar(10),#datefrom,111)
and CONVERT(varchar(10),AM.date,111)=CONVERT(varchar(10),#dateto,111)
END
in code behind,when execute the below code I getting the error.I didn't understand it
var objattendance = context.Sp_GetAttendanceBwDates(datefrom,dateto,emp);
error message
occurred in System.Data.Entity.dll but was not handled in user code.
Additional information: The data reader is incompatible with the specified 'FlairModel.Sp_GetAttendanceBwDates_Result'. A member of the type, 'record_Id', does not have a corresponding column in the data reader with the same name.
I guess theer is problem in the column i.e. property get created when you imported SP in you entiry frameowrk ,
So there might be problem with your select statement here which is not returning proper name matching with that generated complex type or generated type
for example this query with matching columns like
select AM.employee_Id as record_Id''need to be matching property name,
CONVERT(varchar(10),AM.date,111) as date''need to be matching property name
from
tblAttendanceMaster AM where AM.employee_Id=#empid and
CONVERT(varchar(10),AM.date,111)<=CONVERT(varchar(10),#datefrom,111)
and CONVERT(varchar(10),AM.date,111)=CONVERT(varchar(10),#dateto,111)

Converting Varchar into Date in data type

I am relatively new to SQL Server so I was wondering how to convert the data type from varchar to date format? I have a few thousands records so I need a query to help to convert the varchar to date in a single query.
I have the date in this format: yyyymmdd, in varchar(8) and I want to convert this into yyyymmdd, in date format.
Is there any queries to help me with this?
For various conversions between VARCHAR and DATETIME have a look at this link.
Actually in your case, since your VARCHAR is in yyyymmdd format, you could just:
convert(datetime, YourVarcharDateField, 112)
Simply Use this Inbuilt CONVERT Function, and Check this Link for formatting Dates
-- Use 101 if you have divider in your date
SELECT convert(datetime, '2014-01-02',101) as [DateTime]
-- Use 112 if you don't have divider in your date
SELECT convert(datetime, '20140131',112) as [DateTime]
Edited:
UPDATE yourTable SET field = convert(datetime, 'yourFieldName',112)
--This will update all of your field regardless of any particular row
--If you want to update any particular set of rows use `WHERE` clause
if you have more various formats goto to the given link.
Data types can be converted either implicitly or explicitly.
Implicit conversions are not visible to the user. SQL Server automatically converts the data from one data type to another. For example, when a smallint is compared to an int, the smallint is implicitly converted to int before the comparison proceeds.
Explicit conversions use the CAST or CONVERT functions.
The CAST and CONVERT functions convert a value (a local variable, a column, or another expression) from one data type to another
convert(datetime, '2013-05-04',101)
CAST ( expression AS data_type )
ALTER TABLE [dbo].[table] ADD ConvertedDate Date
UPDATE [dbo].[SysData] SET ConvertedDate = CAST(VarCharDate as Date)
ALTER TABLE [dbo].[table] DROP COLUMN VarCharDate
Use CAST OR CONVERT function to convert string to date
Try this:
SELECT CAST('20140102' AS DATE) AS convertedDate;
SELECT CAST(colName AS DATE) AS convertedDate FROM tableA; -- Replace column name and table name
OR
SELECT CONVERT(DATE, '20140102', 112) AS convertedDate;
SELECT CONVERT(DATE, colName, 112) AS convertedDate FROM tableA; -- Replace column name and table name
OUTPUT of both queries:
|convertedDate|
|-------------|
|2014-01-02 |
In SQL SERVER, there are two types of built in conversion techniques.
Convert
Cast
Convert having its own defaults so it will be outdated in upgraded version of SQL SERVER
better make use of CAST Conversion technique
In your scenario.Already having the date with datatype of Varchar(8) trying to Convert into Date
Solve in systematic manner.
Adding the one new Column in the existing table.
Alter Table Table_name Add changedDataTypeDate Date
Update the values in varchar datatype to Date Datatype
UpDate Table_name Set ChangedDataTypeDate = CAST(OriginalDataTypeDate as Date)
Again change the new column name into old column name.
Sp_Rename 'Tablename.changedDataTypeDate','OriginalDataTypeDate','COLUMN'
Its done.
Based on u r requirement.
Alter Table customer Add Purchase_Changedtype Date
Update Customer set Purchase_changedtype = CAST(Purchase_date as Date)
(If u need Time also replace Datetime istead of Date)
Alter table Customer Drop column Purchase_date
Sp_Rename 'Customer.Purchase_ChangedType','Purchase_Date','Column'

Discover SQL Server procedure default parameters using SYS or INFORMATION_SCHEMA tables

Like Oracle, SQL Server supports parameter defaults in stored procedures. Oracle syntax:
CREATE OR REPLACE PROCEDURE p_default (
p_in_number IN number := 0,
p_out_number OUT number,
p_in_varchar IN varchar2 := '0',
p_out_varchar OUT varchar2,
p_in_date IN date := date '1981-07-10',
p_out_date OUT date
)
SQL Server syntax:
CREATE PROCEDURE p_default (
#p_in_number INTEGER = 0,
#p_out_number INTEGER OUT,
#p_in_varchar VARCHAR(10) = '0',
#p_out_varchar VARCHAR(10) OUT,
#p_in_date DATE = '1981-07-10',
#p_out_date DATE OUT
)
With Oracle, I can discover defaults using this query:
SELECT argument_name, defaulted FROM all_arguments WHERE object_id = :proc_id
How can I discover this in SQL Server selecting from sys or INFORMATION_SCHEMA tables? I don't see any useful column in INFORMATION_SCHEMA.PARAMETERS, and the sys.parameters.has_default_value seems not to be set correctly (!)
Note, I have asked as similar question about DB2:
Discover DB2 procedure default parameters using SYSCAT tables
There's no simple way to do it. As the documentation for has_default_value states:
SQL Server only maintains default values for CLR objects in this catalog view; therefore, this column has a value of 0 for Transact-SQL objects. To view the default value of a parameter in a Transact-SQL object, query the definition column of the sys.sql_modules catalog view, or use the OBJECT_DEFINITION system function.
So you'd have to pull the whole stored proc definition out and parse it yourself to determine the value.
Side note: I'd be wary of #p_in_date DATE = '1981-07-10'. I know that the date datatype is a bit more sensible than datetime, but I'm not sure if the above is still ambiguous - certainly if it was converted to datetime, it may result in 10th July or 7th October, depending on language settings. I'd be more comfortable with '19810710' which will always be interpreted as 10th July.