Convert integer of MYYYY into Date in ssrs - reporting-services

I have a field that is a bunch of integers formatted like so:
92014
102014
I would like to convert the field into a datetime of the first of each month. So the newly formated field would be:
9/01/2014 00:00:00
10/01/2014 00:00:00
(or however datetimes would actually get formatted). Can anyone help?

This is how you can accomplish this in an expression:
DateTime.ParseExact(IIF(LEN(Trim(Fields!StringDate.Value)) < 6, "0" & Fields!StringDate.Value,Fields!StringDate.Value),"Myyyy",System.Globalization.CultureInfo.InvariantCulture)

Related

sql incorrect date output incorrect

i have a table like this
|id| date |name|
1 23/11/20 jake
2 01/07/20 jhon
3 23/05/20 blake
4 11/02/20 drake
5 1/03/14 crake
i ran a query like this
WHERE date >= '1/07/20' AND date <= '23/11/20'
i expected a result where i would get only the results between those dates
but i got some results which were from 2014
the data type for the date column is varchar
#note i can not change the datatype
how can i only get dates between the two ?
String-wise comparison is the problem: typically, '10/01/19' (Janurary 10th, 2019) is greater than '01/01/20' (January 1st, 2020), because the former starts with 1, and the later with 0.
You need to turn these strings to dates before you can compare them:
where str_to_date(date, '%d/%m/%y') between '2020-07-01' and '2020-66-23'
This is inefficient, because the entire column needs to be converted before the filtering can happen. I would warmly recommend fixing your data model, and store dates as dates.
Side note: your strings need to be consistently formatted as mm/dd/yy for this to work; if you have varying formats - or strings that do not map to valid dates - then you have a bigger problem than what you have asked here.

SSRS how to sort date in MM/YY format?

I'm stuck at this point, I get my data from a stored procedure with the date format as MM/YY, but SSRS sorted my date in a wrong way: 01/2019, 02/2019,..., 12/2019;01/2018, 02/2018,...
So I'd like to do to have my data in the right order.
Since your date is converted to MM/YYYY format, the data is text and not numerical so it's sorted one character at a time rather than by the value.
If you want to sort by year and then by month, you would need a separate SORT option for each that parses the text into separate month and year values.
=RIGHT(Fields!DOB.Value, 4)
This gets the 4 characters from the right of the text which is the year in the data.
=LEFT(Fields!DOB.Value, 2)
LEFT , 2 gets the first two characters of the string - the month in the field.
Create a new column in the query where you convert the string date to a date data-type and sort on that e.g.
select
convert(date, '01/'+ MyDateStringColumn) MyDateColumn
from MyTable
order by MyDateColumn

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.

Convert different varchar dates to date

I'm having some troubles with converting varchar types to date types.
In SQL-server 2008 I'm having a table with different types of dates in type VARCHAR.
For example:
DDMMYYYY (05032013)<br/>
MMDDYYYY (03052013)<br/>
YYYYMMDD (20130305)<br/>
...
I have to convert these different string types to type "date" using a SQL-query.
Any suggestions how I can do this?
These are my records:
TYPE || FORMAT
____________________
DDMMYYYY||05032013
MMDDYYYY||03052013
YYYYMMDD||20130305
Try this
SELECT case
when type='DDMMYYYY' then convert(date,STUFF(STUFF('05032013',3,0,'-'),6,0,'-'),105)
when type='MMDDYYYY' then cast(substring('03052013',5,4)+'-'+substring('03052013',0,3)+'-'+substring('03052013',3,2) as date)
when type='YYYYMMDD' then cast(STUFF(STUFF('20130305',5,0,'-'),8,0,'-') as date)
end
If you can identify every row which date format is, you can convert them to date type. Otherwise i don't think that it will be completely correct. For example you can't implement logic for the following record - (03052013) can be 2013-05-03 or 2013-03-05.

Check date format in conditional split transformation in ssis

I am having flat file which contain bad date like [02/02/0200].I want export data into sql table.I am using Condition split or derived column transformation for date column.
I want that correct date goes to main table and errors rows go to error table.
Could you please let me know what expression i have to used for date column .
Thanks,
Jeetesh Garg
When you convert your date [02/02/0200] it's gonna be 200 Year 02 Month 02 Day.
So you need to eliminate nonsenses dates. This gonna eliminate dates which are lower than 2000 Years.
(DT_DATE)([YourDate]) > (DT_DATE)(2000 - 01 - 01)
OK so new derived column statement (bccPostDate - data type is "unicode string" ) :
((DT_I4)(SUBSTRING(bccPostDate,1,2))) > 12 || ((DT_I4)(SUBSTRING(bccPostDate,4,2))) > 31 || ((DT_I4)(SUBSTRING(bccPostDate,7,4))) < 1900 ? "" : bccPostDate
IS working I have tried with Years and this statement returning column with "Unicode string" data type.