Select Only Numbers From Varchar column [duplicate] - sql

This question already has answers here:
Fastest way to remove non-numeric characters from a VARCHAR in SQL Server
(16 answers)
Closed 9 years ago.
In SQL Server 2008 R2 I have some data in a varchar(12) column, it looks something like this:
Data:
%%1234
%1765
34566
123
%SDRMH
HJG434
I'd like to drop the '%' from all the rows that have it and also only select those which are numbers. I've already tried to use the ISNUMERIC() function, but it looks like that leaves the % in the rows.
Any help is greatly appreciated.

You can use a combination of REPLACE and ISNUMERIC to achieve the result set you want:
SELECT REPLACE(columnName,'%','') FROM tableName
WHERE ISNUMERIC(REPLACE(columnName,'%','')) = 1

You could use the REPLACE function to strip out all the % instances
SELECT REPLACE(column_name,'%','');
GO

This function would be expensive to use, but it could help you create / populate a new column properly typed as an int (for example):
create function [dbo].[is_int]
( #value as varchar(max) )
returns int
as
begin
declare #return int
set #return = 5
while len(#value) > 0
begin
if left(#value,1) in('1','2','3','4','5','6','7','8','9','0')
begin
set #value = right(#value, len(#value)-1)
set #return = 1
end
else
begin
set #return = 0
set #value = ''
end
end
return #return
end
or you could modify it to return the integers themselves, instead of a true/false (1/0).

You could do something like
SELECT REPLACE(column,'%','') WHERE ISNUMERIC(REPLACE(column,'%','')) = 1

Related

how to sum up value within one cell SQL

I have some binary values such as 00, 0000, 001000.11111000, 1111100000
I need to sum it up so it turns into 0, 0, 1, 5, 5 ( sum 0s and 1s up)
how can we do that in SQL please?
Thanks
Assumption:
The binary values are stored as string.
Each value is in its own cell in a table. Something like:
BinaryValues (Consider it a column name)
00
0000
001000
and so on.
You want to add up the individual digits to get the sum.
SQL Product you are usind supports functions, looping, string manipulation like substring, extracting string length etc.
As per my best knowledge these are primitives available in all SQL products.
Solution:
Write a function (call it by any name. Ex: AddBinaryDigits) which will take the binary value in string format as input.
Inside the function and do a string manipulation. Extract each digit and add it up. Return the sum as result.
Call the function:
If using binary values stored in a table:
SELECT AddBinaryDigits(BinaryValues) FROM <WhatEverTableName>
If using fixed value:
SELECT AddBinaryDigits('00')
SELECT AddBinaryDigits('0000')
SELECT AddBinaryDigits('001000')
and so on.
Edited to include the request to create function.
CREATE FUNCTION <funtionName>
(
#ParameterName AS VARCHAR(expected string length like 10/15/20 etc.)
)
RETURNS INT
BEGIN
SQL Code to sum
RETURN SummedUpValue
END
Use the below query. If needed convert it into function.
create function dbo.fnSumChars(#someInt VARCHAR(20))
RETURNS INT
AS
BEGIN
DECLARE #count INT = LEN(#someInt),
#counter INT = 1
DECLARE #Sum INT = 0
WHILE #counter <= #count
BEGIN
SELECT #sum += CAST(SUBSTRING(CAST(#someInt AS VARCHAR), #counter, 1) AS int)
SELECT #counter += 1
END
RETURN #sum --5
END
This is the function and you can call this function like below
SELECT dbo.fnSumChars('1111100000')
If these are already in string format, this is the easiest:
select len(replace('1111100000', '0', ''))
No need for a function either, because it can be inlined in the query. Functions, even the light ones, incure perf penalty.

SQL Insert records in between characters

I would like to ask something, I'm working on generating an excel containing records from table.
Now, I have records in a column ex. 123450000
The customer wants the data to be 12345-0000
So in my select statement, every 5th of the column, there should be a dash in between
If the record has only 5 characters, then no "-" will be placed
I'm using SQL Server database for this.
Thanks :)
SELECT STUFF('12345000',6,7,'-0000');
This function should help;
DELIMITER //
CREATE FUNCTION dashify(#indata VARCHAR(MAX)) RETURNS VARCHAR(MAX) AS
BEGIN
DECLARE #breakat INT = 5
WHILE #breakat < LEN(#indata)
BEGIN
SET #indata = STUFF(#indata, #breakat+1, 0, '-');
SET #breakat = #breakat + 6
END
RETURN #indata;
END;
//
SELECT dbo.dashify('12345678901234') //
> 12345-67890-1234

Detect my parameter in my stored procedure contains only 6 characters

How do I detect my #AE_LedgerAC parameter in my store procedure contains only 6 characters? if it contains only 6 characters, I want to add in front the details contain on this table BIN.BranchId:
ALTER PROCEDURE [dbo].[AccountEntries]
#AE_LedgerAC varchar(21)=null,
#AE_Transaction_Ref varchar(50)=null,
#AE_Detail nvarchar(50)=null,
AS
DECLARE #NEW_LedgerAC varchar(21) = null
IF(LEN(#AE_LedgerAC) = 6)
BEGIN
#NEW_LedgerAC = SELECT TOP 1 BIN.BranchId FROM BIN + #AE_LedgerAC
ELSE
#NEW_LedgerAC = #AE_LedgerAC
INSERT INTO [AccountEntries](LedgerAC, Transaction_Ref, Detail)
VALUES (#AE_LedgerAC, #AE_Transaction_Ref, #AE_Detail)
use LEN ( string_expression )
IF(LEN(#AE_LedgerAC) = 6)
BEGIN
...
END
Assuming this is MSSQL, check out the following links from Microsoft's MSDN:
LEN
IF/ELSE
ISNULL
SET #local_variable

SQL Server converting varbinary to string

I want to do conversion in T-SQL from a varbinary type to string type
Here is an example :
First I got this varbinary
0x21232F297A57A5A743894A0E4A801FC3
And then I want to convert it to
21232f297a57a5a743894a0e4a801fc3
How to do this?
Try:
DECLARE #varbinaryField varbinary(max);
SET #varbinaryField = 0x21232F297A57A5A743894A0E4A801FC3;
SELECT CONVERT(varchar(max),#varbinaryField,2),
#varbinaryField
UPDATED:
For SQL Server 2008
I know this is an old question, but here is an alternative approach that I have found more useful in some situations. I believe the master.dbo.fn_varbintohexstr function has been available in SQL Server at least since SQL2K. Adding it here just for completeness. Some readers may also find it instructive to look at the source code of this function.
declare #source varbinary(max);
set #source = 0x21232F297A57A5A743894A0E4A801FC3;
select varbin_source = #source
,string_result = master.dbo.fn_varbintohexstr (#source)
If you want to convert a single VARBINARY value into VARCHAR (STRING) you can do by declaring a variable like this:
DECLARE #var VARBINARY(MAX)
SET #var = 0x21232F297A57A5A743894A0E4A801FC3
SELECT CAST(#var AS VARCHAR(MAX))
If you are trying to select from table column then you can do like this:
SELECT CAST(myBinaryCol AS VARCHAR(MAX))
FROM myTable
This works in both SQL 2005 and 2008:
declare #source varbinary(max);
set #source = 0x21232F297A57A5A743894A0E4A801FC3;
select cast('' as xml).value('xs:hexBinary(sql:variable("#source"))', 'varchar(max)');
I looked everywhere for an answer and finally this worked for me:
SELECT Lower(Substring(MASTER.dbo.Fn_varbintohexstr(0x21232F297A57A5A743894A0E4A801FC3), 3, 8000))
Outputs to (string):
21232f297a57a5a743894a0e4a801fc3
You can use it in your WHERE or JOIN conditions as well in case you want to compare/match varbinary records with strings
Here is a simple example I wrote to convert and convert back using the 2 convert methods, I also checked it with a fixed string
declare #VB1 VARBINARY(500),#VB2 VARBINARY(500),#VB3 VARBINARY(500)
declare #S1 VARCHAR(500)
SET #VB1=HASHBYTES('SHA1','Test')
SET #S1=CONVERT(varchar(500),#VB1,2)
SET #VB2=CONVERT(varbinary(500),#S1,2)
SET #VB3=CONVERT(varbinary(500),'640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA',2)
SELECT #VB1,#S1,#VB2,#VB3
IF #VB1=#VB2 PRINT 'They Match(2)'
IF #VB1=#VB3 PRINT 'They Match(3)'
PRINT str(Len(#VB1))
PRINT str(Len(#S1))
PRINT str(Len(#VB2))
SET #VB1=HASHBYTES('SHA1','Test')
SET #S1=CONVERT(varchar(500),#VB1,1)
SET #VB2=CONVERT(varbinary(500),#S1,1)
SELECT #VB1,#S1,#VB2
IF #VB1=#VB2 PRINT 'They Match(1)'
PRINT str(Len(#VB1))
PRINT str(Len(#S1))
PRINT str(Len(#VB2))
and the output
|||
0x640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA|640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA|0x640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA|0x640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA
(1 row(s) affected)
They Match(2)
They Match(3)
20
40
20
||
0x640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA|0x640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA|0x640AB2BAE07BEDC4C163F679A746F7AB7FB5D1FA
(1 row(s) affected)
They Match(1)
20
42
20

Modify int result of count in sql server 2005

I am working on sql server 2005 and I am taking count from a specific table
SELECT count(StudentIdReference) as studentCount FROM StudentTable
Right now this select statement is returning me result like 2 or 78 or 790. But in future it will grow rapidly and on UI I don't have sufficient space to show the digit like 1000000.
What I want that after 3 digit, I will get the number like 1K or 1.6K, just as we see on stackoverflow.
This would be simpler to be done in the Presentation Layer of your application.
You coud write a user function and do something like this....
CREATE FUNCTION prettyPrint
(#number int)
RETURNS varchar(30)
AS
BEGIN
declare #return varchar(30)
set #return = cast(#number as varchar(3))
if #number > 1000
set #return = ''+ cast((#number/1000) as varchar(3)) + '.' + cast((#number % 1000)/100 as varchar(3)) +'K'
-- here must be more 'exceptions' or change all this about the magic number 1000
return #return
end
select dbo.prettyPrint(1500)
SELECT prettyPrint(count(StudentIdReference)) as studentCount FROM StudentTable
As others have stated you should really be doing this in your Presentation Layer not at the DB, however, this will do it for you:
Declare #StudentCount int,
#StudentCountFormatted varchar(10)
Select #StudentCount = Count(StudentIdReference) as studentCount FROM StudentTable
If #StudentCount > 999
Begin
Select #StudentCountFormatted = Convert(Varchar(10), Convert(numeric(19,1), (#StudentCount/ 1000.00))) + 'K'
End
Else
Begin
Select #StudentCountFormatted = #StudentCount
End
Select #StudentCountFormatted
You need to write your own logic to show such text. There is no built-in method.
I would return the COUNT as-is from SQL Server and leave the formatting up to the UI. This is because:
1) usually easier/performant to do formatting/string manipulation outside of SQL
2) different places in your code using the same query may want to use the data in different ways (maybe not now, but could do in future) so returning the count as-is gives you that flexibility - i.e. won't need 1 version to return the count as an INT and another to return the same as a formatted VARCHAR
You could do it in SQL, but in general I believe in pushing this in to the UI as it's a display/formatting behaviour.
You can always try something like this
SELECT
CASE
WHEN len(cast(count(*) as varchar(10)))< 4 then cast(count(*) as varchar(10))
WHEN len(cast(count(*) as varchar(10)))> 4 and len(cast(count(*)as varchar(10)))< 7
THEN cast(cast(count(*) / 1000.0 as decimal(10,1)) as varchar(10)) + 'k'
ELSE cast(cast(count(*) / 1000000.0 as decimal(10,1)) as varchar(10)) + 'm'
END StudentCount
FROM StudentTable