SQL Server 2008 :: Extracting Numbers From String

Jun 2, 2015

Anyny in-built sql function that gives us numeric values in a string?

I have to deal with some inconsistent US phone numbers stored in DB. They are stored as

(xxx)xxx-xxxx
xxx xxxx xxxx
(xxx) xxx-xxxx
xxx-xxx-xxxx
xxxxxxxxxx

I don't want to apply nested REPLACE function to eliminate all unnecessary characters to get 10 digit number from DB.

View 9 Replies


ADVERTISEMENT

SQL Server 2012 :: Extracting String Between Certain Characters

Aug 18, 2014

I need extracting string that is between certain characters that are in certain position.

Here is the DDL:

DROP TABLE [dbo].[StoreNumberTest]
CREATE TABLE [dbo].[StoreNumberTest](
[StoreNumber] [varchar](50) NULL,
[StoreNumberParsed] [varchar](50) NULL)
INSERT INTO [dbo].[StoreNumberTest]

[Code] ....

What I need to accomplish is to extract the string that is between the third and fifth '-' (dash) and insert it into the StoreNumberParsed while eliminating the fourth dash.

Sample output would be:

KY117
CA132
OH174
MD163
FL191

I know that parse, charindex, patindex all might come in play, but not sure how to construct the statement.

View 5 Replies View Related

Extracting A Portion Of A String In SQL Server 2005

Apr 22, 2008

Hello, this seems simple but I've been banging my head a while.

I have a data field that is formated like this: NYT/2000-Subways/7510-Electronics Mtc/7540-Data Svcs.

I need to pull out the string after the second / and before the third / in this case the value is 7510-Electronics Mtc but it does change.

I have this function that returns the first value (NYT):

USE [Data_Warehouse]
GO
/****** Object: UserDefinedFunction [dbo].[Get_Dept] Script Date: 04/22/2008 09:22:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- Provide the phrase before (ex. 'Worklog Related to'), the field searched (ex. UPDATE_ACTION), and
-- the phrase after (ex. ']').
-- This function returns the string found after any phrase and before any phrase, within any field searched.

ALTER FUNCTION [dbo].[Get_Dept](@in_Phrase_Before varchar(250),@in_Search_Field varchar(250))
RETURNS varchar(250)
AS
BEGIN
Declare @str_String As varchar(250)
Declare @tmp_String as varchar(250)
Set @str_String = null -- to clear the variable buffer

Set @in_Phrase_Before=char(37)+(LTrim(Rtrim(@in_Phrase_Before)))+char(37) -- if 'Null', all strings will be returned

If Left(@in_Search_Field,4) = 'NYT/'
BEGIN
Set @tmp_String = Right(@in_Search_Field,(len(@in_Search_Field) - 4 ) )
Set @in_Search_Field = @tmp_String
END

If (PatIndex(@in_Phrase_Before, @in_Search_Field)) > 0 --Checks to see if the phrase before is in the search field
Set @str_String=SubString(@in_Search_Field,1, (PatIndex(@in_Phrase_Before, @in_Search_Field)-1) )

RETURN @str_String

END

Thanks in advance for the help

View 10 Replies View Related

SQL Server 2008 :: Extracting Data From Linked Server

Dec 17, 2010

I am using MS SQL 2008, and I am trying to extract data from a MySQL database. I am having trouble extracting the data I need.

In SQL Server management studio, I can see the linked server...I can browse the different databases on the server. I can see user and system tables in all of the databases.

When I try and query a linked table (select * from server.db.table) I get Invalid object name 'servername.databasename.tablename'.

When I try script the table.. right_click on the linked table, Script table as, Select to, New Query Editor window, I get an error '[servername].[databasename]..[tablename] contains no columns that can be selected or the current user does not have permissions on that object.'

This leads me to believe that it is a permissions problem, but if I have access to the MySQL database using MySQL and the same login/password) and can retrieve the data there, then I think my login credentials should be enough using MS SQL. I guess I think its odd that I could have enough credentials to get in and see table names, but not do a select against it.

View 8 Replies View Related

SQL Server 2008 :: Getting Row Numbers For Each People Group?

Feb 18, 2015

I have a set of rows in a table like for example

Client ID Client Name Date Score
1 Smith 12/31/2014 25
1 Smith 10/15/2014 45
2 John 08/11/2014 55
2 John 06/18/2014 15
3 Rose 04/15/2014 12
4 Mike 07/23/2014 28
5 Mary 01/5/2014 56
6 Lisa 08/1/2014 54
6 Lisa 05/10/2014 34

Now I want to use Row Number function or any way where I can get the result as below

Client ID Client Name Date Score RowNo
1 Smith 12/31/2014 25 1
1 Smith 10/15/2014 45 2
2 John 08/11/2014 55 1
2 John 06/18/2014 15 2
3 Rose 04/15/2014 12 1
4 Mike 07/23/2014 28 1
5 Mary 01/5/2014 56 1
6 Lisa 08/1/2014 54 1
6 Lisa 05/10/2014 34 2

View 3 Replies View Related

SQL Server 2008 :: CAST (INT AS DATETIME) - Random Numbers

May 20, 2015

While trying to solve a SQL challenge I found myself trying to understand what is happening when you CAST a INT to date time.

Trying to understand the results. Here are some random numbers and Castings. My question is why do they produce the datetimes they do?

SELECT CAST((1.1) AS DATETIME)
SELECT CAST((200) AS DATETIME)
SELECT CAST((15) AS DATETIME)
SELECT CAST((99.99999) AS DATETIME)

View 9 Replies View Related

SQL Server 2008 :: Small Recursive Numbers Table

Sep 20, 2015

I am trying to do a very small numbers table to compare A1c's against. However I am running into a issue when recursion hits the number 2.27 it starts to go out of my scope that I want with the next number being 2.27999999999999. Here is the code I'm using below. I need a Decimal(2,2) or Numeric (2,2) format with a range of 01.00 to 20.00. However every time I use Numeric or Decimal as the data type I get a error "Msg 240, Level 16, State 1, Line 5.Types don't match between the anchor and the recursive part in column "Number" of recursive query "NumberSequence"."

DECLARE @Start FLOAT , @End FLOAT ---DECIMAL(2,2) Numeric (2,2)
SELECT @Start=01.00, @End=20.00
;WITH NumberSequence( Number ) AS
(
SELECT @start as Number
UNION ALL
SELECT Number + 00.01
FROM NumberSequence
WHERE Number < @end

View 5 Replies View Related

SQL Server 2008 :: Logic To Split Monthly Numbers

Sep 22, 2015

I am trying to split the annual cost into monthly numbers based on the contract Period.Since the contract period varies from company to company not sure how to implement the logic.

create table #Invoice
(
Company Varchar(50),
Startdate2015 DateTime,
EndDate2015 DateTime,
ContractPeriod2015 Int,
ContractAmount2015 Float,

[code]..

View 3 Replies View Related

SQL Server 2008 :: Assign Consecutive Numbers To A Block Of Data

Mar 17, 2015

I want to assign consecutive numbers to a block of data where block of data is based on days consecutive to each other i.e., one day apart.

Date format is: YYYY-MM-DD

Data:

TestId TestDate
----------- -----------------------
1 2011-07-21 00:00:00.000
1 2011-07-22 00:00:00.000
1 2011-07-27 00:00:00.000
1 2011-07-29 00:00:00.000
1 2011-07-30 00:00:00.000
1 2011-07-31 00:00:00.000

[Code] ....

My Attempt:

WITH cte AS
(
SELECTTestId,
TestDate,
ROW_NUMBER() OVER
(
PARTITION BYTestId

[Code] .....

Expected Output:

TestId TestDate OrderId
----------- ----------------------- --------------------
1 2011-07-21 00:00:00.000 1
1 2011-07-22 00:00:00.000 1
1 2011-07-27 00:00:00.000 2
1 2011-07-29 00:00:00.000 3
1 2011-07-30 00:00:00.000 3

[Code] ....

The OrderId is the column I am trying to obtain using my following cte code, but I can't work around it.

View 7 Replies View Related

SQL Server 2008 :: Search Each And Every String In Comma Delimited String Input (AND Condition)

Mar 10, 2015

I have a scenario where in I need to use a comma delimited string as input. And search the tables with each and every string in the comma delimited string.

Example:
DECLARE @StrInput NVARCHAR(2000) = '.NET,Java, Python'

SELECT * FROM TABLE WHERE titleName = '.NET' AND titleName='java' AND titleName = 'Python'

As shown in the example above I need to take the comma delimited string as input and search each individual string like in the select statement.

View 3 Replies View Related

SQL Server 2012 :: Find Numbers From A String

Apr 8, 2014

I have a string and i want to get only the numbers from right.

For example if I have the string Like '123756zxfggr123456' then it will show me only 123456 or if i have the string like
'4vbz67xfggr123dfd' then it will show me only 123 or if i have the string like '123756zxfgg43r5' then it will show me only 5.

I got a function where it gives me all the numbers in a string but I don't need that

CREATE FUNCTION dbo.udf_GetNumeric
(@strAlphaNumeric VARCHAR(256))
RETURNS VARCHAR(256)
AS
BEGIN
DECLARE @intAlpha INT

[Code] ....

If I ran the select statement it gives me the result 111123456 but i want only 123456 or if i select

SELECT dbo.udf_GetNumeric('111zxfggr6587fhhfkwee') AS 'Num' it will show me 6587.

View 8 Replies View Related

Extracting Part Of A String

Aug 22, 2006

Hello,

I have a field in my table that includes free form text. Within this text are two five digit numbers seperated by a forward slash (/). I need to be able to locate the "/" and then return and display the numbers before and after the "/" seperately. For example:

"the text looks like this but has two numbers 55555/66666 within the string"

I need to get the "55555" and the "66666" in oprder to then display them. can anyone help? I am using ASP/SQL. Appreciated in advance!

View 5 Replies View Related

Transact SQL :: Extracting A Word From String?

Jun 30, 2015

Is there a function / way to extract a word from a particular position in the String.

I want to extract a word which is in the 16th position. All words are separated by spaces.

View 6 Replies View Related

SQL Server 2008 :: String As Primary Key

Feb 26, 2015

I have tables in database where a VARCHAR(50) string is unique identifier. The database currently has an integer identity column as clustered primary key, and there's a non-clustered index on string column. The customer always queries based on a defined set of the identifier (string) column.

I wonder if someone sees an advantage of adding a persisted computed column to the table as the checksum of the string column, and then create a non-clustered index on the checksum and the string. When a customer requests data, we would compute the checksum of the customer provided identifier and add to the where clause or join, that the checksum and string must match.

Will SQL Server perform checksum check (integer) and only if it succeeds, perform the string check, in which case I see an advantage of added the checksum column? Or will SQL Server always check for both the checksum and string, in which case the additional column only adds unnecessary overhead? To note is the fact that the table(s) will have millions of rows, but the customer will request data for at at most, 100 or so identifiers.

View 9 Replies View Related

SQL Server 2008 :: How To Compare String

Jun 19, 2015

I need to compare to string in sql

requirement is

Select Reson_id from reason_data where reason='ryete / werewr ddsad '

is there any way without string function? bcz string function take more exaction time

View 9 Replies View Related

SQL Server 2008 :: Search For A Percentage Value In String Using Contains?

May 6, 2015

Interest rate has been stored in comments column along with other information ( e.g. mike's student loan is 5% and car payment is $ 150). I need to extract 5% using Contains .. Why Contains? because it's a 1.7 m rows dataset and searching for fours specific interest rate values (e.g. 3%, 9%, 12% and 15%)

View 3 Replies View Related

SQL Server 2008 :: How To Extract Part Of A String

May 11, 2015

Here is a sample order # we used for one of our shipments: BL-53151-24954-1-0001-33934

I need to extract the "24954" portion of that order # while within an INNER JOIN, but not sure how.

My problem is we have 2 order tables: OrderTable1 contains a field with the full order #. OrderTable2 contains a field with only the "24954" portion. I need to JOIN on these 2 fields somehow.

So an example would be the following:

OrderTable1.Full_Order_No: BL-53151-24954-1-0001-33934
OrderTable2.Order_No: 24954

SELECT
ot1.Full_Order_No
, ot2.Order_No
FROM
OrderTable1 ot1
INNER JOIN OrderTable2 ot2 ON ot2.Order_No = [do something here to truncate ot1.Full_Order_No]

How can I do this?

Few notes:

-the 1st part of the order number, "BL-53151-" will ALWAYS be the same. It's our client # which will never change for the purpose of this query.
-The portion I need (24954) can be more or less than the 5 current digits.
-There will always be 6 portions to the order number, split up between 5 dashes.

View 2 Replies View Related

SQL Server 2008 :: Using String Functions On Text

May 26, 2015

I've below value in a column with data type - TEXT

QU 221025U2V/AN G-DT DL A 5 1A- 11,5,SF,230,30162,LZ,2,118,0,0,10170,25,06

This text value has some special characters in it. and I could not paste the exact value as this text box is not allowing me to do so. So, for reference I've attached a screenshot (Capture.png) of the value.

I want to fetch last two values from this text i.e. 25 and 06. (It can be anything like 56R,06T but will be the last two values separated by comma)...

View 5 Replies View Related

SQL Server 2008 :: Get Date Format From String?

May 28, 2015

How can we identify the Date Format from a String in SQL Server.

I might get an input from external source as "MM-DD-YYYY" or "DD-MM-YYYY" or "YYYY-MM-DD" or "YYYY-DD-MM", all i have to do is return the same with Current Date in the same format as Input.

Ex :

1. "02-20-2013" -> "05-28-2015"
2. "2014-04-19" -> "2015-05-28"

Any method to identify the DateFormat then it would have made the job very easy.

View 2 Replies View Related

SQL Server 2008 :: Search Any Of Two Names From String

Jun 20, 2015

DECLARE @search VARCHAR(10);
SET @search = 'dog,COW';
WITH T(C)
AS
(
SELECT 'Cat,Dog,Sparrow,Trout,Cow,Seahorse'
UNION ALL
SELECT 'Cat,Dog,Sparrow,Trout,Cow,Seahorse'
)
SELECT *
FROM T
WHERE C LIKE '%' + @search + '%'

I have a string in a cell 'Cat,Dog,Sparrow,Trout,Cow,Seahorse'

I want to search any of the two names from the string.

Search string:'cat,cow'
Result:no result (not working),[size="7"][/size]
Search string:'cat,dog'
result :given string(working fine )

View 2 Replies View Related

SQL Server 2008 :: Get Numeric Values From String

Jul 29, 2015

Why can i get the numeric values from this string?

{_cpn}=1743; {_cpnll}=4511

Result: 1743, 4511

Position and len of the value can be different...

View 6 Replies View Related

Using String List Of Numbers With IN

Mar 20, 2014

A table I'm working with has a varchar column containing a comma-delimited string of numbers, which match up to smallint codes in another table. I gather this is someone's implementation of a many-to-many relationship. :)

Using SQL Server 2008 and wondering if there's a special trick to using a string list of numbers with IN() URL...converting the string into a temp table[/url] but I'd just like to make sure there isn't a quicker, easier approach, or if that's it.

View 3 Replies View Related

Spilting A String Of Numbers

Mar 1, 2007

Hi,

Say I have a string of Numbers eg. "1,2,3,4,5" and in order to split it I use split("1,2,3,4,5", ",") function, what would I get as an answer? My second question is, if I want to pass all 5 numbers in a sub report as if I am selecting from a multi-value parameter, how would I do that? I have tried several different ways of doing it and I still can't get it to work. Please help me...and feel free to email me back if somethign is not clear.

Thanks a lot,

-Rohit

View 5 Replies View Related

Stripping Out Numbers From A String

Oct 25, 2006

Hi

I have a field that starts with numbers and then has a description after if, eg.
"123 This is the description for string 123"

1. How would I go about stripping out the first instance of number to leave the string as "This is the description for string 123"

thanks.

Bill

View 9 Replies View Related

SQL Server 2008 :: Compare Up To 9 String Variables For Similarity?

Mar 6, 2015

We're converting to new student info system. Sometimes registrar entered the same school into the schools table but spelled it differently. Trying to find all student assigned transfer credits from the same school but the school name is different. My db shows a max of 9 different schools students have rec'd transfer credits. Spending too much time trying to figure out best way to do it w/o a ton of IF stmts. Looking at Soundex and Difference functions. Still looks like a lot of coding. how to compare up to 9 string variables in sqlserver 2008?

View 2 Replies View Related

SQL Server 2008 :: Substitute Line Breaks For A String

Aug 17, 2015

Is it possible to change any line breaks or newlines for a string ...

In a select statement in a store procedure, in this way I can reproduce text in a label on a webpage complete with line breaks ....

View 4 Replies View Related

Changing Date To String Of Numbers

May 27, 2006

I basic question but can someone help.

I have a SELECT statement, the result of which populates a
datagrid.  The first column has consecutive dates in it and I want
to hyperlink each date to a seperate Javascript function (the
Javascript is created on the fly and is unique for each date).  I
need a different function name for each function and so tried the date
but "/" is not allowed in the Javasript function name.  I think
the easiest way will be to produce a new column with the date expresses
ddmmyyyy, ddmmyy or some such unique number (but not dd/mm/yyyy). 
I tried :-

    "CASE " & _
    "WHEN t3.date = t3.date THEN (DAY(t3.Date) + MONTH(t3.Date) + YEAR(t3.Date)) ELSE NULL END AS [javaKey]

but this adds the year to the month to the day - not a unique result as 1/2/06 and 2/1/06 are the same.

I am just getting to grips with VB.Net (as an amature) but am a distinct beginner at SQL!

Many thanks

Mike

View 2 Replies View Related

Splitting Alphabets And Numbers In A String

Mar 2, 2012

I have a column in a table which looks like below.

Column
-------
AA123
D123
AXC1
QF23

I need to split this value into two part, Alphabets and numeric. How to do this using SQL query.My column value will not have mixed characters like A1D3,G32S,12F.It will always follow the ablve pattern mentioned above.

View 3 Replies View Related

How Do I Calculate The Sum Of Each Number In A String Of Numbers?

Jul 20, 2005

I've got a string of numbers, say 123456 (the actually number is 12 digitslong). I need to calculate the sum of each individual number in the stringof numbers, so in the example of 123456 the sum would be 21 (1+2+3+4+5+6).Thanks

View 5 Replies View Related

SQL Server 2008 :: Remove Double Quote In String Fields

Feb 23, 2015

I have a SQL select syntax as below

0 AS SalaryMin,
2088 AS SalaryMax,
2088 AS BillableHours,
'Month' AS SalaryPaidCode,
0 AS SalaryBreakdownHourly,
0 AS SalaryBreakdownDaily,

[Code] ...

While outputting to CSV.file

I got :0,2088,2088,"Month",0,0,0,0,0,0,0,"N/A","N/A","G","N/A","Exempt","Other",1

How can I remove all double quotes in the string fields? so that O can get the result as below while the output
0,2088,2088,Month,0,0,0,0,0,0,0,N/A,N/A,G,N/A,Exempt,Other,1

View 7 Replies View Related

SQL Server 2008 :: Finding Multiple Character Occurrence In String

Mar 24, 2015

I am trying to figure out how I can find the names of people who have a double occurrence or more of characters either in their first name or last name.

This is for a data cleanup.

CREATE TABLE #Names ( FIRSTNAME VARCHAR (20), LASTNAME VARCHAR (20))
INSERT INTO #Names VALUES
('Thomas', 'Hohner')
,('Mike', 'SSmith')
,('TtTony', 'Hawk')
,('Jeff', 'Smith')
,('ZZSara', 'Jones')
,('Luke', 'HendersonZZ')
,('Lily', 'PPadZZZ')

SELECT * FROM #Names
DROP TABLE #Names

View 9 Replies View Related

SQL Server 2008 :: Stored Procedure Text String Replace

Apr 16, 2015

We have a legacy database that have hundreds of stored procedures.

The previous programmar uses a string like servername.databasename.dbo.tablename in the stored procedures.
We now have migrated the database to a new server. The old server is either needed to be replaced by the new server name, or remove it.

I don't know why he used servername as part of the fully qualified name, we don't use linked servers. So I think better removing the servername in all the stored procedures.

I know I can do a generate script, and replace the text and then use alter procedure to recreate all the stored procedures. But since hundreds of them, is there a programmatically way to replace them?

View 2 Replies View Related

SQL Server 2008 :: Split Comma Separated String Into Columns?

Apr 24, 2015

Our front end saves all IP addresses used by a customer as a comma separated string, we need to analyse these to check for blocked IPs which are all stored in another table.

A LIKE statement comparing each string with the 100 or so excluded IPs will be very expensive so I'm thinking it would be less so to split out the comma separated values into tables.

The problem we have is that we never know how many IPs could be stored against a customer, so I'm guessing a function would be the way forward but this is the point I get stuck.

I can remove the 1st IP address into a new column and produce the new list ready for the next removal, also as part of this we would need to create new columns on the fly depending on how many IPs are in the column.

This needs to be repeated for each row

SELECT IP_List
, LEFT(IP_List, CHARINDEX(',', IP_List) - 1) AS IP_1
, REPLACE(IP_List, LEFT(IP_List, CHARINDEX(',', IP_List) +0), '') AS NewIPList1
FROM IpExclusionTest

Results:

IP_List
109.224.216.4,146.90.13.69,146.90.85.79,46.208.122.50,80.189.100.119
IP_1
109.224.216.4
NewIPList1
146.90.13.69,146.90.85.79,46.208.122.50,80.189.100.119

View 8 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved