Showing posts with label row. Show all posts
Showing posts with label row. Show all posts

Thursday, March 22, 2012

Difficulty in writing a DateTime to a table

I'm developing an application in VB 2005 Express using SQL 2005 Express. I need to put a timestamp into my table each time I create a row...

The following is a snippet...

Dim DDate As [SqlDateTime] = Now()

Dim TheQuery As String = "INSERT INTO Groups (PC_Name_Stamp, OperatorNo, Group_Type, Date_Time) VALUES ('Development', '2', 'Test',' " & DDate & " ')"

Which won't work as I am attempting to concatinate a SqlDateTime into a string.

My best guess is that I need to somehow to use a DEFAULT value in the table that persists so each time a row is created the datetime it was created is saved with the row, rather than being re-calculated each time the table is opened. There are probably several other ways of doing it and this may not be the easiest.

I'm not a programmer, just an Engineer, so I can only read Help for 5 minutes at a time.

Hi Ian,

What is the data type of the field named Date_Time? What is the error you are getting?

I would suggest removing the quotes from your query and just letting SQL handle the data type change from SqlDateTime into what ever Date_Time is defined as. If it is a string, the conversion will likely happen automatically. (At least it does in T-SQL.)

If that doesn't work, consider using a type conversion function rather than just wraping the value in quotes. You should be able to find information about data type conversion functions in the VB help documentations.

One final consideration, if you only need to determine if a row has been updated and the actual time is not really important, you might consider the timestamp data type. This data type automatically updates when a row is added or changed, but it stores a relative time, no the time associated with a clock.

Regards,

Mike Wachal
SQL Express team

-
Mark the best posts as Answers!

|||

Mike,

In order: "datetime"

The error I get is a VB pre-complier error warning that I am attempting to mix "String" with "SqlDateTime".

Removing the quotes does not help as the connection to the database is made with the following command:

Dim TheCommand As SqlCommand = New SqlCommand(TheQuery, TheConnection)

Where both "TheQuery" and "TheConnection" are strings.

Unfortunately I need to know the time and date, this will be used for locating information later.

My most recent attempts have used :

Dim TheQuery As String = "INSERT INTO Groups (PC_Name_Stamp, OperatorNo, Group_Type, Date_Time) VALUES ('Development', '2', 'Test',' GETDATE() ')"

Which throws "Cannot insert an explicit value into a timestamp column. Use INSERT with a column list to exclude the timestamp column, or insert a DEFAULT into the timestamp column." on execution (of the VB program).

I have attempted to set the default on the Date_Time column in "Groups" to be calculated as Getdate() and persistent, but then I get the following error: "Computed column 'DateTime' in table 'Tmp_Groups' cannot be persisted because the column is non-deterministic." I note that a calculated column in the table has no "format", which makes sense if it is not to be saved.

I am assuming that this is the correct forum, I think that the problem is related to SQL rather than .NET or VB.

I will be off the air now for 54 hours due to the weekend.

Thanks for your time Mike.

Ian

|||

It's interesting that you're getting an error specifying a timestamp field. Timestamp is different than datetime, you should make sure that you really have a datetime field.

That said, you're still trying to create string values by putting quotes around them. That isn't how you do it, you have to conver non-string values into strings using a conversion function, in this case CStr(). I managed to write an insert from VB by converting the output of Now() to a string and passing that in my query. I've povided the table definition and VB code below for you to examine.

Table Def-

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE TABLE [dbo].[Shippers](

[ShipperID] [int] IDENTITY(1,1) NOT NULL,

[CompanyName] [nvarchar](40) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

[Phone] [nvarchar](24) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

[tsTime] [datetime] NULL,

[sTime] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

CONSTRAINT [aaaaaShippers_PK] PRIMARY KEY NONCLUSTERED

(

[ShipperID] ASC

)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]

-- End Table -

- Code--

Sub Main()

Dim cnn As New SqlConnection

cnn.ConnectionString = "Data Source=.\sqlexpress;Initial Catalog=NorthwindSQL_Upsize;Integrated Security=True"

cnn.Open()

Dim cmd As New SqlCommand

cmd.CommandText = "INSERT INTO Shippers (CompanyName, Phone, tsTime) VALUES('VB Shippers','555-1212','" & CStr(Now()) & "')"

cmd.Connection = cnn

cmd.ExecuteNonQuery()

End Sub

- End Code --

|||

Mike,

A very astute observation, I do have an issue with the column format.

A search of my harddisk shows a total of 8 copies of this database, two I have as backups, one is a copy I was importing data into using Management Studio, the rest have uncertain pedigree, (some I no doubt created in error).

I long for the days of SQL Server 2000 when I only kept two copies of the database, (one live & one copy). Having a cut-back latest version makes it very tempting to develop & use the latest, but I wonder if SQL Server Express has been emasculated to the extent that it is no longer worthy of the title "Database". Yes, the mistake was mine, I think it was an easier mistake to make due to the "over enthusiastic feature removal".

Thank you for you help Mike.

Ian

sql

Wednesday, March 21, 2012

Difficult SQL

Hello,

Please can anyone help?

I have got two tables, accounts and account customers.

The accounts table has more than one row for each account, identified by the account number and the sub account.

Each customer should be linked to all of the sub accounts for their account (in the account customers table).

I have found some accounts where one customer might be linked to all the sub accounts but the 2nd customer on the account is only linked to one of the sub accounts. This is wrong & needs to be corrected.

Please can anyone advise how I go about selecting all of the problem accounts.

To start with I grouped all the existing links together, I then need to somehow check each account against this main list.

Please can you advise.

Thanks,
BethBeth, describe the tables and give sample data. :confused:

Wednesday, March 7, 2012

Different sums from table

Could someone explain to me, how I can get sum from row which I have values in 2 colums and I want the realtime sum to third column. Fourth colum is for item.

Also can someone tell me how to sum these third colums where the item is same so I have real time values for the item sum.

Thanks!

AD

Hi,

regardless that this makes no sense at all, this could be an example (as far as I understood your problem):

Select OrderId, Sum(Unitprice) + Sum (Quantity) AS ThirdColumn

from [Order Details]

Group by OrderId

HTH, Jens Suessmeyer.

http://www.sqlserver2005.de

Different row delimiters in same connection manager

I have a situation where two CSV dmited types are read in. One file row type could be CRLF while another could be CR. Seeing how the only difference between these file types is the row delimiter, I would like to use one conn manager. Is it possible? Ideas?

Try this:
create a string variable Delimiter.
In Connection Manager window select your csv connection and look at its properties.
Open Expressions form and add an expression to the property RowDelimiter setting it to the variable User::Delimiter
Then you should be able to progamatically switch from one delimiter to another (toggling the variable value) before starting to load from the csv.
(i cannot try now this solution, take is as a hint)
|||

Unfortunately, this solution will not work. The RowDelimiter property is used only initially when no flat file connection columns are defined. After the columns get created the actual row delimiter is a column delimiter of the last column. You cannot do the same trick with the column delimiter because it is not expressionable.

Are you only concerned about the redundancy (with the two connection managers), or you had in mind more elegant data flow (with a multi flat file connection and one flat file source in a loop, perhaps)? I would like to find out more about your motivation to merge those two connection managers.

Thanks,

|||I am looking mainly to cut down the redundancy. There are 100+ inbound conns that need to support CRLF and CR returns, and it would end up being way too redundant to create a seperate manager for each return.|||

I can settle with just looking for CR for row delimiter. So, is there a way to maybe pad (or ignore) any LF's?

|||You can try that. The additional LF character would wrap up to the start of the first column of the next row. You should be able to clean it up using the Derived Column transform. However, I suspect it will make a headache with the incomplete last row, which would consist of a single LF character.

The solution I would go with is to have a Script Task before the Data Flow and preprocess files in it -- to unify the row delimiters.

Thanks,|||

Yah, I thought about doing this, however, won't this drain performance by having to essentially read/write the file twice (keeping in mind that some of these files can be rather large in size)? How would you handle the pre-processing in script?

Thanks.

|||

It will certainly have some performance impact. You should probably do some tests to verify how acceptable the cost is for the added manageability of your packages.

In the script, I would look for CR character and if the next one is not LF I would insert it. If you can be sure about the files with expected initial format ({CR}{LF}), you can skip those.

Thanks,

Different row colors (AlternatingItemStyle in asp.net)

Hi everyone!
Is it possible to changes the background color for each row? People coding
asp.net would know this as "AlternatingItemStyle" in a datagrid.
Can this be done using the VS IDE or do I have to go behind the code in the
rdl file?
Best Regards
Jesper.Hello Jesper,
you can put the following into the Background-property in VS IDE:
Backgroundcolor
=IIF(RowNumber("<table1>") Mod 2, "<color1>", "<color2>")
That's all.
Stefan
"Jesper Jensen" <jbj@.union.dk> schrieb im Newsbeitrag
news:egG1HdNrHHA.1204@.TK2MSFTNGP04.phx.gbl...
> Hi everyone!
> Is it possible to changes the background color for each row? People coding
> asp.net would know this as "AlternatingItemStyle" in a datagrid.
> Can this be done using the VS IDE or do I have to go behind the code in
> the rdl file?
> Best Regards
> Jesper.
>|||Hello Stefan
So simple and so powerfull.
Thank you very much!!!!!
Best Regards
Jesper
"Stefan" <stefan@.nospam.nospam> wrote in message
news:eqki9NOrHHA.1168@.TK2MSFTNGP03.phx.gbl...
> Hello Jesper,
> you can put the following into the Background-property in VS IDE:
> Backgroundcolor
>
> =IIF(RowNumber("<table1>") Mod 2, "<color1>", "<color2>")
>
> That's all.
>
> Stefan
>
> "Jesper Jensen" <jbj@.union.dk> schrieb im Newsbeitrag
> news:egG1HdNrHHA.1204@.TK2MSFTNGP04.phx.gbl...
>> Hi everyone!
>> Is it possible to changes the background color for each row? People
>> coding asp.net would know this as "AlternatingItemStyle" in a datagrid.
>> Can this be done using the VS IDE or do I have to go behind the code in
>> the rdl file?
>> Best Regards
>> Jesper.
>

Sunday, February 19, 2012

Different columns

Hi !
Lets say i have a table TITLES with columns ID,INDEKS,TEKST
Values in row are
ID INDEKS TEKST
1 World War II Day by Day
now is use query
SELECT * FROM TITLES T INNER JOIN CONTAINSTABLE(TITLES,*,'"war" and "day"')
CT
ON T.ID=CT.[KEY]
This query returns nothing because word "war" is in column INDEKS and word
"day" is in column "TEKST"!
Is there any solusion?
Best regards;
Meelis
i have tryed different solutions(freetext,freetextable,contains, multible
joins aso.) but with no luck
The problem is when i use query
SELECT * FROM TITLES T INNER JOIN CONTAINSTABLE(TITLES,*,'"war" and "day"')
CT ON T.ID=CT.[KEY]
I need get result when word "war" is in field INDEKS and word "day" is in
field TEKST
word "day" is in field INDEKS and word "war" is in field TEKST
or word "war" is in field INDEKS and word "day" is in field INDEKS
or word "war" is in field TEKST and word "day" is in field TEKST
Meelis
"Meelis Lilbok" <meelis.lilbok@.deltmar.ee> kirjutas snumis news:
us8CkB1PIHA.3676@.TK2MSFTNGP06.phx.gbl...
> Hi !
> Lets say i have a table TITLES with columns ID,INDEKS,TEKST
> Values in row are
> ID INDEKS TEKST
> 1 World War II Day by Day
> now is use query
>
> SELECT * FROM TITLES T INNER JOIN CONTAINSTABLE(TITLES,*,'"war" and
> "day"') CT
> ON T.ID=CT.[KEY]
> This query returns nothing because word "war" is in column INDEKS and word
> "day" is in column "TEKST"!
>
> Is there any solusion?
>
> Best regards;
> Meelis
>
|||Does this work for you?
create table titles(id int identity not null constraint titlespk primary
key,INDEKS varchar(50),TEKST varchar(50))
GO
insert into titles(indeks,tekst) values('World War','Day by Day')
insert into titles(indeks,tekst) values('World War Day','test')
insert into titles(indeks,tekst) values('test','World Way Day')
GO
create fulltext catalog test as default
GO
create fulltext index on titles(indeks,tekst) key index titlespk
Go
select * from titles
join containstable(titles,indeks,'War') as a on a.[key]=id
join containstable(titles,tekst,'day') as b on titles.id=b.[key]
GO
http://www.zetainteractive.com - Shift Happens!
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"Meelis Lilbok" <meelis.lilbok@.deltmar.ee> wrote in message
news:us8CkB1PIHA.3676@.TK2MSFTNGP06.phx.gbl...
> Hi !
> Lets say i have a table TITLES with columns ID,INDEKS,TEKST
> Values in row are
> ID INDEKS TEKST
> 1 World War II Day by Day
> now is use query
>
> SELECT * FROM TITLES T INNER JOIN CONTAINSTABLE(TITLES,*,'"war" and
> "day"') CT
> ON T.ID=CT.[KEY]
> This query returns nothing because word "war" is in column INDEKS and word
> "day" is in column "TEKST"!
>
> Is there any solusion?
>
> Best regards;
> Meelis
>
|||Hi
Does not, let me explain with my poor english
INDEKS column is indexed part for book titles
TEKST column is for text.
When book title has any non alphanumeric chars then title is splited to two
parts
for example: Title is "World War II - Day by Day"
then indeks=World War II
tekst=- Day by Day
now user searches words "war" and "day"
with containstable i get nothing back, beacuse "war" is on INDEKS and "day"
is on TEKST
searched words can be on both or only on field!
Best regards;
Meelis
"Hilary Cotter" <hilary.cotter@.gmail.com> kirjutas snumis news:
#l4aqEkQIHA.5160@.TK2MSFTNGP05.phx.gbl...
> Does this work for you?
> create table titles(id int identity not null constraint titlespk primary
> key,INDEKS varchar(50),TEKST varchar(50))
> GO
> insert into titles(indeks,tekst) values('World War','Day by Day')
> insert into titles(indeks,tekst) values('World War Day','test')
> insert into titles(indeks,tekst) values('test','World Way Day')
> GO
> create fulltext catalog test as default
> GO
> create fulltext index on titles(indeks,tekst) key index titlespk
> Go
> select * from titles
> join containstable(titles,indeks,'War') as a on a.[key]=id
> join containstable(titles,tekst,'day') as b on titles.id=b.[key]
> GO
> --
> http://www.zetainteractive.com - Shift Happens!
> Looking for a SQL Server replication book?
> http://www.nwsu.com/0974973602.html
> Looking for a FAQ on Indexing Services/SQL FTS
> http://www.indexserverfaq.com
> "Meelis Lilbok" <meelis.lilbok@.deltmar.ee> wrote in message
> news:us8CkB1PIHA.3676@.TK2MSFTNGP06.phx.gbl...
>
|||Taking Hilary's example and expanding it to handle your cases, this might work:
select * from titles
left join containstable(titles,indeks,'War') as a on a.[key]=id
left join containstable(titles,indeks,'day') as b on b.[key]=id
left join containstable(titles,tekst,'War') as c on titles.id=c.[key]
left join containstable(titles,tekst,'day') as d on titles.id=d.[key]
where (a.[key] is not null and (c.[key] is not null or d.[key] is not null))
or (b.[key] is not null and (c.[key] is not null or a.[key] is not null))
or (c.[key] is not null and (b.[key] is not null or d.[key] is not null))
or (d.[key] is not null and (c.[key] is not null or a.[key] is not null))
which is messy and probably not very efficient, and becomes increasingly
more complex to construct as you add terms.
An alternative is to create a new column that you index for these multiple
column searches, concatentating the columns together - this is what I do for
wide searches on my own site - eg. if you search in the Title column it only
finds words in Title, but if you do a "wide" search it uses the Keywords
column which is concatentated from Title + SubTitle + Author + Subject +
AdditionalKeywords.
Dan
Meels wrote on Wed, 19 Dec 2007 16:27:30 +0200:

> Hi

> Does not, let me explain with my poor english

> INDEKS column is indexed part for book titles
> TEKST column is for text.

> When book title has any non alphanumeric chars then title is splited to
> two parts for example: Title is "World War II - Day by Day"
> then indeks=World War II tekst=- Day by Day

> now user searches words "war" and "day"

> with containstable i get nothing back, beacuse "war" is on INDEKS and
> "day" is on TEKST

> searched words can be on both or only on field!

> Best regards;
> Meelis
[vbcol=seagreen]
> "Hilary Cotter" <hilary.cotter@.gmail.com> kirjutas snumis news:
> #l4aqEkQIHA.5160@.TK2MSFTNGP05.phx.gbl...
[vbcol=seagreen]
[vbcol=seagreen]
[vbcol=seagreen]
[vbcol=seagreen]
[vbcol=seagreen]
[vbcol=seagreen]
[vbcol=seagreen]
[vbcol=seagreen]
[vbcol=seagreen]
[vbcol=seagreen]

different colors for a matrix

Hi.
I have a matrix containing 3 row groups and 2 column groups and of course a detail area.
I want to show the detail area in different colors according to the row number.For example
1. row -->blue
2. row -->green
3. row -->blue

4. row -->green
5. row -->blue

6. row -->green
and so this goes....

I used the =IIF(RunningValue(FieldName, CountDistinct, "matrix1") Mod 2 , color1, color2)
for the detail's backgroundColor property
But it didn't work as I want.This caused a result like this:
1. row -->blue

2. row -->blue

3. row -->blue

4. row -->green

5. row -->blue

6. row -->green
.......
And I didn't understand how it works.
How could I do this.Which group name should I use in the formula?
Or do you have another idea for this problem?

Hi,

Chris Hayes blog has all the details you need:

http://blogs.msdn.com/chrishays/

Regards,

Sanjay

different color in each row

Hello,
I am using reportviewer control, It is a table based report and I have many
columns. Is it possible to give different color to each row so that users
will easily track the same line.
Thanks,
Jim.Yes. Click on the row selector of the table to highlight the entire row in
Layout view, then in the Properties sheet go to the BackgroundColor property.
Use an expression for the color. The expression should be something like:
=iif(RowNumber("DataSet1") mod 2 = 1, "WhiteSmoke", "LightCyan")
and obviously have your own colors instead of the examples WhiteSmoke and
LightCyan. also replace DataSet1 with tne name of your dataset.
HTH
Charles Kangai, MCT, MCDBA
"JIM.H." wrote:
> Hello,
> I am using reportviewer control, It is a table based report and I have many
> columns. Is it possible to give different color to each row so that users
> will easily track the same line.
> Thanks,
> Jim.
>