substract specific string from data sql - sql

I am new to SQL
If I have a column like this
ID
00001234
00012345
00001235
00123456
I want to see a column of ID without '0' Like this
ID
1234
12345
1235
123456
How can I start? any advice?

In SQL Server you can use:
SELECT ID,
REPLACE(LTRIM(REPLACE(ID, '0', ' ') ), ' ', '0')
FROM mytable
The above can be easily adjusted to any other RDBMS you may use.

Cast it to Bigint and cast it back to varchar
Note:Assumption: RDBMS SQL SERVER, ID is of character type
SELECT * INTO #TAB FROM (
select '00001234' ID
UNION ALL
select '00012345'
UNION ALL
select '00001235'
UNION ALL
select '00123456'
)A
SELECT CAST(CAST(ID AS BIGINT) AS VARCHAR(50)) FROM #TAB

Related

How to write different data type in one result column

For example, i have two columns in one table named tab1
1th column has type int, it is PK column with numerals
2nd one has nvarchar type.
ID Name
1 Anna
2 Vladimir
What i want:
Result
ID_Name
1 Anna
2 Vladimir
Use CONCAT function in your select:
Select Concat(ID, ' ', Name) AS ID_Name FROM tab1
your first column is of type int so you need to convert it intoor nvarchar to concatenate with name column wich is nvarchar
SELECT CONVERT(VARCHAR,ID) + ' ' + Name AS ID_Name
FROM my_table
also you can use CONCAT like this
SELECT CONCAT(ID,' ',Name) FROM my_table
select concat(column1,' ',column2) from table
in your question, it will be
select concat(ID,' ',Name) from tab1
SELECT CONVERT(VARCHAR(30),ID) + ' ' + Name AS ID_Name
FROM my_table
you can use (concat('column1','column2') as column name)
You need to use CONVERT() or CAST() string function while you want to use integer column concate with VARCHAR OR NVARCHAR column.
From SQL Server 2012 onward, you can use CONCAT() string function, which is take care of integer to string conversion.
Please check below select script.
SELECT
*
INTO #tblA
FROM
(
SELECT 1 ID,'Anna' Name UNION ALL
SELECT 2 ID,'Vladimir' Name
) A
SELECT
CONVERT(NVARCHAR(11),t.ID) + ' ' + t.Name AS ID_Name
--CONCAT(t.ID,' ',t.Name) AS ID_Name /*SQL Server 2012 Onwards*/
FROM #tblA t

How to Union the title in sql query with data?

I want the title for the SQL query.How Can I generate it? In the following code ContractID is int, Contract Number is nvarchar and ClientContractNumber is nvarchar.
I have tried the following Code
SELECT *
INTO #temp
FROM
(SELECT ContractID,ContractNumber, ClientContractNumber
FROM dbo.bsContract
WHERE ContractNumber = 'CR6359-V1') t
SELECT 'Title',' ',' '
UNION ALL
SELECT 'ContractID', 'ContractNumber', 'ClientContractNumber'
UNION ALL
SELECT *
FROM #temp
DROP TABLE #temp
The following error occurs:
Msg 245, Level 16, State 1, Line 7
Conversion failed when converting the varchar value 'Title' to data type int.
The expected output SHOULD BE:
Title
ContactID ContractNumber ClientContractNumber
----------------------------------------------------
6368 cr1234 newContract
use cast for ContractID
SELECT 'Title',' ',' '
UNION ALL
SELECT 'ContractID','ContractNumber','ClientContractNumber'
UNION ALL
SELECT cast( ContractID as varchar(50)),ContractNumber, FROM #temp
DROP TABLE #temp
You need to cast the contract ID to varchar before unioning since union requires all the datatypes to match.
Edit:
SELECT * INTO #temp FROM (SELECT cast(ContractID as varchar) ContractID,ContractNumber,ClientContractNumber
FROM dbo.bsContract WHERE ContractNumber='CR6359-V1') t
SELECT 'Title' ContractID,' ' ContractNumber,' ' ContractNumber
UNION ALL
SELECT 'ContractID' ContractID,'ContractNumber' ContractNumber,'ClientContractNumber' ContractNumber
UNION ALL
SELECT * FROM #temp
DROP TABLE #temp

Dynamic Comma Seperated string into different column

May someone please help me for this strange scenario. i have a data as given below.
DECLARE #TABLE TABLE
(
ID INT,
PHONE001 VARCHAR(500)
)
INSERT TEST
SELECT 1,'01323840261,01323844711' UNION ALL
SELECT 2,'' UNION ALL
SELECT 3,',01476862000' UNION ALL
SELECT 4,'01233625418,1223822583,125985' UNION ALL
SELECT 5,'2089840022,9.99021E+13'
and i am trying to put in seperate column for each comma value. the max number of column depends on the largest comma seperated string.
Expected Output
1|01323840261|01323844711|''
2|''|''|''
3|01476862000|''|''|
4|01233625418|1223822583|125985|
5|2089840022|9.99021E+13|''|
try
select id,T.c.value('t[1]','varchar(50)') as col1,
T.c.value('t[2]','varchar(50)') as col2 ,
T.c.value('t[3]','varchar(50)') as col3 from
(select id,cast ('<t>'+ replace(PHONE001,',','</t><t>') +'</t>'
as xml) x
from #TABLE) a cross apply x.nodes('.') t(c)

Computed column does not include one of the values

I'm trying to create a computed column in order to have a unique index on a nullable column that ignores NULL rows1. I've composed this test case:
SELECT TEST_ID, CODE, UNIQUE_CODE, CAST(UNIQUE_CODE AS VARBINARY(4000)) AS HEX
FROM (
SELECT TEST_ID, CODE,
ISNULL(CODE, CONVERT(VARCHAR, SPACE(10)) + CONVERT(VARCHAR, TEST_ID)) AS UNIQUE_CODE
FROM (
SELECT 1 AS TEST_ID, 'ABCDEFGHIJ' AS CODE
UNION ALL
SELECT 2, 'XYZ'
UNION ALL
SELECT 3, NULL
) TEST
) X;
It works as expected when CODE is not null but I only get a string of whitespaces when CODE is null (i.e., the trailing TEST_ID is missing):
TEST_ID | CODE | UNIQUE_CODE | HEX
--------+------------+-------------+-----------------------
1 | ABCDEFGHIJ | ABCDEFGHIJ | 0x4142434445464748494A
2 | XYZ | XYZ | 0x58595A
3 | NULL | | 0x20202020202020202020
The funny thing is that I already use this technique successfully in another table and I can't spot the difference:
CREATE TABLE SOME_OTHER_TABLE (
SOME_OTHER_TABLE_ID INT IDENTITY(1, 1) NOT NULL,
NOMBRE VARCHAR(50),
-- This works just fine:
NOMBRE_UNICO AS ISNULL(NOMBRE, CONVERT(VARCHAR, SPACE(50)) + CONVERT(VARCHAR, SOME_OTHER_TABLE_ID)),
CONSTRAINT SOME_OTHER_TABLE_PK PRIMARY KEY (SOME_OTHER_TABLE_ID)
);
What am I missing?
(1) This was a workaround for SQL Server 2005 that's no longer necessary in later versions thanks to filtered indexes.
There you go with " 3"
SELECT TEST_ID, CODE, UNIQUE_CODE, CAST(UNIQUE_CODE AS VARBINARY(4000)) AS HEX
FROM (
SELECT TEST_ID, CODE,
ISNULL(CODE, CONVERT(VARCHAR(20), SPACE(10)) + CONVERT(VARCHAR(20), TEST_ID)) AS UNIQUE_CODE
FROM (
SELECT 1 AS TEST_ID, cast('ABCDEFGHIJ' as varchar(20)) AS CODE
UNION ALL
SELECT 2, 'XYZ'
UNION ALL
SELECT 3, NULL
) TEST
) X;
'ABCDEFGHIJ' (first value in the union list) is exactly 10 characters and this column is a first argument of IsNull. So it takes 10 characters as size for IsNull result. Which is enough only for spaces. Replacing this constant with 'ABCDEFGHIJKLMNOPQR' would do the trick also.
It looks like SQL is trying to help out to define the column length in your inner query. By casting/converting to a specific size this fixes the problem. Once your UNIQUE_CODE field exceeds this value, the returned value is limited to the size of the column.
SELECT TEST_ID, CODE, UNIQUE_CODE, CAST(UNIQUE_CODE AS VARBINARY(4000)) AS HEX
FROM (
SELECT TEST_ID, CODE,
ISNULL(CODE, CONVERT(VARCHAR, SPACE(10)) + CONVERT(VARCHAR, TEST_ID)) AS UNIQUE_CODE
FROM (
SELECT 1 AS TEST_ID, CONVERT(VARCHAR(50), 'ABCDEFGHIJ') AS CODE
UNION ALL
SELECT 2, 'XYZ'
UNION ALL
SELECT 3, NULL
) TEST
) X;
You can run below piece of code to find out why ?
this fails :
declare #a char(20)
set #a=null
declare #b char(10)
set #b='aaaaaa'
select isnull(#a,convert(char(10),space(10)+#b))
This works:
declare #a char(20)
set #a=null
declare #b char(10)
set #b='aaaaaa'
select isnull(#a,convert(char(30),space(10)+#b))

How to convert the float datatype to string

Using sql server 2000
Table1
id value (float)
001 10.00
002
003
004 08.50
...
i want to check the value column, if is null then display 'NA'
Tried Query
Select id,
CASE WHEN value IS NULL then '-' else value end AS value
from table1
'
Select id,
isnull(value, '-') AS value
from table1
Both query showing error as "Error converting data type varchar to float."
How to solve this issue.
need query help
Select id,
isnull(convert(varchar(20),value), '-') AS value
from table1
Try
SELECT id, CAST(COALESCE(value, 'NA') as VARCHAR(25)) as [Value]
FROM tableName
use below query :
SELECT id, ISNULL( CAST(value as VARCHAR(25)), 'NA')) as [Value]
FROM tableName
select id,cast(isnull(value,'') as varchar(10)) from table