Showing posts with label fields. Show all posts
Showing posts with label fields. Show all posts

Tuesday, March 27, 2012

embedded control characters

Beyond my control: I am finding control characters (likely tab) is
making its way into address fields of our operational system. This is
messing me up when I load the data into our warehouse w/ BCP (fields
get shifted).
Is the a nifty way to strip control characters from data?
TIA
Robrcamarda (rcamarda@.cablespeed.com) writes:
> Beyond my control: I am finding control characters (likely tab) is
> making its way into address fields of our operational system. This is
> messing me up when I load the data into our warehouse w/ BCP (fields
> get shifted).
> Is the a nifty way to strip control characters from data?

UPDATE tbl
SET col = replace(col, char(9), ' ')
WHERE col LIKE '%' + char(9) + '%'

You could have to nest replace, if there are more characters you want
to kill.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Embed HTML tags into report fields

I have a need to render data returned from a relational DB with spacing intact. The application is a viewer for input data so the end user can determine if the input data is different than the file layout specified so the data will be parsed correctly, so spacing is critical.

I changed to a fixed-pitch font to ensure that the text records align properly in VS (which they do), but when I render to HTML, the spaces collapse (of course). PDFs render correctly, but we were trying for HTML output.

Is there any way to replace the native spaces with   so it will render correctly in HTML? From the other posts, it seems not, but I want to double check.

If anyone knows of a way to do this, I would appreciate any insight.

Thx.See http://blogs.msdn.com/bimusings/archive/2005/12/14/503648.aspx.sql

Embed HTML tags into report fields

I have a need to render data returned from a relational DB with spacing intact. The application is a viewer for input data so the end user can determine if the input data is different than the file layout specified so the data will be parsed correctly, so spacing is critical.

I changed to a fixed-pitch font to ensure that the text records align properly in VS (which they do), but when I render to HTML, the spaces collapse (of course). PDFs render correctly, but we were trying for HTML output.

Is there any way to replace the native spaces with   so it will render correctly in HTML? From the other posts, it seems not, but I want to double check.

If anyone knows of a way to do this, I would appreciate any insight.

Thx.See http://blogs.msdn.com/bimusings/archive/2005/12/14/503648.aspx.

Embarrassingly simple question about using stored proceedures

The stored procedure sp_columns returns several fields but i want just the
COLUMN_NAME
if the stored procedure was a table I would write the query
Select COLUMN_NAME from sp_columns
Whats the simplest way of doing this for a stored proceedure
many thanks
David HEither re-write the procedure or use below technique:
CREATE TABLE #tmp...
INSERT #tmp
EXEC sp_columns
SELECT ... FROM #tmp
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
"David Hills" <dhills@.pcfe.ac.uk> wrote in message
news:BEEA10DA-869E-4A9B-B884-56B6070B6725@.microsoft.com...
> The stored procedure sp_columns returns several fields but i want just the
COLUMN_NAME
> if the stored procedure was a table I would write the query
> Select COLUMN_NAME from sp_columns
> Whats the simplest way of doing this for a stored proceedure
> many thanks
> David H
>|||An even simpler approach would be to ditch sp_columns and use the
INFORMATION_SCHEMA views instead. For example, the get the columns of the
Authors table in the pubs database use:
USE pubs
GO
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_CATALOG = 'pubs'
AND TABLE_NAME = 'authors'
or
SELECT COLUMN_NAME
FROM pubs.INFORMATION_SCHEMA.Columns
WHERE TABLE_CATALOG = 'pubs'
AND TABLE_NAME = 'authors'
You can get more information INFORMATION_SCHEMA views in BOL:
http://msdn.microsoft.com/library/d...br />
4pbn.asp
Cheers,
Stefan
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:OmygI4YDEHA.628@.TK2MSFTNGP10.phx.gbl...
> Either re-write the procedure or use below technique:
> CREATE TABLE #tmp...
> INSERT #tmp
> EXEC sp_columns
> SELECT ... FROM #tmp
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
>
> "David Hills" <dhills@.pcfe.ac.uk> wrote in message
> news:BEEA10DA-869E-4A9B-B884-56B6070B6725@.microsoft.com...
the
> COLUMN_NAME
>|||That's a good point, Stefan!
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
"Stefan Delmarco [MSFT]" <StefanDe@.online.microsoft.com> wrote in messag
e
news:eAuYCTZDEHA.3240@.TK2MSFTNGP10.phx.gbl...
> An even simpler approach would be to ditch sp_columns and use the
> INFORMATION_SCHEMA views instead. For example, the get the columns of the
> Authors table in the pubs database use:
> USE pubs
> GO
> SELECT COLUMN_NAME
> FROM INFORMATION_SCHEMA.Columns
> WHERE TABLE_CATALOG = 'pubs'
> AND TABLE_NAME = 'authors'
> or
> SELECT COLUMN_NAME
> FROM pubs.INFORMATION_SCHEMA.Columns
> WHERE TABLE_CATALOG = 'pubs'
> AND TABLE_NAME = 'authors'
> You can get more information INFORMATION_SCHEMA views in BOL:
>
http://msdn.microsoft.com/library/d..._ia-iz_4pbn.asp[
color=darkred]
> Cheers,
> Stefan
> --
> This posting is provided "AS IS" with no warranties, and confers no[/color]
rights.
> "Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote
in
> message news:OmygI4YDEHA.628@.TK2MSFTNGP10.phx.gbl...
> the
>|||Many thanks, that's just what I needed to know to do this
SELECT TABLE_NAME,COLUMN_NAME
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_CATALOG = 'sipr'
AND COLUMN_NAME LIKE '%rlg%'
TABLE_NAME COLUMN_NAME
--
d21_eed44 rlg_code
D04_RLG rlg_code
D04_RLG rlg_name
D04_RLG rlg_snam
SRS_RLG rlg_code
SRS_RLG rlg_name
SRS_RLG rlg_snam
D04_STU stu_rlgc
D04_CONTCT stu_rlg
D51_STU_CTS stu_rlgc
D51_STU_V26 stu_rlgc
INS_STU stu_rlgc

Sunday, February 26, 2012

Else statement is not working in the stored procedure

I have a stored procedure that needs to populate the fields of records for tblBag_data from the tblshipping_sched if not found it looks in the tblshipment_history. But it not checking the history table(2nd table). Please help!

CREATE Procedure spUpdate_bag_data
@.t1 int OUT
AS

declare @.work_ord_num char(9), @.two char(7), @.work_ord_line_num char(3), @.cust_num char(5), @.cust_name char(50), @.apple_part_num char(12), @.apple_catalog_num char(28);

Declare update_bag CURSOR
FOR
SELECT work_ord_num, work_ord_line_num
FROM tblBag_data
WHERE cust_num IS NULL;

OPEN update_bag
FETCH NEXT FROM update_bag INTO @.work_ord_num, @.work_ord_line_num

WHILE @.@.FETCH_STATUS = 0 --and @.counter<30
BEGIN
--set @.counter = @.counter + 1
SET @.two = LEFT(@.work_ord_num,6) + '%'
set @.cust_num = '';

SELECT @.cust_num = cust_num, @.cust_name = cust_name, @.apple_part_num = apple_part_num, @.apple_catalog_num = apple_catalog_num
FROM tblShipping_sched
WHERE work_ord_num like @.two AND work_ord_line_num = @.work_ord_line_num;



IF @.@.RowCount > 0
BEGIN

UPDATE tblBag_data
SET cust_num = @.cust_num, cust_name = @.cust_name, apple_part_num = @.apple_part_num, apple_catalog_num = @.apple_catalog_num
WHERE work_ord_num like @.two AND work_ord_line_num = @.work_ord_line_num;
END

ELSE
BEGIN

SELECT cust_num = @.cust_num, cust_name =@.cust_name, apple_part_num =@.apple_part_num, apple_catalog_num = @.apple_catalog_num FROM tblShipment_history
WHERE work_ord_num like @.two AND work_ord_line_num = @.work_ord_line_num;

IF @.cust_num IS NOT NULL and len(@.cust_num)= 5
UPDATE tblBag_data SET cust_num = @.cust_num, cust_name = @.cust_name, apple_part_num = @.apple_part_num, apple_catalog_num = @.apple_catalog_num
WHERE work_ord_num like @.two AND work_ord_line_num = @.work_ord_line_num;

END

FETCH NEXT FROM update_bag INTO @.work_ord_num, @.work_ord_line_num
END

close update_bag
deallocate update_bag

return(1)Why are you using a cursor? There's no need.

Also if
work_ord_num AND wrk_ord_line_num

are not the primary or a unique constraint to
FROM tblShipping_sched

Then you can get back multiple rows...and your assingment to the variables will be the last one returned...

And since there is no input variable to the sproc, it means you'll be doing every row in the table every time you run it...
well where cust_num is null

Need to see the DDL for the three tables...(sample data wouldn't hurt either)|||Originally posted by Brett Kaiser
Why are you using a cursor? There's no need.

Also if
work_ord_num AND wrk_ord_line_num

are not the primary or a unique constraint to
FROM tblShipping_sched

Then you can get back multiple rows...and your assingment to the variables will be the last one returned...

And since there is no input variable to the sproc, it means you'll be doing every row in the table every time you run it...
well where cust_num is null

Need to see the DDL for the three tables...(sample data wouldn't hurt either)

Hi Brett,

I have a tblBag_data that needs four fields populated from the tblshippping_sched or tblShipment_history. The stored procedure is takes the work_ord_num and work_ord_line_num in tblBag_data and match them to the tblshipping_sched if the cust_num is null. It loops thru the tblshipping_sched for that record if it finds the record it populate the four fields(cust_name, cust_num..)in the tblBag_data. But if it doesn't find it it suppose to go to tblShipment_history table and loops thru for the same record and populates the tblBag_data once it finds it.

The If statement seems to be working fine. But else is definitely not working. If there is better way to write this without cursor please provide some sample code.

Thank you.
I hope it|||Sorry...work got in the way...

How about:

UPDATE l
SET cust_num = r.cust_num
, cust_name = r.cust_name
, apple_part_num = r.apple_part_num
, apple_catalog_num = r.apple_catalog_num
FROM tblBagData l
INNER JOIN tblBagData r
ON r.work_ord_num like LEFT(l.work_ord_num,6) + '%'
AND r.work_ord_line_num = l.work_ord_line_num
WHERE cust_num IS NULL

And you don't even have to worry if it finds it ot not because youcan then just do the second query, because the cust_num will still be null|||Originally posted by Brett Kaiser
Sorry...work got in the way...

How about:

UPDATE l
SET cust_num = r.cust_num
, cust_name = r.cust_name
, apple_part_num = r.apple_part_num
, apple_catalog_num = r.apple_catalog_num
FROM tblBagData l
INNER JOIN tblBagData r
ON r.work_ord_num like LEFT(l.work_ord_num,6) + '%'
AND r.work_ord_line_num = l.work_ord_line_num
WHERE cust_num IS NULL

And you don't even have to worry if it finds it ot not because youcan then just do the second query, because the cust_num will still be null

Hmm... This might be a solution.
I'll give it a try.
Thanks!

Eliminating Duplicates

I have a table with a field called PID. This field can have duplicate
values in it. I have 2 other fields called CAU and SUF. I may have 3 rows
with the same PID and same CAU, but the SUF field when concatenated with the
2 aforementioned fields make the row unique.
I want to know how to select 1 of the 3 rows but not the other 2. In
otherwords I need 1 row for each SUF. Hopes this makes sense and I would
appreciate any help.Try the following query. Work from the inner select out.
CREATE TABLE OneSUF
(
PID INT NOT NULL,
CAU INT NOT NULL,
SUF INT NOT NULL,
Description VARCHAR(255) NOT NULL
)
INSERT INTO OneSUF VALUES (1, 2, 3, '1, 2, 3')
INSERT INTO OneSUF VALUES (1, 2, 4, '1, 2, 4')
INSERT INTO OneSUF VALUES (1, 2, 5, '1, 2, 5')
INSERT INTO OneSUF VALUES (1, 3, 1, '1, 3, 1')
INSERT INTO OneSUF VALUES (1, 3, 2, '1, 3, 2')
INSERT INTO OneSUF VALUES (1, 3, 3, '1, 3, 3')
INSERT INTO OneSUF VALUES (1, 4, 3, '1, 4, 3')
INSERT INTO OneSUF VALUES (1, 4, 4, '1, 4, 4')
INSERT INTO OneSUF VALUES (1, 4, 5, '1, 4, 5')
INSERT INTO OneSUF VALUES (1, 4, 6, '1, 4, 6')
SELECT SUF3.SUF, SUF3.CAU, SUF3.PID, SUF3.Description
FROM OneSUF AS SUF3 INNER JOIN
(
SELECT SUF1.SUF, SUF1.CAU, MIN(SUF1.PID) AS PID FROM OneSUF AS SUF1 INNER
JOIN
(SELECT SUF, MIN(CAU) AS CAU FROM OneSUF GROUP BY SUF) AS SUF2
ON SUF1.SUF = SUF2.SUF AND SUF1.CAU = SUF2.CAU
GROUP BY SUF1.SUF, SUF1.CAU) AS SUF4
ON SUF3.SUF = SUF4.SUF AND SUF3.CAU = SUF4.CAU AND SUF3.PID = SUF4.PID
ORDER BY 1,2,3
--
Barry McAuslin
Look inside your SQL Server files with SQL File Explorer.
Go to http://www.sqlfe.com for more information.
"Jeff Humphrey" <jeffhumphrey@.cox-internet.com> wrote in message
news:%23OzmAYOmDHA.2416@.TK2MSFTNGP10.phx.gbl...
> I have a table with a field called PID. This field can have duplicate
> values in it. I have 2 other fields called CAU and SUF. I may have 3
rows
> with the same PID and same CAU, but the SUF field when concatenated with
the
> 2 aforementioned fields make the row unique.
> I want to know how to select 1 of the 3 rows but not the other 2. In
> otherwords I need 1 row for each SUF. Hopes this makes sense and I would
> appreciate any help.
>

Sunday, February 19, 2012

Efficiently refering to non-identifier fields on "one" side of joi

I have what is probably an elementary question, and will be grateful for you
r
help. I am just moving a database from Jet and learning SQL Server as I go.
In Jet, if I had a query doing aggregate functions on a one-many join, I
would often use First() and Last() on fields on the one side other than the
unique identifier, for efficiency. E.g., I would group on the one side by
Student ID, and refer to First([Student name]). This seems more efficient
than "GROUP BY [student name]", which would be redundant since I am already
grouping by Student ID; and more computationally efficient than Min() or
Max().
Question is, what's the efficient way to do this in SQL Server, since
First() and Last() are not available (which I understand is because SQL
Server more strictly adheres to the paradigm of the unordered set)? Thanks.
--DavidDavid
First() --SQL Server is MIN()
Last() --SQL Server is MAX()
"David Roodman" <DavidRoodman@.discussions.microsoft.com> wrote in message
news:C1F7D9F9-D540-421A-9943-C07B4FC6295C@.microsoft.com...
>I have what is probably an elementary question, and will be grateful for
>your
> help. I am just moving a database from Jet and learning SQL Server as I
> go.
> In Jet, if I had a query doing aggregate functions on a one-many join, I
> would often use First() and Last() on fields on the one side other than
> the
> unique identifier, for efficiency. E.g., I would group on the one side by
> Student ID, and refer to First([Student name]). This seems more efficient
> than "GROUP BY [student name]", which would be redundant since I am
> already
> grouping by Student ID; and more computationally efficient than Min() or
> Max().
> Question is, what's the efficient way to do this in SQL Server, since
> First() and Last() are not available (which I understand is because SQL
> Server more strictly adheres to the paradigm of the unordered set)?
> Thanks.
> --David

Efficiently refering to non-identifier fields on "one" side of

Whether this helps me, as Bill Clinton said, depends on what the meaning of
"is" is. I understand that I can substite, as you seem to be suggesting by
"is." My question is whether this is *efficient*. If I join the student tabl
e
to the attendance record table, which has one row for each student and schoo
l
day, and use Min(), then wouldn't I be telling SQL server to look, for each
student, at 180 identical copies of that student's name and find the
"minimum" one? Wouldn't that be inefficient?
--David
"Uri Dimant" wrote:

> David
> First() --SQL Server is MIN()
> Last() --SQL Server is MAX()
>
> "David Roodman" <DavidRoodman@.discussions.microsoft.com> wrote in message
> news:C1F7D9F9-D540-421A-9943-C07B4FC6295C@.microsoft.com...
>
>Perhaps you should tell us why you'd use FIRST and/or LAST at all. Is the
model properly normalised?
ML
http://milambda.blogspot.com/|||I think it is properly normalized... I have one table with a row for each
student. It contains student ID, name, and so on. I have another table with
a
row for each student and day, for attendance. The two are linked by student
ID. I want a view that gives me each student's name and the number of days
present. Actually, this is a made-up example, but should serve. I had though
t
the efficient way to do this in Jet was to GROUP BY the student ID, and
report the First() of the student name,and some aggregate statistics on the
student's attendance. I avoided grouping by student name or applying min() t
o
the student name because I thought those options were both more
computationally expensive. Is this untrue? Thank you.
"ML" wrote:

> Perhaps you should tell us why you'd use FIRST and/or LAST at all. Is the
> model properly normalised?
>
> ML
> --
> http://milambda.blogspot.com/|||To amplify my previous explanation:
It says "For greater speed, use Group By on as few fields as possible. As an
alternative, use the First function where appropriate."
at
http://msdn.microsoft.com/library/d...ter
S.asp|||> To amplify my previous explanation:
> It says "For greater speed, use Group By on as few fields as possible. As
> an
> alternative, use the First function where appropriate."
There is no FIRST() in SQL Server, so what MSDN happens to say about Jet is
completely irrelevant here.
Are you using SQL Server or Jet?|||>I think it is properly normalized... I have one table with a row for each
> student. It contains student ID, name, and so on. I have another table
> with a
> row for each student and day, for attendance. The two are linked by
> student
> ID. I want a view that gives me each student's name and the number of days
> present. Actually, this is a made-up example, but should serve. I had
> thought
> the efficient way to do this in Jet
Jet? What are you doing in a SQL Server group?
Anyway, you don't have any DDL, so I'll make some up for you.
CREATE TABLE dbo.Students
(
StudentID INT PRIMARY KEY, -- generated from where?
FullName VARCHAR(32)
)
GO
CREATE TABLE dbo.StudentAttendance
(
StudentID INT FOREIGN KEY
REFERENCES dbo.Students(StudentID),
dt SMALLDATETIME,
Present INT NOT NULL DEFAULT 1
)
GO
SET NOCOUNT ON;
INSERT dbo.Students SELECT 1, 'Johnny Keener';
INSERT dbo.Students SELECT 2, 'Molly NiceGirl';
INSERT dbo.Students SELECT 3, 'Trish WithSTD';
INSERT dbo.Students SELECT 4, 'Jimmy BadApple';
INSERT dbo.Students SELECT 5, 'New Kid';
GO
INSERT dbo.StudentAttendance SELECT 1, '20060101', 1;
INSERT dbo.StudentAttendance SELECT 1, '20060102', 1;
INSERT dbo.StudentAttendance SELECT 1, '20060103', 1;
INSERT dbo.StudentAttendance SELECT 2, '20060101', 1;
INSERT dbo.StudentAttendance SELECT 2, '20060102', 1;
INSERT dbo.StudentAttendance SELECT 2, '20060103', 1;
INSERT dbo.StudentAttendance SELECT 3, '20060101', 0;
INSERT dbo.StudentAttendance SELECT 3, '20060102', 1;
INSERT dbo.StudentAttendance SELECT 3, '20060103', 0;
INSERT dbo.StudentAttendance SELECT 4, '20060101', 0;
INSERT dbo.StudentAttendance SELECT 4, '20060102', 0;
INSERT dbo.StudentAttendance SELECT 4, '20060103', 0;
GO
SELECT s.StudentID,
s.FullName,
DaysPresent = COALESCE(a.DaysPresent, 0)
FROM
dbo.Students s
LEFT OUTER JOIN
(
SELECT StudentID,
DaysPresent = SUM(Present)
FROM dbo.StudentAttendance
GROUP BY StudentID
) a
ON
s.StudentID = a.StudentID;
GO
DROP TABLE dbo.StudentAttendance;
DROP TABLE dbo.Students;
I don't see why FIRST() or LAST() would make sense in any of this, even in
Jet.
A|||From my first post: "I am just moving a database from Jet and learning SQL
Server as I go."
Am I not being clear? It seems that people are reading too fast but trying
to be helpful. I was doing what is recommended practice in Jet; the
underlying concern that motivated that advice from MSDN still stands,
seemingly; and I am wondering what is the efficient way to handle it in SQL
Server. "Efficient" for the computer, not me.

Efficient Date Query?

Have data stored in two separate fields (Start_Date and End_Date). i.e.:
ID Start_Date End_Date
1 5/5/2006 7/8/2006
2 7/7/2006 7/9/2006
3 6/8/2006 8/5/2006
....
Basically I have to query this based on a MONTH and a YEAR and return all
records where any day in that month will fall between the start/end date.
So if I pass 7/2006 to the query, it will return all of the records. If I
choose 8/2006 it will only return the last one. If I choose 6/2006 it will
return the first and last. Theoretically, I could do a UNION of between
statements for every day a given month, but that is ridiculously ineffecient
and borderline stupid. There has to be a good way of doing this, I just
can't think of it off the top of my head and the guy who's paid to do so
isn't here. Can someon refresh my memory on an easy way to do this?
Thank you!This is a multi-part message in MIME format.
--=_NextPart_000_081B_01C6A751.D81740F0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
The guy that's paid to do this isn't here either...
This may work for you. (I've selected using the ISO format for date.)
DECLARE @.MyDate varchar(6)
DECLARE @.Durations table
( ID int
, Start_Date datetime
, End_Date datetime
)
INSERT INTO @.Durations Values (1, '5/5/2006','7/8/2006')
INSERT INTO @.Durations Values (2, '7/7/2006','7/9/2006')
INSERT INTO @.Durations Values (3, '6/8/2006','8/5/2006')
SET @.MyDate =3D '200608'
SELECT ID
, Start_Date
, End_Date
FROM @.Durations WHERE @.MyDate BETWEEN ( convert( varchar(6), Start_Date, 112 )) AND ( =convert( varchar(6), End_Date, 112 ))
-- Arnie Rowland
"To be successful, your heart must accompany your knowledge."
"James" <minorkeys@.gmail.com> wrote in message =news:uP8Lwh4pGHA.516@.TK2MSFTNGP05.phx.gbl...
> Have data stored in two separate fields (Start_Date and End_Date). =i.e.:
> > ID Start_Date End_Date
> 1 5/5/2006 7/8/2006
> 2 7/7/2006 7/9/2006
> 3 6/8/2006 8/5/2006
> ....
> > Basically I have to query this based on a MONTH and a YEAR and return =all > records where any day in that month will fall between the start/end =date. > So if I pass 7/2006 to the query, it will return all of the records. =If I > choose 8/2006 it will only return the last one. If I choose 6/2006 it =will > return the first and last. Theoretically, I could do a UNION of =between > statements for every day a given month, but that is ridiculously =ineffecient > and borderline stupid. There has to be a good way of doing this, I =just > can't think of it off the top of my head and the guy who's paid to do =so > isn't here. Can someon refresh my memory on an easy way to do this?
> > Thank you! > >
--=_NextPart_000_081B_01C6A751.D81740F0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&

The guy that's paid to do this isn't =here either...
This may work for you. (I've selected =using the ISO format for date.)
DECLARE @.MyDate =varchar(6)
DECLARE @.Durations =table ( ID int , Start_Date datetime , End_Date datetime )
INSERT INTO @.Durations Values =(1, '5/5/2006','7/8/2006')INSERT INTO @.Durations Values (2, '7/7/2006','7/9/2006')INSERT INTO @.Durations Values (3, '6/8/2006','8/5/2006')
SET @.MyDate =3D ='200608'
SELECT = ID , Start_Date , End_DateFROM =@.Durations WHERE @.MyDate BETWEEN ( convert( varchar(6), Start_Date, 112 )) AND =( convert( varchar(6), End_Date, 112 ))
-- Arnie Rowland"To be =successful, your heart must accompany your knowledge."
"James" =wrote in message news:uP8Lwh4pGHA.516@.TK2MSFTNGP05.phx.gbl...> =Have data stored in two separate fields (Start_Date and End_Date). i.e.:> => ID Start_Date End_Date> 1 5/5/2006 7/8/2006> 2 7/7/2006 7/9/2006> 3 6/8/2006 8/5/2006> ....> > Basically I have to query this based on a MONTH =and a YEAR and return all > records where any day in that month will =fall between the start/end date. > So if I pass 7/2006 to the query, =it will return all of the records. If I > choose 8/2006 it will =only return the last one. If I choose 6/2006 it will > return the first =and last. Theoretically, I could do a UNION of between > =statements for every day a given month, but that is ridiculously ineffecient > =and borderline stupid. There has to be a good way of doing this, I =just > can't think of it off the top of my head and the guy who's paid =to do so > isn't here. Can someon refresh my memory on an easy =way to do this?> > Thank you! > >

--=_NextPart_000_081B_01C6A751.D81740F0--

Efficient Date Query?

Have data stored in two separate fields (Start_Date and End_Date). i.e.:
ID Start_Date End_Date
1 5/5/2006 7/8/2006
2 7/7/2006 7/9/2006
3 6/8/2006 8/5/2006
....
Basically I have to query this based on a MONTH and a YEAR and return all
records where any day in that month will fall between the start/end date.
So if I pass 7/2006 to the query, it will return all of the records. If I
choose 8/2006 it will only return the last one. If I choose 6/2006 it will
return the first and last. Theoretically, I could do a UNION of between
statements for every day a given month, but that is ridiculously ineffecient
and borderline stupid. There has to be a good way of doing this, I just
can't think of it off the top of my head and the guy who's paid to do so
isn't here. Can someon refresh my memory on an easy way to do this?
Thank you!The guy that's paid to do this isn't here either...
This may work for you. (I've selected using the ISO format for date.)
DECLARE @.MyDate varchar(6)
DECLARE @.Durations table
( ID int
, Start_Date datetime
, End_Date datetime
)
INSERT INTO @.Durations Values (1, '5/5/2006','7/8/2006')
INSERT INTO @.Durations Values (2, '7/7/2006','7/9/2006')
INSERT INTO @.Durations Values (3, '6/8/2006','8/5/2006')
SET @.MyDate = '200608'
SELECT
ID
, Start_Date
, End_Date
FROM @.Durations
WHERE @.MyDate BETWEEN ( convert( varchar(6), Start_Date, 112 )) AND ( conver
t( varchar(6), End_Date, 112 ))
--
Arnie Rowland
"To be successful, your heart must accompany your knowledge."
"James" <minorkeys@.gmail.com> wrote in message news:uP8Lwh4pGHA.516@.TK2MSFTNGP05.phx.gbl...[
vbcol=seagreen]
> Have data stored in two separate fields (Start_Date and End_Date). i.e.:
>
> ID Start_Date End_Date
> 1 5/5/2006 7/8/2006
> 2 7/7/2006 7/9/2006
> 3 6/8/2006 8/5/2006
> ....
>
> Basically I have to query this based on a MONTH and a YEAR and return all
> records where any day in that month will fall between the start/end date.
> So if I pass 7/2006 to the query, it will return all of the records. If I
> choose 8/2006 it will only return the last one. If I choose 6/2006 it wil
l
> return the first and last. Theoretically, I could do a UNION of between
> statements for every day a given month, but that is ridiculously ineffecie
nt
> and borderline stupid. There has to be a good way of doing this, I just
> can't think of it off the top of my head and the guy who's paid to do so
> isn't here. Can someon refresh my memory on an easy way to do this?
>
> Thank you!
>
>[/vbcol]

Friday, February 17, 2012

Efficiency in inserting Null Values into fields which allow nulls.

Hi,
I have fields in my table which allow nulls. Is it efficient to not insert anything (the field automatically shows up as null in this case) and leave or store some value into it. The field is a smallint field?

Thanksheres some info from BOL :

Allowing Null Values
The nullability of a column determines if the rows in the table can contain a null value for that column. A null value, or NULL, is not the same as zero (0), blank, or a zero-length character string such as ""; NULL means that no entry has been made. The presence NULL usually implies that the value is either unknown or undefined. For example, a null value in the price column of the titles table of the pubs database does not mean that the book has no price; NULL means that the price is unknown or has not been set.In general, avoid permitting null values because they incur more complexity in queries and updates and because there are other column options, such as PRIMARY KEY constraints, that cannot be used with nullable columns.

If a row is inserted but no value is included for a column that allows null values, Microsoft® SQL Server? 2000 supplies the value NULL (unless a DEFAULT definition or object exists). A column defined with the keyword NULL also accepts an explicit entry of NULL from the user, no matter what data type it is or if it has a default associated with it. The value NULL should not be placed within quotation marks because it will be interpreted as the character string 'NULL', rather than the null value.

Specifying a column as not permitting null values can help maintain data integrity by ensuring that a column in a row always contains data. If null values are not allowed, the user entering data in the table must enter a value in the column or the table row cannot be accepted into the database.

Note Columns defined with a PRIMARY KEY constraint or IDENTITY property cannot allow null values.
|||To be honest I'm not sure. Once you've made the decision to use NULLs (and there are lots of pros/cons to that) I doubt there is much in it. However, if you have a choice *and* you're really trying to eek out the last drop of performance then I'd avoid NULLs. Having said that I'm sure there are thousands of other places to look for better optimisations before you go here.

Wednesday, February 15, 2012

Editing text fields using standard tools

Is there possibility to edit easily data type "text" field using standard tools such as SQL server 2005 Management Studio or Server Explorer in Visual Studio 2005 Professional. I want multiline editing akin to multiline textbox in Windows Form. I understand that something may be written/generated in Windows Forms , but I want this possibility for any table.
My pressing "Enter" to create another line for in-place editing just leaves a text cell for the tools mentioned above.
No, I am afraid they don′t. Some editors support Shift+Enter or Alt+Enter, but as far as I know these one don′t.

Jens K. Suessmeyer.

http://www.sqlserver2005.de