My database is SQL Server, and the thing is that "dates" are stored as a string type. If I execute that query it does not order properly.
What I want to do is, get each day's value for between 7am and 8 am. For example, I want to get 14th of June records for between 7am an
Use CAST() or CONVERT() in your WHERE / ORDER BY clause conditions.
However, you should store dates as date types instead of strings to avoid such problems.
Related
I'm using Microsoft Access as a front end to a SQL Server data warehouse. I need to speed up date comparisons in the front end. The dates are stored as integers in the back end (e.g. today is 20191123). In the Access front end I convert the integer dates to date data type:
CDate(Right([eventDate],2) & "-" & Mid([eventDate],5,2) & "-" & Left([eventDate],4))
That lets me do date comparisons. For example, to find occurrences of eventDate between today and 1 month from today I put this in the WHERE clause:
Between Date() And DateAdd("m",1,Date())
I need a faster way of doing this. My Access queries are reasonably fast without the date comparison in a WHERE clause, but as soon as I add the date comparison a query that previously ran in a fraction of a second takes 10-15 seconds.
You could convert your Access dates to the integer format of the table field and then use:
Between Val(Format(Date(), "yyyymmdd")) And Val(Format(DateAdd("m",1,Date()), "yyyymmdd"))
Utilize SQL Server SQL engine which is waaay faster than Access!
Create pass-through query in Access, then pass the parameters in "ODBC Connect Str" and copy/paste your Access SQL to this query.
If the connection string is right and if the SQL syntax is correct, it will be executed on the SQL Server side and it should be a lot faster. I mean a LOT.
Your Connection string should look like this:
ODBC;DRIVER={SQL Server Native Client 11.0};SERVER=NameOfTheServer;DATABASE=NameOfTheDatabase;UID=Username;PWD=Password
Change the name of the driver if you are using a different one.
Note: you might want to change functions for date as those are not the same as in Access i.e. Date() in Access will be GETDATE() in SQL Server. CDate should be replaced with CAST or CONVERT, etc.
As part of my job duties, I'm responsible for extracting data from our vendor's Oracle 11g database, and loading it into our SQL Server 2016 database. I've been doing this successfully with SSIS and the Attunity Oracle connectors.
Today I was informed that there was a new column added to the existing Invoices table on the Oracle side. There was already a DATE column called Order Date, which contains valid date values with zero'd times, like 2017-12-25 00:00:00.
The new column is called Order Date Time and is also a DATE column. When I opened up the SSIS package and pulled up the Oracle source in my DFT, I previewed the data and found the values in Order Date Time to be 2432-82-75 50:08:01. I tried converting the column with CAST and all the TO_* functions, but the conversions either failed outright, or returned a string of zeros.
TO_CHAR("Order Date Time", 'YYYYMMDDHH24MISS')
yields 00000000000000
After a bit of Googling for "Oracle date value invalid", I'm now thinking that these DATE values are actually corrupted. Am I missing anything here? Is there some sort of special Oracle-specific technique for storing time values in a DATE column that I may not be aware of?
(And yes, it does bother me quite a bit that our vendor added another DATE column instead of just using the time portion of the existing Order Date column.)
Unfortunately, Oracle database engine allows inserting invalid date values, which leads to many problems especially when importing data to others database engines such as SQL Server.
To handle this issue, you have to implement the logic that fits your needs, as example:
You can exclude these records from you queries by filtering on acceptable date ranges: (WHERE date between ...)
You can Update records with invalid values by replacing with NULL
You can use a CASE statement in your query to replace values with NULL
I faced this issue one time while importing data to SQL Server from an Oracle data source, there was unacceptable date values, i decided to update all records where date are invalid and replace with NULL values before starting the import process.
There are many links related to this issue:
Detecting invalid values in the DB
How to identify invalid (corrupted) values stored in Oracle DATE columns
Corrupt date fields causing query failure in Oracle
Invalid Date in DATE Column SQLPlus VS SQLDeveloper
Ask Tom - date validation in oracle
Dealing with invalid dates
Error: Invalid date format
DB Connect; Oracle DB date field data is corrupt
I'm running the following query on a table from SQLPro for MSSQL:
SELECT * FROM MyTable where Date = '2016-06-28'
The Date column contains fields formatted as datetimes for example: '2016-06-27 19:00:00:000'. When I run the query it returns results whose entries in the Date column are one day earlier than the date I queried for. So in the above example all results returned have date '2016-06-27'. There is data for the date I'm looking for since if I query for '2016-06-29' I get the data for the 28th.
Further, when I query from a cursor using pymssql I get the data for the right dates so it seems like the issue is with SQLPro and not the database itself. Anyone know what's going on/how to fix it?
Developer here. It is indeed a bug with SQLPro. I've sent a fix to #stableMatch which should resolve the issue.
I am exporting data from Excel to SQL Server through an API. Everything is supposed to save as nvarchar but date is stored as int value (automatically). How can I keep, in SQL table, date value as it is i.e. like 01/01/1990 instead of 32874.
Might be a basic question, Googled and looked into SO but couldn't find what I am after yet. Help appreciated!
In SQL Server, you can convert to a date by using:
select dateadd(day, 32874, '1899-12-31')
There is probably also a way to fix this when importing the data, by treating it as an actual date or string.
How do I convert a datetime to just a date in SSIS?
I'm trying to perform an aggregate query which will group by a particular date. In order to do that, I'm trying to extract just the date portion of the timestamp(stored as a datetime) so I can perform the GROUP BY on the next step.
I've looked at the DATEPART function, but it looks like I would have to call it multiple times to retrieve the month, date and year, concatenate them together and then parse the new date. There has got to be a better way to do this.
In SSIS you can use Data Conversion Transformation, for input value you have DT_DBTIMESTAMP and for output use DT_DBDATE
My first question would be if you are dealing with data contained in dbs on the same server-why not just perform all of your casting and grouping in SQL Server before you bring the data over to SSIS? For performance reasons, it is best to do any massaging of the data SQL Server before bringing it into SSIS. However, if you are dealing with heterogeneous data (oracle, excel, text files, data from a different sql server instance), then you could use the derived column transformation and convert the date using the cast (DT_DBDATE) then use the aggregate component to do your group by.
Unfortunately, that is the best way to do it (in my opinion) and depending on your destination column (if that is where the flow is ending) it will still have the 00:00:00:000 on it. But you probably already figured that =P.
Here's you you can do it in T-SQL, I'm assuming SSIS allows full access to SQL Server functions:
SELECT CAST( FLOOR( CAST(getdate() AS float) ) AS datetime)
Basically, it converts the date to a float (time is a fraction of a day), uses floor to chop off the decimal (cast to int might round up), then casts the float back to a date.
Efficient, works, and doesn't require multiple calls to DATEPART()
The DATEADD() function adds a time/date interval to date and then returns the date.
Syntax is
DATEADD(interval, number, date)
Query is
select DATEADD(dd,0, DATEDIFF(dd, 0, GETDATE()))