SSRS Format string to date time DD MMM YYYY HH MM - sql-server

Display in reporting services text box - data is grouped.
Best way to format grouped data 20170518110610 to DD MMM YYYY HH MM?
I.e.
18 May 2017 11:06
Tried Text box properties - number - custom format - dd-mmm-yyyy hh:mm the result is the same 20170518110610

You have problems because 20170518110610 is a varchar. You need to convert it to date before format it.
Possible solutions:
1) Return a datetime not varchar from datasource (preferred)
2) Parse value in SSRS then format value:
=Format(DateTime.ParseExact("20170518110610", "yyyyMMddHHmmss", Nothing), "dd-mmm-yyyy hh:mm")

Related

How to convert string dd-mmm-yyyy to date in sql/snowflake?

I am getting date in the format Sat Dec 31 07:56:31 UTC 2020 in snowflake in string format.
Ideally I would like to convert it to Timestamp. But if not, I can work with converting dd-mmm-yyyy to date format in sql.
I tried using substr to extract dd, mmm , yyyy from the string, and want to convert it to timestamp.
SELECT TO_TIMESTAMP(REPLACE('Sat Dec 31 07:56:31 UTC 2022','UTC',''), 'DY MON DD HH24:MI:SS YYYY')
This worked for me.

Formatting YYYYMM to MMMYYYY in SSRS

I would like to to format one of my data fields to be in the date format of MMM YYYY. For example, if "201209" is being returned, then I would like it to be displayed as Sep 2012. On the SQL side I am using a CAST to only look at the Year and Month, normally this field would look like "20120914". Here is what I used in the procedure:
cast(left(cast(TransactionDateKey as varchar(100)), 6) as int)
Within SSRS, I use the following code:
=Format(DateValue(MonthName(Right(Fields!Month.Value, 2))
+ "," +
Left(Fields!Month.Value,4)), "Y")
however the output is "September 2012", how would I go about get the abbreviation "Sep", instead of the full month name?
Another Option is Format() in SQL Server
Select Format(CONVERT (date,convert(char(8),TransactionDateKey )),'MMM yyyy')
For example
Select Format(CONVERT (date,convert(char(8),20120914 )),'MMM yyyy')
-- Returns Sep 2012
I should note that Format() is not known for its performance, but does offer some nice features
If you are returning an actual datetime field in your dataset - which is what your TransactionDateKey appears to be - you can handle the formatting completely in an SSRS expression using format:
=format(Fields!TransactionDateKey.Value,"MMM yyyy")
If you are returning your yyyyMM for grouping purposes, there is nothing stopping you grouping on a datetime value of the first of the month for TransactionDateKey within your SQL:
select dateadd(m,datediff(m,0,TransactionDateKey),0) as FirstDayOfTheMonth
If you absolutely need to return a varchar in the format yyyyMM, you can convert it to MMM yyyy, though you first need to convert it to a date in your expression - slashes and all - before SSRS will play ball:
=format(cdate(left(Fields!Month.Value,4) & "/" & right(Fields!Month.Value,2) & "/01"), "MMM yyyy")
In SSRS you can use:
=StrConv(LEFT(
MONTHNAME(REPLACE(RIGHT(Fields!Month.Value,2),"0","")),3
),vbProperCase,NOTHING) & " " & LEFT(Fields!Month.Value,4)
Which returns Sep 2012 for 201209.
Let me know if this helps.

How to create query with format date DD-MM-YYYY

I have query database like this:
SELECT * FROM TABLE WHERE start_date = '01-10-2016' //DD-MM-YYYY
But above code error and actually in database date format is YYYY-MM-DD.
So how to create query with format date DD-MM-YYYY?
A proper date column (data type date !) has no format. Then it's enough to get your data input for a date column right, use to_date() for non-standard input format like #Shiva posted. Or better yet, always provide date literals in ISO 8601 format 'YYYY-MM-DD' to begin with, which works with any locale setting.
If you are running a broken design with dates stored as text, then combine to_date() and to_char() to transform any valid date format into any other text format:
SELECT * FROM tbl
WHERE start_date = to_char(to_date('01-10-2016', 'DD-MM-YYYY'), 'YYYY-MM-DD');
May be you can use to_date function to convert the above format into the standard format.
For instance,
SELECT to_date('01-10-2016', 'DD-MM-YYYY');
----------
2016-10-01
1 row
https://www.techonthenet.com/postgresql/functions/to_date.php

How to convert date time format in SQL Server 2014

I'm using this query
SELECT convert(nvarchar(MAX), GETDATE(), 22) AS Date
Result: 08/05/16 12:23:08 PM
But I want result like this 8/5/2016 12:23:08 PM
dd/mm/yyyy hh:mm:ss a
As of SQL Server 2012 the FORMAT function is available allowing you to specify the format of data types and is locale-aware so it will consider date formatting in relation to the session's language or optional culture parameter.
You can achieve your custom formatting like so: FORMAT(GETDATE(), 'd/M/yyyy hh:mm:ss tt')
Note your requested format dd/mm/yyyy hh:mm:ss a is incorrect as in the case of single digits you want to remove zero padding i.e. 10/8/2016 not 10/08/2016. That's why in the format string I use only d and M.
Also, pay attention to #GarethD comment about the cost on larger datasets.
You could use the FORMAT function in T-SQL : https://msdn.microsoft.com/en-us/library/hh213505(v=sql.120).aspx
Here is the code :
SELECT FORMAT(GETDATE(), 'd/M/yyyy hh:mm:ss tt')

how to display date format as MM/DD/YYYY HH:MM:SS Z in ssrs

Can you please suggest me how to display date format for a date column as MM/DD/YYYY HH:MM:SS Z format in ssrs ?
I have googled but i am not getting exact format.
I was wondering how to do this(how to display date in UTC format ('MM/dd/yyyy HH:mm:ss Z'). But in Sql Server 2012 it's too easy. Can be done like this.
In Sql query we can do like below
SELECT EmpName, FORMAT(ApprovedDate, 'MM/dd/yyyy HH:mm:ss Z') as ApprovedDate From Employee
Or in SSRS report expression we can do like below
Format(Fields!ApprovedDate.Value, "MM/dd/yyyy hh:mm:dd" & " Z")
EmpName and ApprovedDate are the columns of Employee Table.
Hope this might be helpful for others
=System.TimeZone.CurrentTimeZone.ToUniversalTime(First(Fields!NOW.Value, "DataSet1"))
This works for me:
=Format(Fields!FeildName.Value, "MM/dd/yyyy hh:mm:ss tt")

Resources