Need some help writing an SQL statement.

Posted on 2011-03-08
Last Modified: 2012-05-11
I use Spiceworks on my network at work, and I need an update sql statment that will replace the existing ID file anywhere there are ticket ID numbers with a number that is 10109000 less than what is there. I foolishly thought setting a number that was the date was a good way to go for the helpdesk tickets. I was wrong. I know that purchases, comments and tickets all the use and link tickets together by their ID, and I've looked through the other tables, but I probably missed something. It's been awhile since I wrote any SQL and my guess is that someone can post it quickly for me.

So, in a nutshell I need to replace the ticket ID with a number that is 10109000 less than what exists now. For example:

ID = 10110015 would be changed to 1015. (10110015 - 10109000) = 1015

Question by:JBHale
  • 2

Expert Comment

by:Alex Matzinger
ID: 35074960
Update <your table> set ID=ID-10109000

Expert Comment

by:Alex Matzinger
ID: 35075007
I'm sorry, i got caught up with the last little bit, and forgot to include the condition.  Could you please give me an example of when you would like to update the ID.  Such as you have ID1 ID2 and ID3, and you need ID1 changed becasue ID3=X but ID2 doesn't need to be changed because it equals Y...or somtehing like that.  Thank You.
LVL 40

Accepted Solution

Sharath earned 250 total points
ID: 35075085
Probably you can add a WHERE conditio to update only those IDs which are greater than 10109000.
Update your_table set ID = ID - 10109000 WHERE ID > 10109000 

Open in new window


Expert Comment

ID: 35075159
if i read correctly what you meant to say i believe this is your query:

SET ID=ID-10109000
WHERE ID BETWEEN (highestID-10109000) AND highestID

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
date diff with Fiscal Calendar 4 30
Sql Server group by 10 29
optimize stored procedure 6 28
sqlserver get datetime field and create a string 5 18
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question