Increasing Binary field length - sql-server

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

Related

How can I return all rows from bulk insert?

I have a large SQL INSERT that I need to do.
The table is called testResult and has an id, name and value column.
The insert looks like this:
INSERT INTO testResult ( name, value )
VALUES
('Helium', '.001'),
('Oxygen', '.19'),
('Palladium', '.054'),
('Carbon', '.21'),
...etc
The id is an identity type column and is auto generated by Sql Server.
Is there a way to return the inserted values along with the auto-generated Id after I do the insert?
Thanks!
Yes, the OUTPUT clause does exactly that.
Example:
INSERT INTO testResult (name, value)
OUTPUT INSERTED.*
VALUES
('Helium', '.001'),
('Oxygen', '.19'),
('Palladium', '.054'),
('Carbon', '.21');
You can specify exact column names, like INSERTED.id.
This is all done within one SQL statement from your end, without the need to write separate SELECT.
Check more in the docs

Power BI Bar char with time text values

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
##:##

SEQUENCE in SQL Server 2008 R2

I need to know if there is any way to have a SEQUENCE or something like that, as we have in Oracle. The idea is to get one number and then use it as a key to save some records in a table. Each time we need to save data in that table, first we get the next number from the sequence and then we use the same to save some records. Is not an IDENTITY column.
For example:
[ID] [SEQUENCE ID] [Code] [Value]
1 1 A 232
2 1 B 454
3 1 C 565
Next time someone needs to add records, the next SEQUENCE ID should be 2, is there any way to do it? the sequence could be a guid for me as well.
As Guillelon points out, the best way to do this in SQL Server is with an identity column.
You can simply define a column as being identity. When a new row is inserted, the identity is automatically incremented.
The difference is that the identity is updated on every row, not just some rows. To be honest, think this is a much better approach. Your example suggests that you are storing both an entity and detail in the same table.
The SequenceId should be the primary identity key in another table. This value can then be used for insertion into this table.
This can be done using multiple ways, Following is what I can think of
Creating a trigger and there by computing the possible value
Adding a computed column along with a function that retrieves the next value of the sequence
Here is an article that presents various solutions
One possible way is to do something like this:
-- Example 1
DECLARE #Var INT
SET #Var = Select Max(ID) + 1 From tbl;
INSERT INTO tbl VALUES (#var,'Record 1')
INSERT INTO tbl VALUES (#var,'Record 2')
INSERT INTO tbl VALUES (#var,'Record 3')
-- Example 2
INSERT INTO #temp VALUES (1,2)
INSERT INTO #temp VALUES (1,2)
INSERT INTO ActualTable (col1, col2, sequence)
SELECT temp.*, (SELECT MAX(ID) + 1 FROM ActualTable)
FROM #temp temp
-- Example 3
DECLARE #var int
INSERT INTO ActualTable (col1, col2, sequence) OUTPUT #var = inserted.sequence VALUES (1, 2, (SELECT MAX(ID) + 1 FROM ActualTable))
The first two examples rely on batch updating. But based on your comment, I have added example 3 which is a single input initially. You can then use the sequence that was inserted to insert the rest of the records. If you have never used an output, please reply in comments and I will expand further.
I would isolate all of the above inside of a transactions.
If you were using SQL Server 2012, you could use the SEQUENCE operator as shown here.
Forgive me if syntax errors, don't have SSMS installed

Why the OUTPUT statement while inserting into the table inside the IF clause returns null on second column?

Here is my "upsert" code:
UPDATE LastTicket SET LastTicketNumber=LastTicketNumber+1
OUTPUT INSERTED.LastTicketNumber WHERE CategoryId='1';
IF ##ROWCOUNT=0 INSERT INTO LastTicket (CategoryId,LastTicketNumber)
OUTPUT INSERTED.LastTicketNumber VALUES ('1','2')
So, when the row exists, it successefully updates, the OUTPUT returns the new, incremented LastTicketNumber.
On the other hand, when the row does not exist, the sql server successefully creates it and populates with the data I am passing to SqlCommand (1,2). So, it creates the row, but returns null. Meaning nothing! Why is that? And why when i replace the "INSERTED.LastTicketNumber" with the "INSERTED.CategoryId" is BEGINS to return not-null, the category id. Why is that? And how to return what I need?
The table has only these two columns and nonclustered primary composite key on both of them.
(MSSQL 2008)
If no row exists in the table, the first time the batch runs it will return two result sets - the first being empty (because there is no row to update) and the second containing the inserted Id.
Perhaps you are seeing the first result set and not the second.
Try the following:
DECLARE #t table (LastTicketNumber int)
UPDATE LastTicket SET LastTicketNumber=LastTicketNumber+1
OUTPUT INSERTED.LastTicketNumber INTO #t (LastTicketNumber) WHERE CategoryId='1';
IF ##ROWCOUNT=0 INSERT INTO LastTicket (CategoryId,LastTicketNumber)
OUTPUT INSERTED.LastTicketNumber INTO #t (LastTicketNumber) VALUES ('1','2')
select LastTicketNumber from #t

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

Resources