Formatting a date value in Reporting Services - reporting-services

I am a bit rusty and would be very grateful if you could help. I am using Reporting Services. I am using T-SQL. The date_ordered field mentioned below is data type varchar.
I have a simple report that gives the date_ordered value. Unfortunately, when I ORDER BY date_ordered DESC, it sorts the date by the numerical value of the day, so 31-Oct-14 appears first and 01-Apr-13 last.
I would like the report to show most recent dates first and end with the last date ordered.
I'm sure it's very obvious, but I can't see it.
With many thanks.

you just have to convert your date to a datetime format in tsql. then when you sort it it works as expected.
Select CONVERT (datetime, '24-04-2012', 105 as date) as date,
[date dispatched] from Orders order by 1
see How convert string to date T-SQL? and https://msdn.microsoft.com/en-us/library/ms187928.aspx

Related

How can i do a count of two columns in mysql?

i want to do a count of two columns in mysql. One of the columns is a string but another is a date like 06/08/2017 and when i do my query i get 0 results.
SELECT count(*) FROM `castigos` WHERE inicio_normal=05/06/2017 AND cod_emplazamiento=1
I have entries of that data but its dont show me anything. Maybe the type of data in the date is wrong?
What should i do?
Add the date field to your select and group by it. Otherwise mysql extensions doesn't recognize you want to group by the date and will aggregrate all the results into 1 column. And since you are getting 0 count, you're where clause must not be working.
Your date format seems malformed. usually YYYY/MM/DD format (standard format);
or specify a format using SELECT STR_TO_DATE('17/09/2010','%d/%m/%Y');
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.
the below uses the implicit casting and default date format to convert the string date to a valid date.
SELECT inicio_normal, count(*)
FROM `castigos`
WHERE inicio_normal='2017/05/06'
AND cod_emplazamiento=1
GROUP BY inicio_normal
Otherwise its doing math and comparing that date to the number stored for the date.
Understand dates should be stored in a date datatype and when you query dates you're passing in a string that is being cast to a date datatype for comparison. So you need to use the standard format, or cast your string to a date so the db engine knows how to convert your format to a date.
Try this :
SELECT count(*) FROM `castigos` WHERE inicio_normal="05/06/2017" AND cod_emplazamiento=1 GROUP BY inicio_normal
WHERE inicio_normal=05/06/2017
If you divide 3 by 6 then by 2017 you get a very small value indeed. OTOH if you reformat this as a date (e.g. 20170605, if you gave us a European formatted date - dd/mm/yyyy) then your query will find the rows you showed us.

SQL Change date format from yyyy-mm-dd to dd-mm-yyyy

I have created MySQL table :
CREATE TABLE EMP(
EMPID INTEGER NOT NULL (5),
SURNAME VARCHAR(25),
SAL INTEGER(5),
JON VARCHAR(25),
START_DATE DATE,
END_DATE DATE,
DEPNO INTEGER(5)
);
with following records:
INSERT INTO EMP
(EMPID,SURNAME,SALARY,JOB,START_DATE,END_DATE,DEPNO)
VALUES
('1','Vorosila','500000','COO','20150101',null,'1');
however I need to change date format from 2015 01 01 to 01 01 2015
Can anybody show me or tell me how to do that ?
THANK YOU IN ADVANCE
DATE values do not have a "format", they are objects that represent instants in time (or entire days, but still independent of formatting).
Formats are applied on input and output, so you just need to apply the correct format, which you can find in the MySQL manual, to the SELECT statement.
You cannot change the default date format in mysql.
I once hoped for the default date to be editable so I wouldn't have to jump through these hoops to get the date I actually wanted, mysql even has a date format system variable, but it is unused. Date Format Mysql - link
What you should really do is store it as the default format Year-Month-Date and then convert it on select.
The first thing I'd suggest is having your date columns as date types, which would give your dates the following format '2015-01-01'.
If you do this then you can use DATE_FORMAT - link - the second value in the DATE_FORMAT function allows you to customise the returned date, and there are many different thing you can do with this if you look at the link:
SELECT
DATE_FORMAT(`START_DATE`,'%d-%m-%Y')
AS `START_DATE`
FROM ...
The other option you have is to store your dates in the format that you already want as a char or varchar column.
HOWEVER, as should be obvious, this column will not be treated as storing dates, and so will not give you the correct comparisons in a where clause when using > < BETWEEN or the correct ordering in an order by clause. It is after all just a string of numbers in this case.
However you can then use STR_TO_DATE - link if you did need to use a where or order by on this column to change it back to a date within the query - in this case the second value is the custom format of your 'dates' in the column. Keep in mind with a where you will need to compare it with the correct mysql format as shown below:
SELECT
`START_DATE`
FROM table
WHERE STR_TO_DATE(`START_DATE`,'%d-%m-%Y') BETWEEN '2015-01-01' and '2016-01-01'
In MySQL you can change the format of a date using DATE_FORMAT method which is similar to to_char in Oracle.
DATE_FORMAT(SYSDATE(), '%DD-%MM-%YYYY');
For more information about specifiers check this thread http://www.sqlines.com/oracle-to-mysql/to_char_datetime
You can do what you probably want by creating a view and referring to that instead of the (underlying) table.
CREATE VIEW emp_view AS
SELECT empid,
surname,
sal,
jon,
date_format(start_date, '%d-%m-%Y') as start_date,
date_format(end_date, '%d-%m-%Y') as end_date,
depno
FROM emp;
Note that this changes the type of the date columns to varchar, so comparisons will no longer work as expected:
SELECT * FROM emp_view WHERE start_date > '01-12-1924'; // fails!

How to select latest date from database in MySQL

I have list of date in MySQL in the format of "MM-DD-YYYY" and When I was trying to fetch the latest date from table it just return the last date of a Year like 12-01-2014 instead of return latest date 03-16-2016.
Payment history table:
to_date
03-16-2016
12-01-2014
11-07-2014
10-03-2014
01-09-2014
I used following query:
SELECT MAX(to_date) FROM paymenthistory WHERE empid=59;
Result : 12-01-2014
Related post: Get the latest date from grouped MySQL data
Thanks in advance
You're working with strings, not native dates, so you're getting the maximum date.
Either convert those strings to ACTUAL mysql date/datetime values, or you'll have to go with ugly hacks, like
SELECT MAX(STR_TO_DATE(to_date, '%m-%d-%Y'))
and performance will be massively bad. MySQL's native date format is yyyy-mm-dd hh:mm:ss, which is a natural "most significant first" format. If your date strings were formatted like that, then even a max(string) would work.
It sounds like your date column is actually a VARCHAR format since it is seeing 12-01-2014 as the last date which is only true if stored as a VARCHAR.
Be sure your to_date column is a DATE type.
have you tried this?
SELECT TOP 1 * FROM paymenthistory WHERE empid = 29 ORDER BY to_date DESC;
For mysql try this
SELECT * FROM paymenthistory WHERE empid=59 ORDER BY to_date DESC LIMIT 1;

MySQL: Modifying output of a field for calculation

I know calculating age from DOB is relatively simple but I have an issue with different data entry formats in the database. Also, I know this can be easier using PHP, but I don't know PHP and only have MySQL to work with.
The DOB entered into the DB is entered as "month/day/year" or "00/00/0000". But when calculating against today's date, the date would be formatted as "year-month-day" or "0000-00-00". Furthermore, the month placed in the DOB field can have either a one number month (1/01/1999) or a two number month (01/01/1999), so it's not consistent.
I am trying to use the below to utilize CONCAT, SUBSTRING and LOCATE to output the DOB in a better suited format for the age calculation. I think I'm close but not quite there. Any help would be very much appreciated.
SELECT
CONCAT(SUBSTRING(APPU_DOB,-4,4),'-', SUBSTRING(APPU_DOB,LOCATE('/', APPU_DOB),1),'-',SUBSTRING(APPU_DOB,4,2))
FROM APPU_APP_USER
JOIN APPL_APP ON APPU_APPL_ID = APPL_ID
WHERE DATE_FORMAT(APPL_CREATE_DT, '%Y-%M-%D') >= '2014-01-01';
Instead of Concat use str_to_date function.
select str_to_date( appu_dob, '%m/%d/%Y' ) as 'dob';
on 1/01/1999 it returns a valid date formatted object with value 1999-01-01.
You can use it on other date strings that have single or two digit day or month numbers.
Note: To represent or refer a month, use small case m but not capital M, in the format pattern string.
And you should better redefine the data type of appu_dob field to date. So that you can easily apply date specific functions on it for any calculations.

how to get the data using from date and to date in sql server?

i need to retreive data from database with the condition from date to to date using between query,
my query is,
select * from Master where Date between '01-08-2013' and '30-08-2013'
but it retreive all data from the table...
i need only data with in that date..
i tried another one like,
select * from PatientMaster where EntryDate >= '01-08-2013' and EntryDate<= '30-08-2013'
how its posible..
whats wrong with my query...
sorry im very bad in english...
thank you in advance...
A date string has the syntax YYYY-MM-DD and not DD-MM-YYYY
select * from Master
where `Date` between '2013-08-01' and '2013-08-30'
for that you can use
select * from Master where Date >='01-08-2013' and dateadd(dd,1,'30-08-2013')
You have to convert your strings to dates. This page shows you how to do it in mysql, which is what you have tagged. For sql server, which is in your subject line, use this page.
Then you do a slight modification of your 2nd attempt. Instead of
and EntryDate <= the end date
you want
and EntryDate < the day after the end date
That takes care of any time components. It might not matter in your case, but it's a good habit to get into.
You'll be looking for an query that works with your format? (dd-mm-yy)
CAST to the desired format!
http://www.w3schools.com/sql/func_convert.asp
105 = dd-mm-yy
SELECT * FROM Master
WHERE CONVERT(date, Date, 105) BETWEEN '01-08-13' and '30-08-13'
Be conscious with regards of the choice of data type for date Columns,
with or without time, day or year first etc. and please do not use varchar
for dates...
know that it CAN be confusing to call a date column for only Date...
be consistent with high/lower case.