How to format date in SSRS? - date

In SSRS report query is generating date as a column in the format of :
Sales ID20200331 ID20200430 ID20200531
To remove the ID i used following expression:
=Right( Fields!ID20210331.Value, len(Fields!ID20210331.Value) - 2)
This gives me 84, instead of removing ID.
How can I remove ID and format date as 2020 Mar etc.
Thanks

If your fields values are "ID20200430" etc then in SSRS you can use something like this..
=DateSerial(
MID(Fields!IDDate.Value, 3, 4),
MID(Fields!IDDate.Value, 7, 2),
RIGHT(Fields!IDDate.Value, 2)
)
However It appears that it's your column [names] that represent dates is this correct?
If this is true, then you would have to UNPIVOT the columns in SQL then convert the resulting values into a real date format.
Here' some sample data to show how to do this.
DECLARE #t TABLE (Sales varchar(10), ID20200331 int, ID20200430 int, ID20200531 int)
INSERT INTO #t VALUES
('A', 1,2,3),
('B', 4,5,6),
('C', 7,8,9)
SELECT
Sales, IdDate, SomeNumber
, MyDate = DATEFROMPARTS(SUBSTRING(IdDate, 3, 4), SUBSTRING(IdDate, 7, 2), SUBSTRING(IdDate, 9, 2))
FROM #t
UNPIVOT(
SomeNumber FOR IdDate IN ([ID20200331],[ID20200430],[ID20200531])
) unpvt
Which gives us this including the myDate column which is the correct date type
You could then use this in a matrix control in SSRS to get the data back into a pivoted view

Related

Impala - handle CAST exception for column

SELECT src_user, CAST(start_time as timestamp) as start_time_ts, start_time, dest_ip, src_ip, count(*) as `count`
FROM mytable
WHERE
start_time like '2022-06%'
AND src_ip = '2.3.4.5'
AND rule_name like '%XASDF%'
group by 1, 2, 3, 4, 5 order by 2 desc)
I'm getting an error with pyspark:
[Cloudera][JDBC](10140) Error converting value to Timestamp.
Now, if I don't order by in the query I can get a good result set with timestamps properly converted, so there's a row somewhere that starts with 2022-06 that is not parsing correctly.
How can I setup my error handling so that it will show me the actual value that is causing the error, rather than telling me what the error is?
Here's the code:
df = spark.read.jdbc(url= 'jdbc:impala://asdf.com/dasafsda', table = select_sql, properties = properties)
df.printSchema()
df.show(truncate=False)
I dont know Impala specific function but lets say your normal date is in format yyyy-mm-dd so you can do length check on it if you suspect 2022-06 is causing problem and then set it to default date for error value and store it in separate column to see its real value
like
SELECT src_user,CASE WHEN LENGTH (start_time) =10 THEN CAST(start_time as timestamp) ELSE CAST("1900-01-01" as timestamp) END as start_time_ts, start_time as start_time_raw,start_time, dest_ip, src_ip, count(*) as `count`
FROM mytable
WHERE
start_time like '2022-06%'
AND src_ip = '2.3.4.5'
AND rule_name like '%XASDF%'
group by 1, 2, 3, 4, 5 order by 2 desc)
after this query just filter 1900-01-01 to see vales in start_time_raw which were not casted

Create `has_($value)`column for table values

I would like to perform an operation similar to a dynamic pivot table. I utilize Postgresql as database framework. The table t has a column with values 10, 20, and 30. I wish to create n columns, in this case equal to 3, to allow the boolean assignment has_($value) equal to 1 if existent in respective group, or 0 if not. I tried to understand tablefunc and crosstab without success.
CREATE TABLE IF NOT EXISTS t (
id INTEGER NOT NULL,
value INT NOT NULL
)
INSERT INTO t (id, value) VALUES
(1, 10),
(1, 20),
(2, 10),
(3, 30),
(3, 20)

Retrieving Row column values as their Scala type and not Column

What I'm trying to achieve is inferring values to certain DataFrame columns taking into account values of each individual row.
.withColumn("date", when(col("date").isNull, lit(new DateTime(col("timestamp").as[Long]).getYear)))
The problem is that I can't wrap my head around how to retrieve, for each of the Row objects, its value for the given column. I've seen other solutions but they either list the whole set of values for all of the rows, or just get the first value of them, which isn't what I'm trying to achieve.
Image an example DF like this...
(year, val1, val2, val3, timestamp)
(null, 10, 12, null, 123456789)
(null, 11, 12, null, 234567897)
And what I want to see after applying individual functions (for example, extracting year from timestamp) to each of the Rows is...
(year, val1, val2, val3, timestamp)
(2018 [using DateTime class], 10, 12, 1012, 123456789)
(2018 [using DateTime class], 12, 12, 1212, 234567897)
Is there any way of doing this?
Thats where UDFs come into play :
val udf_extractYear = udf((ts:Long) => new DateTime(ts).getYear)
then you can use this using e.g.
df
.withColumn("year", when(col("year").isNull, udf_extractYear(col("timestamp"))).otherwise(col("year")))
.show()
As you can see your timestamp column is automatically mapped to Long

Picking up the date from very long string name column in Sybase

I am working in Sybase with these this table having column 'ID', 'File_Name'
Table1
IDS File_Name_Attached
123 ROSE1234_abcdefghi_03012014_04292014_190038.zip
456 ROSE1234_abcdefghi_08012014_04292014_190038.zip
All I need is to pickup the first date given in file name.
Required:
IDS Dates
123 03012014
456 08012014
You can use SUBSTRING and PATINDEX to find start_index of date:
LiveDemo
CREATE TABLE #table1(IDS int, File_Name_attached NVARCHAR(100));
INSERT INTO #table1
VALUES (123, 'ROSE1234_abcdefghi_03012014_04292014_190038.zip'),
(456, 'ROSE1234_abcdefghi_08012014_04292014_190038.zip');
SELECT
IDS,
[DATES] = SUBSTRING(File_Name_attached,
PATINDEX('%_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_%', File_Name_attached) + 1,
8)
FROM #table1;
Warning
I have no Sybase DB for testing so if this won't work let me know.

How to extract month from a String representing a date in TSQL

In my SQL Server database I have the dates stored as char(12) in the following format:
yyyymmddhhmm
I didn't create the definition and I cannot modify it. I always dealt with this string at application level, by using C#.
Now I need to perform a task in TSQL and I need to group by month. Therefore I need to extract the month. Is there any TSQL function available for this task?
In case there is not, is it a good solution to create a stored procedure, getMonth(stringDate) that takes the string and extract the 4th and 5th characters from it? Can I use the clause:
group by getMonth(stringDate)
in my query? Thanks
You can;
SUBSTRING(fld, 5, 2)
Personally I would not create a UDF for something so simple (which is what you would consider rather than an SP) unless you find yourself needing to cast that string to DATETIMEs
You can use the following to get your month, since month is always 2 characters after the 4 character year.
declare #date char(12)
set #date = '201203220906'
select substring(#date, 5, 2)
results: 03
or another way to get it is, then you can group by the results in either query:
declare #date char(12)
set #date = '201203220906'
select Month(cast(left(#date, 8) as datetime))
Assuming
CREATE TABLE #Table(
DateValue char(12),
Value int)
INSERT INTO #Table VALUES ('20120110833', 1)
INSERT INTO #Table VALUES ('20120110833', 2)
INSERT INTO #Table VALUES ('20120110833', 3)
INSERT INTO #Table VALUES ('20120210833', 4)
INSERT INTO #Table VALUES ('20120210833', 5)
You can simply do this:
select
MONTH(CAST(LEFT(DateValue, 8) as datetime)) Month,
SUM(value)
FROM
#Table
GROUP BY
MONTH(CAST(LEFT(DateValue, 8) as datetime))
trim the hour/minute part, cast as datetime and apply MONTH function
For this specific case (date in char type with format yyyymmddhhmm), you can use in your query the next functions:
substring (to extract yyyymmdd),
convert (to get datetime value),
datepart (to get month component)
...then you can group by month (or whatever date component), change datecolumnname and tablename with appropiate values
select datepart(month,convert(datetime,substring(datecolumnname,1,8),112)),
count(datepart(month,convert(datetime,substring(datecolumnname,1,8),112)))
from tablename
group by datepart(month,convert(datetime,substring(datecolumnname,1,8),112))