Replace existing DATE format with another format in SSRS expression - sql

I have a table which has a TEST column which contains many values:
TEST
2014-04-02
2014-04-03
2014-04-04
WEEKLY TOTAL
PRIOR WEEK
12 WEEK
How can I write an expression using IIF statement so if the row contains a date then change the date to a different format?
For example:
The first row has 2014-04-02 (yyyy-mm-dd) which should be changed to 02-Apr (dd-mmm)
The last row has 12 week which should not be touched because it's not a date

Solution 1:
=iif(isDate(Fields!Test.Value)
,FORMAT(
CDATE(iif(isDate(Fields!Test.Value), Fields!Test.Value, Nothing))
, "dd-MMM"
)
,Fields!Test.Value)
Solution 2:
Handle it in SQL itself using CASE WHEN

IF all of your dates have a '-' AND none of your text has a '-':
=IIF(Fields!TEST.Value like "*-*", FORMAT(CDATE(Fields!TEST.Value), "dd-MMM") ,Fields!TEST.value)
Not tested, but should work. Edited to return an actual date.
Edit: after some more experimentation, it dawned on me that what you want would result in the SSRS column having an inconsistent type (date/string), and I don't think it is possible to have both string types and date types in the same column.
My mistake was not to realize this sooner.
In order to return actual dates, you will need to clean/filter your data.

Related

select data from range of two dates

I want a query for selecting data between two dates (p_startdt,p_enddt) which is given by the user, if there is no input then by default data of last one year will be given as output. I am not able to put case for null or no input
where invc_dt between p_startdt and p_enddt
Use NVL to handle the case of a NULL value. The following example will take start date as a year ago if p_startdt is null and p_enddt as the current date if p_enddt is null:
WHERE invc_dt
BETWEEN NVL(p_startdt,ADD_MONTHS(SYSDATE,-12)) AND
NVL(p_enddt,SYSDATE)
Note: I'm assuming the data type of the column invc_dt is DATE.

Adding DATEADD to a SUBSTRING OF TEXT to get a date in the future

I am attempting to pull out a date from a substring of a text field that includes 'x' number of metadata such as company name, currency etc that is all input to one field with a nodeID as an identifer e.g.
Column name - Attributes
Values look like this:
Company Name27/04/2021... and so on.
I need to pull the Date within the A61 field only then add on a dateadd for anything that has a date greater than but not greater than 3 months on from this date.
This is also joined to a separate table that holds a 'Filename' column that is linked to that date.
This is the current, incorrect code that I am using but it is obvious I need to work on this.
Any suggestions would be appreciated!
SELECT DISTINCT n.Filename
, SUBSTRING ('<A61>', 6,5) + SUBSTRING ('<A61>', 11,5)AS ExpiryDate
FROM NODE n
JOIN NODEMETADATA m
ON n.MetaID = m.MetaID
WHERE SUBSTRING ('<A61>', 6,5) + SUBSTRING ('<A61>', 11,5) = DATEADD(MONTH, 3, GETDATE())
AND n.NODETYPE = 9
There are no error messages but the results do not contain anything within and including the 3 months as my DATEADD is seemingly only going to pull anything that is exactly 3 months after.
Due to this I am also unsure if the DATEADD is correctly working on the substring and treating the substring as a combined date.
SSMS 2008
Sample data for specific Cell:
<A4>Company name</A4></A5><A61>27/04/2021</A61><A9>VAT</A9>...and so on

sql date time validation

I have a column with datatype nvarchar and the column contains data i.e
2017-12,
2017-28,
2017-00,
0000,
0000-22
etc'.
So in the column I have invalid data i.e 2017-00,0000,0000-22, so I want the column output should be valid data i.e 2017-12,2017-28 etc'.
And use that column in join condition and case statements.
Use this query
Here i am doing the following.
1. First removing the - from your values
2. Since you said 2017-12,2017-28 is valid data that means year followed by date.
so they should fall between 2017-1 to 2017-31 i.e., 20171 and 207131 when the dash is removed.
Below query first remove the dash for your column then get the values which are between 20171 and 201731. In the query year is not hardcoded, so it works for next year also like 2018.
Select * from yourtable where REPLACE(yourcolumn, '-', '')
between (year(getdate())+'1') AND (year(getdate())+'31')

Is it possible to return part of a field from the last row entered into a table

I am proposing to have a table (the design isn't settled on yet and can be altered dependent upon the views expressed in reply to this question) that will have a primary key of type int (using auto increment) and a field (ReturnPeriod of type Nchar) that will contain data in the form of '06 2013' (representing in this instance June 2013).
I would simply like to return 06 or whatever happens to be in the last record entered in the table. This table will never grow by more than 4 records per annum (so it will never be that big). It also has a column indicating the date that the last entry was created.
That column seems to my mind at least to be the most suitable candidate for getting the last record, so essentially I'd like to know if sql has a inbuilt function for comparing the date the query is run to the nearest match in a column, and to return the first two characters of a field.
So far I have:
Select Mid(ReturnPeriod,1,2) from Returns
Where DateReturnEntered = <and this is where I'm stuck>
What I'm looking for is a where clause that would get me the last entered record using the date the query is run as its reference point(DateRetunEntered of type Date contains the date a record was entered).
Of course there may be an even easier way to guarantee that one has the last record in which case I'm open to suggestions.
Thanks
I think you should store ReturnPeriod as a datetime for example not 06 2013 as a VARCHAR but 01.06.2013 as a DATETIME (first day of 06.2013).
In this case, if I've got your question right, you can use GETDATE() to get current time:
SELECT TOP 1 MONTH(ReturnPeriod)
FROM Returns
WHERE DateReturnEntered<=GETDATE()
ORDER BY DateReturnEntered DESC
If you store ReturnPeriod as a varchar then
SELECT TOP 1 LEFT(ReturnPeriod,2)
FROM Returns
WHERE DateReturnEntered<=GETDATE()
ORDER BY DateReturnEntered DESC
I would store your ReturnPeriod as a date datatype, using a nominal 1st of the month, e.g. 1 Jun 2013, if you don't have the actual date.
This will allow direct comparison against your entered date, with trivial formatting of the return value if required.
Your query would then find the latest date prior to your date entered.
SELECT MONTH(MAX(ReturnPeriod)) AS ReturnMonth
FROM Returns
WHERE ReturnPeriod <= #DateReturnEntered

SSRS report - Group by on Date Part of a Datetime field

I am working on a SSRS report that displays Date, Time and few other columns.
My SP returns just Date Column (which has time part in it) and a few other columns.
In the report I want to group by Date Part and show all details including the time (I used formatting option "T" to display only Time in Date column) in the details group.
For grouping on Date I used:
=FormatDateTime(Fields!TxDate.Value, DateFormat.ShortDate)
Which seems working. However if I try to sort on other columns it is not working. Any thoughts/suggestions on this?
It might be easier if you were to add an extra column into your dataset that was based on your original date+time column but containing just the date. Then you can use this to do a simple group on that column.
For example, if the dataset is based on a SQL query and your date+time column is called [Date], then add another column in the query as CONVERT(DATE, [Date]) AS DateOnly or something similar.