Max aggregate function with autonumber and date field in access - database

I have ran into a problem recently. One of the tables I have in MS Access database contains multiple fields but the main focus is on an autonumber field and date field. When I try to apply Select query with Max(autonumber) function and where clause containing date field, the wrong value or rather empty column is shown. as soon as i remove the date field from the where clause, the value is returned fine. My query is attached. Any help would be appreciated.
SELECT MAX(serial) AS Expr1
FROM coaDetails
WHERE (((coaDetails.[title])='CLAIMS')AND ((coaDetails.[dates])=#04/08/2018#));
The above query, serial is autonumber while dates is date/time with format as dd/mm/yyyy
There is no error in the query but it just gives wrong value. as soon as date condition is removed it gives results.

Use the mm/dd/yyyy or yyyy/mm/dd format for the string expression for the date value:
WHERE (((coaDetails.[title])='CLAIMS')AND ((coaDetails.[dates])=#2018/08/04#));

In the SQL code, the dates have to be formatted the american way: #mm/dd/yyyy#. In the query design grid, which is a user-friendly, graphical view of the SQL code in the back, the format depends on your regional settings.
Therefore, just use the month first:
SELECT MAX(serial) AS Expr1
FROM coaDetails
WHERE (((coaDetails.[title])='CLAIMS')AND ((coaDetails.[dates])=#08/04/2018#));

Related

How do you change date format from YYYY-MM-DD to MM/DD/YYYY in SSIS?

I am loading a flat file to a database table, and need to change the format of the date from YYYY-MM-DD in the flat file, to MM/DD/YYYY in the database table. I tried using the following statement in Derived Columns as shown below, but not sure how to configure the statement, so I got an error message stating that SSIS could not parse the expression.
Derived Column Name: EFF_DATE
Derived Column: Replace EFF_DATE
Expression: TOKEN( MONTH([EFF_DATE]),"//|",DAY([EFF_DATE]),"//|",YEAR([Copy of EFF_DATE]) )
DATA TYPE: databasetimestamp[DT_DBTIMESTAMP]
Can anyone help me determine how to change the format of the column in Derived Column? Otherwise, please let me know if there is another way to do it. Thank you.
This question was different from the last one. In the last question, the date column was data type DateTime. But in this question, the date is a string, and when I used the Derived Column to change the date from YYYY-MM-DD to MM/DD/YYYY, it kept the leading zeroes in MM and DD. The issue then became, not just changing the date format, but also removing the leading zeroes from the Month and Day.
However, I researched and came up with a better solution in SSIS for changing the date value with data type string, as the database I am working with stores the date in that format.
I removed the Derived Column from my Data Source Task, and added an Execute SQL Task in the Control Flow, then added the following Update statement which not only changes the format from YYYY-MM-DD to MM/DD/YYYY, but also removes the leading zeroes from Month and Day. The CONCAT function I used the sample SQL below changes the format from YYYY-MM-DD to MM/DD/YYYY, while the Convert function changes the MM and DD values to data type INT which removed any leading Zeros. This solution allowed the date to remain a string, as that was the table format I had to work with.
UPDATE [StagingTable]
SET START_DATE =
CONCAT( CONVERT(INT, SUBSTRING(START_DATE, 6,2)), '/', CONVERT(INT, RIGHT(START_DATE, 2)),'/', LEFT(START_DATE,4) )]
Thanks to everyone for their comments, as it helped me to think outside the box and determine this solution.

SSRS report - export to excel date time filteration

Populating an SSRS report from dyanmic dataset i.e. number column and order of columns may vary based on condition, and this report on trying to export to excel. But in exporting date column filtration not working. Finds the reason is I bind the column in string format.
Sample data format to understand. Here the columns & order of columns may vary as per condition.
And I am converting this to below format and passing to SSRS as dataset.
And populating report by grouping with ROW_NO & COL
Then I tried to convert DOB column to date time.
For converting DOB to date format changed return type of DOB from sql query as datetime, converted column value to date time using CDate() in SSRS and formatted in own format (dd-mmm-yyyy).
Below expression used for converting to date time:
IIF((Fields!COL.Value="DOB") ,CDate(Fields!VALUE.Value),(Fields!VALUE.Value))
But showing #Error for columns data type other than date time:
Go to your detail textbox in the DOB column. And use the following expression:
=Format(CDate(Fields!DOBValue.Value), "dd-MM-yyyy")
This way the date in your DOB column will show 23-01-2018
Note: The expression just works if in the Fields!DOBValue.Value field are just dates. If there is a N/A it will throw an error. Then you have to clear the N/A first.
And if you want to export it to Excel you need to set the language in your report properties (use your region) and go to your textbox where you wrote in the above expression under Textbox properties > Number chose Date format.
UPDATE
Based on your updated question I think I found the error. The problem is your writing of the month names combined with the report language. My report language is set to DE. For me the following works:
=CDate("15-Mai-94") 'Result 05.05.1994
But this dosent work:
=CDate("15-May-94") 'Result =Error
The second one works, when I set the report language to EN but then the first one throws an error

Excel pivot not recognising converted date from SQL as a date field

I've had a similar problem with dates before.
I'm querying a proprietary CMS database that stores dates as a string 'YYYYMMDD'.
I'm using...
convert(varchar(10),right(VarCompletionDate,2)+'/'+substring(VarCompletionDate,5,2)+'/'+left(varCompletionDate,4), 103)
to convert to 'DD/MM/YYYY' format. On the SQL side this appears to work but in Excel, the date is treated like a string rather than a date and I'm not getting the date filters that you would get with a proper date field...
what am I doing wrong here? is the convertconverting to a varchar rather than a date? ...if I do Convert(date,... I get conversion errors.
In excel, you can select a data cell and confirm that the data does not have an apostrophe sign as suffix.
This forces excel to treat the data as text. If you do find it, please do a find and replace all in your selected data range.
If there is no apostrophe, select your data and go to Home>Number>format dropdown and then select long date or short date as the format type.
This will then bring up the date filters.
Managed to find the answer!
Convert(date,....)
Was the correct treatment to output a date format from SQL.
The Pivot table wouldn't recognise the field as a date after a refresh unless I recreated the pivot table - there must be some kind of data type persistence going on in Excel.

Date Picker format vs Database timestamp

Is it possible to compare two dates if they are formatted in different ways? One date is coming from a ColdFusion UI input which is in the following format:
mm/dd/yyyy
My timestamp in my MSSQL database is in the following format:
yyyy/mm/dd
Background: I need to write a query that compares the dates and returns rows with a timestamp after the user selected date. Would I need to reformat either date?
Example:
<cfset start_date = 'form.sDate'>
<cfquery name="sortDate" datasource="RC">
SELECT *
FROM my_db_table
WHERE Submission_Date > #start_date#
</cfquery>
First off, always use cfqueryparam in your queries when dynamically including content. Second, you should be able to use start_date "as is" to compare the date to the date in the database
So, something like:
<cfquery name="sortDate" datasource="RC">
SELECT *
FROM my_db_table
WHERE Submission_Date > <cfqueryparam value="#start_date#" cfsqltype="cf_sql_date">
</cfquery>
Last, you can always test the raw sql in MSSQL Management Studio to test the date comparison.
(Too long for comments...)
There are a few subtle distinctions here it is important to understand.
You are working with a string and a date object, not two dates. Also, SQL Server does not actually store dates in yyyy/mm/dd format. Dates are stored as big numbers internally. Since those are difficult for humans to process, whatever IDE you are using displays them in a more human friendly way, such as yyyy/mm/dd format. But again, that does not mean the database actually stores them as strings.
Since the datepicker value is a string, you should validate it first, then convert the string into a date object. Comparing apples and oranges (ie dates and strings) is common gotcha. That is almost always where date queries go awry. That is why converting the string into a date object is important. It ensures the database compares the same things ie two dates and that the results will always be what you expect.
Finally, use cfqueryparam to pass the value to your database correctly. Be sure to use the cfsqltype which corresponds to the db column: cf_sql_date (date only) or cf_sql_timestamp (date and time).
Technically, if the strings are always in mm/dd/yyyy format you can pass in the string "as is" and CF will automatically convert it for you. However, trusting the input will always be in the right format is not a great idea. Validating input and using cfqueryparam will ensure the most consistent results.

date comparison in oracle for the year ends with 00

The date stored in my database is 01-01-1900 for field emp_doj(Data Type DATE).
But while retrieving the data, the value is 01-jan-00, even though formatted with dd-mm-yyyy.
I am comparing retrieved date field with some other date in SQL query.
select *
form persons
where per_join = to_date(to_char(i.emp_doj,'DD-MM-YYYY'),'DD-MM-YYYY')
Here the value of to_date(to_char(i.emp_doj,'DD-MM-YYYY'),'DD-MM-YYYY') results in 01-01-00, instead of 01-01-1900
I suspect it's your default NLS settings for the IDE you are using that is merely displaying the dates with a two digit year. Oracle does not store dates in any specific format. It stores them in a binary format internally.
Therefore I suggest you check the settings for your IDE and/or database and you will see the NLS date format set to DD-MM-YY, alter this and set it to DD-MM-YYYY and you will see the dates as you wish to see them.
As for your query, why use TO_CHAR and then TO_DATE? If emp_doj is a date field and per_join is also a date then just compare them directly for equality. If you are trying to cut the time portion off your emp_doj values then just use TRUNC().
For examples and reference on NLS: http://www.orafaq.com/wiki/NLS
More here: http://www.oracle.com/technetwork/database/globalization/nls-lang-099431.html
select to_char(emp_doj,'dd-mm-yyyy') from yourtable
I have got some temporary solutions it currently works for me, I simply changed my select query to
select *
form persons
where to_date(per_join,'DD-MM-YYYY')= to_date(i.emp_doj,'DD-MM-YYYY')

Resources