Thursday, March 29, 2012
deleting rows based on nvarchar data
What is the correct syntax to script the removal of these rows based field parameter?delete tableA where fieldB = ?
Is that what you mean?|||Kinda. I only have one table and want to delete specific rows from that table that have a specific data within a certain field.
Let be more specific. I have a table (tableA) with 10 fields. Field 3 has data that does not conform to a datetime format and I would like to remove it. The field is currently a nvarchar(50) type (2003-10-10).
I want to remove rows that contain data that is looks like this
(0020-10-10). Make sense?|||delete from table where field3 ='0020-10-10'|||Perhaps you can use the ISDATE() function, which returns 1 if a string can be converted to a valid date, and 0 if it cannot.
Try this query:
select *
from YourTable
where ISDATE([Column3]) = 0
If this returns the rows you want deleted, then change the query to a delete query:
delete
from YourTable
where ISDATE([Column3]) = 0|||right but I forgot to mention, there are all kinds of variation of that date.
I ran a script that reads as follows to help identify data within a field that does not fit a date format->
SELECT * FROM findet WHERE ISDATE(servfrom) = 0
This gave me a list of records that are not in proper date format. Now, I would like to remove them from my table. Can I use the same,
delete findet where ISDATE(servfrom)=0|||See previous post.|||That worked like a charm, thank you!!
Sunday, February 19, 2012
delete records ?
Hello, its hard to explain, i have a table like this:
--userpage_visitors--
id bigint
owner nvarchar(20)
visitor nvarchar(20)
created datetime
Then i have some code like this: (@.Visitor is send to the stored proc)
DECLARE @.lastusernvarchar(20)
SELECTTOP 1 @.lastuser= visitorFROM userpage_visitorsWHERE(owner= @.UserName)ORDERBY createdDESC
IF(@.lastuser<> @.Visitor)
BEGIN
INSERTINTO userpage_visitors(owner, visitor, created)VALUES(@.UserName, @.Visitor, @.Created)
-- delete here
END
Now after i have inserted the new visitor into the table, i need to clean the table... so each user should have maximum of 30 visitors, so if the user i inserted above is the 31st user then i need to delete the first user, so i always have 30 fresch visitors,, if they have less then 30 visitors then nothing should happen. The question is, how can i get the 31th post? in mysql you can say that you want post 30, 31, but in mssql you only have the TOP to select limited posts, any ideas?
Patrick
In Sql 2005 you can actually select limited records using Row_Number()But what you need to do is where you have marked -- delete here
IF (SELECT COUNT(ID) FROM Userpage_Visitors ) > 30
BEGIN
DELETE FROM Userpage_Visitors WHERE ID = (SELECT TOP 1 ID FROM Userpage_Visitors ORDER BY Created)
END
That will delete the oldest entry in the table