Showing posts with label extract. Show all posts
Showing posts with label extract. Show all posts

Friday, March 9, 2012

Don't want output file if there are no rows returned

I'm using a simple data flow to extract rows from a table (using a SQL query) and put them in a flat file. If the query returns no rows, I don't want a file to be created. Right now it creates a file with the headers (since I do want the headers if there is data).

Any one know how to do this?

Kevin

Kevin,

Unfortunately I don't think there is any way around this. The workaround is to check to see if any rows were inserted (put a ROWCOUNT component immediately prior to the flat file destination adapter). If NumberOfRows=0, delete the resultant file.

-Jamie

|||

I did get it to work as Jamie suggests. For others who are trying to do the same, I'll post my solution below.

* At the Control Flow level, create a Sequence Container

* Place a Data Flow task in the Container

* Create a variable (called RowCounter of type Int32)

* Within the Data Flow Task, I have four objects in this order: 1) a Data Flow Source (such as OLE DB), 2) a Transformation (such as Copy Column), 3) the Row Count Transformation, and 4) a Data Flow Destination (such as Flat File).

* The Row Count Transformation is set up with the variable RowCounter

* Now back at the Control Flow level, place a File System Task in the Sequence Container

* Connect the Precedence Constraint from the Data Flow to the File System Task

* Open the properties of the Precedence Constraint and set the Evaluation Operator to "Expression and Constraint", the Value to "Success", and the Expression to "RowCounter == 0"

* Finally set the properties of the File System Task as follows: Operation = "Delete File" and Source Connection to the name of the File Connection that you used in the Data Flow Task.

Wednesday, March 7, 2012

Don't understand the concept behinf RAND

I am trying to generate 15-digit creditcard value (varchar) for testing.
I thought I could simply use the RAND function and extract 15 characters
from the right of the decimal. However this doesn't work, I lose most of th
e
significant digits when I try to conver to a varchar.
So I thought I would generate 15 random numbers and concatenate them
together. However this doesn't work either. When I repeatedly run the
statement below, I get do not get any randomness at all, the same digit
repeats over and over again and will only occasionally change.
SELECT str(10*RAND( (DATEPART(mm, GETDATE()) * 100000 )
+ (DATEPART(ss, GETDATE()) * 1000 )
+ DATEPART(ms, GETDATE()) ))
What don't I understand? Can anyone help me out with this?SELECT RIGHT(RTRIM(CONVERT(DECIMAL(18,17),
10.0*RAND( (DATEPART(mm, GETDATE()) * 100000 )
+ (DATEPART(ss, GETDATE()) * 1000 )
+ DATEPART(ms, GETDATE()) ))),15)
"Dave" <Dave@.discussions.microsoft.com> wrote in message
news:1E08F862-FFFF-4DD0-8EFA-4F714DBD125F@.microsoft.com...
>I am trying to generate 15-digit creditcard value (varchar) for testing.
> I thought I could simply use the RAND function and extract 15 characters
> from the right of the decimal. However this doesn't work, I lose most of
> the
> significant digits when I try to conver to a varchar.
> So I thought I would generate 15 random numbers and concatenate them
> together. However this doesn't work either. When I repeatedly run the
> statement below, I get do not get any randomness at all, the same digit
> repeats over and over again and will only occasionally change.
> SELECT str(10*RAND( (DATEPART(mm, GETDATE()) * 100000 )
> + (DATEPART(ss, GETDATE()) * 1000 )
> + DATEPART(ms, GETDATE()) ))
> What don't I understand? Can anyone help me out with this?
>|||I thought that most credit card numbers were a fixed length of 16
digits, in grouping of 4 digits, with the first grouping being the
issuer and the rest of the number following some rules.
That means that your random numbers are going to fail (well, you might
hit a valid card number by chance every few billion rows). Look at
these websites for some help:
[url]http://www.omnipilot.com/Tip%20of%20the%20W.1768.8848.lasso[/url]
http://www.analysisandsolutions.com...e/ccvs/ccvs.htm|||Dave,
I find CHEKSUM(NEWID()) a good seed for the RAND() function.
Also, keep in mind that RAND() returns a float in the range 0 through 1
inclusive.
With the above in mind, I'd use something like:
select right(cast(rand(checksum(newid())) as decimal(15, 15)), 15)
BG, SQL Server MVP
www.SolidQualityLearning.com
"Dave" <Dave@.discussions.microsoft.com> wrote in message
news:1E08F862-FFFF-4DD0-8EFA-4F714DBD125F@.microsoft.com...
>I am trying to generate 15-digit creditcard value (varchar) for testing.
> I thought I could simply use the RAND function and extract 15 characters
> from the right of the decimal. However this doesn't work, I lose most of
> the
> significant digits when I try to conver to a varchar.
> So I thought I would generate 15 random numbers and concatenate them
> together. However this doesn't work either. When I repeatedly run the
> statement below, I get do not get any randomness at all, the same digit
> repeats over and over again and will only occasionally change.
> SELECT str(10*RAND( (DATEPART(mm, GETDATE()) * 100000 )
> + (DATEPART(ss, GETDATE()) * 1000 )
> + DATEPART(ms, GETDATE()) ))
> What don't I understand? Can anyone help me out with this?
>|||>I thought that most credit card numbers were a fixed length of 16
> digits, in grouping of 4 digits
American Express is 15.|||--CELKO-- wrote:
> I thought that most credit card numbers were a fixed length of 16
> digits, in grouping of 4 digits, with the first grouping being the
> issuer and the rest of the number following some rules.
> That means that your random numbers are going to fail (well, you might
> hit a valid card number by chance every few billion rows). Look at
> these websites for some help:
> [url]http://www.omnipilot.com/Tip%20of%20the%20W.1768.8848.lasso[/url]
> http://www.analysisandsolutions.com...e/ccvs/ccvs.htm
Well, he might generate 15 digits and then calculate the last digit
using luhn, so it should at least pass that check.
On a curiosity side-not - anyone ever implemented luhn as a check
constraint? I'd love to see the beast.
Damien

Friday, February 24, 2012

Domain name from URL

Does anyone know how to extract just the domain from a URL in T-SQL? So,
for example, http://www.awebsite.com/pages/thispage.html" would come out as
http://www.awebsite.com, or just www.awebsite.com.
Many thanks for any help.SELECT
substring(REPLACE('http://www.awebsite.com/pages/thispage.html','http://',''
),0,CHARINDEX('/',REPLACE('http://www.awebsite.com/pages/thispage.html','htt
p://','')))
HTH. Ryan
"Chris Pratt" <not@.given.com> wrote in message
news:etv05ZBIGHA.1424@.TK2MSFTNGP12.phx.gbl...
> Does anyone know how to extract just the domain from a URL in T-SQL? So,
> for example, http://www.awebsite.com/pages/thispage.html" would come out
> as http://www.awebsite.com, or just www.awebsite.com.
> Many thanks for any help.
>|||Something like this?
declare @.string varchar(1024)
declare @.UriScheme varchar(16)
set @.string = 'http://www.awebsite.com/pages/thispage.html'
select @.UriScheme = substring(@.string, 0, patindex('%://%', @.string))
set @.string = substring(@.string, patindex('%://%', @.string) + 3, len(@.string
))
select @.UriScheme + '://' + substring(@.string, 0, charindex('/', @.string))
ML
http://milambda.blogspot.com/|||Chris
DECLARE @.fullurl VARCHAR(1000)
SET @.fullurl = 'http://www.cnn.com/articles/sports/show.asp?id=4'
SELECT SUBSTRING(@.fullurl, CHARINDEX('//', @.fullurl)+2,
CHARINDEX('/', SUBSTRING( @.fullurl,
CHARINDEX('//', @.fullurl)+2, LEN(@.fullurl)))-1 )
"Chris Pratt" <not@.given.com> wrote in message
news:etv05ZBIGHA.1424@.TK2MSFTNGP12.phx.gbl...
> Does anyone know how to extract just the domain from a URL in T-SQL? So,
> for example, http://www.awebsite.com/pages/thispage.html" would come out
> as http://www.awebsite.com, or just www.awebsite.com.
> Many thanks for any help.
>|||That worked brilliantly, thanks.
"Ryan" <Ryan_Waight@.nospam.hotmail.com> wrote in message
news:eeh19dBIGHA.2928@.TK2MSFTNGP10.phx.gbl...
> SELECT
> substring(REPLACE('http://www.awebsite.com/pages/thispage.html','http://',
''),0,CHARINDEX('/',REPLACE('http://www.awebsite.com/pages/thispage.html','h
ttp://','')))
> --
> HTH. Ryan
> "Chris Pratt" <not@.given.com> wrote in message
> news:etv05ZBIGHA.1424@.TK2MSFTNGP12.phx.gbl...
>|||That works great (see above posting!), except for two possible scenarios.
The first is where the URL is actually just the domain name anyway - for
example http://www.awebsite.com. You can get round this by forcing a
trailing '/' to the URL string your are testing, so that one is ok.
The other is if the site begins "https" instead of "http", in which case
just "https:" is returned. Would it be possible to cater for this as well?
Many thanks again,
Chris
"Ryan" <Ryan_Waight@.nospam.hotmail.com> wrote in message
news:eeh19dBIGHA.2928@.TK2MSFTNGP10.phx.gbl...
> SELECT
> substring(REPLACE('http://www.awebsite.com/pages/thispage.html','http://',
''),0,CHARINDEX('/',REPLACE('http://www.awebsite.com/pages/thispage.html','h
ttp://','')))
> --
> HTH. Ryan
> "Chris Pratt" <not@.given.com> wrote in message
> news:etv05ZBIGHA.1424@.TK2MSFTNGP12.phx.gbl...
>