Power BI Bar char with time text values - sql-server

I have a table with the following data
create table #tbl (Product varchar(20),dates Datetime)
insert into #tbl values
('Fan','2014-09-15 07:24:37.460'),
('Byke','2015-11-15 01:46:07.460'),
('Book','2017-05-25 12:52:37.670'),
('Pencil','2015-12-30 17:48:07.530'),
('Shoes','2016-09-05 12:13:19.600')
select
Product,
format(dates,'MM:ss') AS Times
from #tbl
drop table #tbl
The time have been converted to text value in SQL query before I import it in Power BI. When I tried to create a bar chart with the text value I created a measure based on the time
timesValue = MAX(Products[Times])
When I place them in my visual I can't see any value
My expected output is

My solution for this:
First create a new column from your StringTime column
Column = INT(SUBSTITUTE('Table'[times],":",""))
Then change format to
##:##

Related

Increasing Binary field length

I have a table with a populated column of type binary(24). I want to increase this field length to binary(32). This part's easy enough. The problem I'm running into is when I try to query the table based on that field's previous value. (We were using a hashbytes function).
So BEFORE the field length increase, I could just use:
SELECT * FROM Table WHERE Field = HASHBYTES('md5', 'value')
AFTER the field length increase, this query returns nothing. How would I change the query to return the appropriate value? What can I add on to the end of the binary in order for it to be recognized?
Thanks!
Edit: I've apparently done something ELSE wrong, as this works fine.
CREATE TABLE #test (test binary(24) null)
insert into #test (test) values (hashbytes('md5', 'test'))
select * from #test
ALTER TABLE #test ALTER COLUMN test binary(32) null
select * from #test where test = hashbytes('md5', 'test')
drop table #test
Could you use CONVERT in the WHERE to turn it back into binary(24) ?
CONVERT binary

How to extract SQL Server image column data into readable format and store into another column?

I have a SQL Server database in which there are around 5 lac records in a table called message_mst, here is the table structure
Table name : message_mst
Columns:
message_id int
message_body image
I am not the person who built this database but whoever built this used image column to store all the message text which is simply plain text. But if we select the records, message_body prints all the text in HEX format. I want to convert it into readable format and then store into new field called message_body_readable.
How can I do that?
You can do it converting the field first to varbinary and than to varchar.
declare #t table (i image)
insert into #t values('some text')
select i, CAST(cast(i as varbinary(max)) as varchar(max))
from #t
Can you try this once..?
SELECT CONVERT(VARCHAR(1000), message_body, 0) FROM message_mst

Insert distinct data into a new table using select and calculate sums at the same time

Using MS SQL I'm trying the following:
I have one table with game data. In this table a player name can occur many times.
I now want to create a summary table, where every player has a single row.
Using the following statement, I can populate the summary tables "playername" column.
INSERT INTO PLAYER_Summary (Playername)
SELECT DISTINCT [Playername]
FROM ppPLAYER
The summary table also has columns for sums of the players results.
How can i populate my summary table so that each name and the correlating sums are in it once.
I need something like:
INSERT INTO PLAYER_Summary
(Playername, WinWhat )
SELECT DISTINCT [Playername] FROM ppPLAYER , SUM(WinWhat) FROM ppPLAYER
How can I make this work and parse the unique playername with his sum?
Thanks
INSERT INTO PLAYER_Summary
(Playername, WinWhat )
SELECT Playername, SUM(WinWhat)
FROM ppPLAYER
group by Playername

Update a part of column value in SQL Server

I have a database in SQL Server with its data. I need change a part of some columns value in some conditions.
Imagine the value as "0010020001".
002 belongs to another value in my database and whenever I want to change it to 005, I must update the previous 10-digits code to "001005001".
Actually, I need to update just a part of columns value using UPDATE statement. How can I do it (in this example)?
While everyone else is correct that if you have control of the schema you should definitely not store your data this way, this is how I would solve the issue you as you described it if I couldn't adjust the schema.
IF OBJECT_ID('tempdb..#test') IS NOT NULL
DROP TABLE #test
create table #test
(
id int,
multivaluecolumn varchar(20)
)
insert #Test
select 1,'001002001'
UNION
select 2,'002004002'
UNION
select 3,'003006003'
GO
declare #oldmiddlevalue char(3)
set #oldmiddlevalue= '002'
declare #newmiddlevalue char(3)
set #newmiddlevalue = '005'
select * from #Test
Update #Test set multivaluecolumn =left(multivaluecolumn,3) + #newmiddlevalue + right(multivaluecolumn,3)
where substring(multivaluecolumn,4,3) = #oldmiddlevalue
select * from #Test
Why dont you use CSV(comma separated values) or use any other symbol like ~ to store tha values. Once you need to update a part of it use php explode function and then update it. After your work is done, concat all the values again to get the desired string to be stored in your column.
In that case your column will have values VARCHAR like 001~002~0001

SQL Server 2008 Beginner Script Question On effecting all rows

I'm useing SQL Server 2008 and I have a small script to split a row of data into several different columns. My question is how do I get the script to effect all the rows in the column? Right now the data row is just a string literal. I'm wondering if there is another way to wright this so I do not have to keep replaceing the DATA ROW EXAMPLE line with each rows data. The script is below.
DECLARE #TEXTINPUT VARCHAR(120)
SET #TEXTINPUT = 'DATA ROW EXAMPLE 12 89564 DATA ROW EXAMPLE'
SELECT #TEXTINPUT AS TEXTINPUT
SELECT SUBSTRING(#TEXTINPUT,1,4) AS UNIT,
SUBSTRING(#TEXTINPUT,6,3) AS NAME,
SUBSTRING(#TEXTINPUT,10,7) AS ACCOUNT,
SUBSTRING(#TEXTINPUT,18,2) AS NOTE
INSERT INTO ExampleTable
(UNIT, NAME, ACCOUNT, NOTE)
VALUES (SUBSTRING(#TEXTINPUT,1,4), SUBSTRING(#TEXTINPUT,6,3), SUBSTRING(#TEXTINPUT,10,7),
SUBSTRING(#TEXTINPUT,18,2))
GO
I assume below the table with source data is called SourceTable and the column you are splitting is called SourceCol
INSERT INTO ExampleTable
(UNIT, NAME, ACCOUNT, NOTE)
SELECT SUBSTRING(SourceCol,1,1) AS UNIT,
SUBSTRING(SourceCol,3,9) AS NAME,
SUBSTRING(SourceCol,13,6) AS ACCOUNT,
SUBSTRING(SourceCol,20,3) AS NOTE
FROM SourceTable

Resources