Retrieve Individual character from varchar field in SQL Server - sql

How can I retrieve individual characters of field values (e.g A - C - D from value ACD) and manipulate in SQL Server if I have a column of type varchar(4) with following variable length values.
Column
---
ACD
BC
CD
Thanks

select case when CHARINDEX('A', columnName, 1) > 0 then 1 else 0 end has_a,
case when CHARINDEX('B', columnName, 1) > 0 then 1 else 0 end has_b,
case when CHARINDEX('C', columnName, 1) > 0 then 1 else 0 end has_c,
case when CHARINDEX('D', columnName, 1) > 0 then 1 else 0 end has_d
from tableName

You may use SUBSTRING:
SELECT SUBSTRING('ABC',1,1)
The first param is the input string, the second one is the start index 1-based, and the third is the length of the result.
So if you have a column Col1 in a table Table1:
SELECT SUBSTRING(Col1,1,1),
SUBSTRING(Col1,2,1),
SUBSTRING(Col1,3,1),
SUBSTRING(Col1,4,1)
FROM Table1
You will get an empty string if for instance you have three characters and you try to get the fourth.
Fiddle Example Here

Related

BigQuery - No mathcing signature for operator CASE in conjunction with CAST to INT64

SELECT
T.myString, #A string that contains a number
CASE CAST(T.myString as INT64)
WHEN CAST(T.myString as INT64) > 0 THEN 1
ELSE 0
END AS test
FROM
`main.tables.test` T
This shows the error:
" No mathcing signature for operator CASE for argument types INT64, BOOL ... "
What am I doing wrong here?
below is the "fix"
SELECT
T.myString, #A string that contains a number
CASE
WHEN CAST(T.myString as INT64) > 0 THEN 1
ELSE 0
END AS test
FROM
`main.tables.test` T
or
SELECT
T.myString, #A string that contains a number
CASE CAST(T.myString as INT64) > 0
WHEN true THEN 1
ELSE 0
END AS test
FROM
`main.tables.test` T
Try bare CASE:
SELECT
T.myString, #A string that contains a number
CASE
WHEN CAST(T.myString as INT64) > 0 THEN 1
ELSE 0
END AS test
FROM
`main.tables.test` T

SQL - Condition to flag more than 1 column is not null

My table has 10 columns. Col1, Col2...Col10
Only one of them should have a numeric value > 0 in it. If more than 1 has a value > 0, then I want to create an error. How can I setup a condition to do that?
Where Col1 is > 0 and Col2 = 0, Col3 = 0, Col4 = 0...
Because there are too many columns, the manual way of writing every scenario is too much code.
What is a more efficient way to do this?
A check constraint to ensure not more than one column has a value greater than zero could look like this:
alter table mytable add constraint check_my_columns
check
(
case when col1 > 0 then 1 else 0 end +
case when col2 > 0 then 1 else 0 end +
case when col3 > 0 then 1 else 0 end +
case when col4 > 0 then 1 else 0 end +
...
<= 1
);
Considering the Cols are type Bit (can hold 0 or 1)
You can sum all the columns and if you get a value > 1 then show the error message
Ex:
if (isnull(col1,0) + isnull(col2,0) + isnull(col3,0) + ... + isnull(col10,0) > 1)
begin
Show error message
Return;
end
You can use apply:
select t.*,
(case when s.col > 2 then 'ERROR' else 'OKAY' end) as status
from t cross apply
(select count(*) as cnt
from (values (t.col1), (t.col2), . . . ) v(col)
where col > 0
) s
You can adjust the where clause to count non-NULL values or whatever.

Prefix 0 in a Phone number string SQL

I have a phone number in sql table, i want to add prefix '0' where ever phone number is 10 digit,
but if it is less than 10 or greater than 10 digit then no 0 prefix required.
7863176061
7724269820
2088076157
1992762084
1318912
output
07863176061
07724269820
02088076157
01992762084
1318912
Try this
SELECT CASE LEN(Num) WHEN 10 THEN '0'+cast(Num as varchar(11)) ELSE Num END AS Num
Try this:
select
case when len(yourcolumn) =10
then '0'+ yourcolumn
else yourcolumn end as column
from yourtable
Use CASE expression to check the length.
Query
select
case when len([phone_number]) = 10
then '0' + cast([phone_number] as varchar(20))
else cast([phone_number] as varchar(20)) end
from [your_table_name];
Find demo here
You have to cast the phone number column to varchar if the column datatype is in bigint. Otherwise you can exclude the cast part in the above query.
i think this will be fast,
select
'0' + cast([phone_number] as varchar(20))
from [your_table_name]
where len([phone_number]) = 10 ;

Convert INT column values to an empty string using ISNULL

I need to convert column ID of INT data type to a empty string ['']. I should not modify the source column data type, but need to convert it in my transformation in the other table. The ID column is "nullable" as it has null in it.This is my code.
CREATE TABLE #V(ID INT) ;
INSERT INTO #V
VALUES (1),(2),(3),(4),(5),(NULL),(NULL) ;
SELECT CASE WHEN CAST(ISNULL(ID,'') AS VARCHAR(10)) = '' THEN '' ELSE ID END AS ID_Column
FROM #V;
this returns:
ID_Column
1
2
3
4
5
NULL
NULL
when I modify my CASE statement it as follows:
CASE WHEN CAST(ISNULL(ID,'') AS VARCHAR(10)) = '' THEN '' ELSE ID END AS ID_Column
it returns:
ID_Column
1
2
3
4
5
0
0
Is this what you want?
select coalesce(cast(id as varchar(255)), '')
from #v;
You have to turn the entire result column into a single column. If you want a blank value, then the type is some sort of character string.
In your examples, the else id means that the result from the case is an integer, which is why you are getting either 0 or NULL.

Number of numeric values present in NVarchar column in SQL Server 2005

I have a Nvarchar column which contains data related to different data types such as Numerics, Dates and so on.
I wanted to find out number of cells with Numeric data type and number of cells with Date data type.
Thanks
It looks like you just want IsDate() and IsNumeric():
SELECT Sum(CASE WHEN IsDate(DataColumn) = 1 THEN 1 ELSE 0 END) AS Dates,
Sum(CASE WHEN IsNumeric(DataColumn) = 1 AND IsDate(DataColumn) = 0 THEN 1 ELSE 0 END) AS Numbers
FROM YourTable
This is assuming that you have a single date format, and that that format is supported by SQL Server's dateformat option. To define the correct format, you would use SET DateFormat. See MSDN Documentation on IsDate.
You mention that '20041201' could be a valid date, which is why I added the extra IsDate() check in the numeric count (I assume you wouldn't want to count it as both a date and a number).
First at all you should split you column (create split function which will transform delimited string into column) . You can find a lot of examples in Internet. I have got one here . So here is an example:
declare #text nvarchar(max)='1211221,ssssss,1212,2010-02-01,20100201'
DECLARE #delimiter char(1)=','
;WITH Pieces(pn, start, stop) AS (
SELECT CASt(1 as Int), CAST(1 as integer), CHARINDEX(#delimiter,#text)
UNION ALL
SELECT pn + 1, CAST(stop + 1 as integer), CHARINDEX(#delimiter,#text, stop + 1)
FROM Pieces
WHERE stop > 0
), T as(
SELECT pn,
SUBSTRING(#text, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS s
FROM Pieces)
SELECT
SUM(CASE WHEN ISNUMERIC(T.s)=1 AND ISDATE(T.s)=0 THEN 1 ELSE 0 END) NumericCount,
SUM(CASE WHEN ISDATE(T.s)=0 THEN 0 ELSE 1 END)DateCount
FROM T
You will have 2 dates and 2 numeric counts