Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Coders I have 3 table in which I want to store only one and delete other like below:

SQL
X
X_20172606
X_20172706


one and name x other has date with it so what I want is I want to delete it previous date table and store only todays date table. How can I achieve this

What I have tried:

Create 3 tables like below
X
X_20172606
X_20172706
Posted
Updated 27-Jun-17 0:36am
Comments
Richard Deeming 27-Jun-17 13:51pm    
That sounds like an extremely bad database design!

You should have a single table (X), with a column to indicate the record date. That way, you're not scrabbling around trying to find which table the record is stored in.

Database Normalization Explained in Simple English[^]
amitesh1989 28-Jun-17 0:22am    
its just an example to give everyone a hint what actually I am looking for

1 solution

SQL has a number of date-formats available.

Getting Previous Day:
You can uses DATEADD() with GETDATE(), using negative values for amount of days you wish to "Add".

Getting Date Parts:
Use YEAR(), MONTH() and DAY() on the new date value and build your string. Don't forget to CAST() them as strings. and use them to build your string for the table name, along with whatever 'X' is, and as long as your at it, make the entire DROP TABLE statement at the same time.

EXEC() the string.

Now - you can, after adjusting the date for the previous date also use some of the date formatting options via, for example, CONVERT().

Notice I didn't spell-it-all out for you. I'd like you to learn how to do the work of looking up how to format the function arguments - which, while you're looking at them, will teach you other things as well which, no doubt, will be useful another day.
 
Share this answer
 
Comments
amitesh1989 27-Jun-17 7:16am    
I have to drop first 2 and show only the last one how can I achieve that
W Balboos, GHB 27-Jun-17 9:15am    
How do you tell them apart? Create the names to do that.
Duplicate your own logic to distinguish them.

Put some work into it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900