?
Solved

Extracting period from Date Column

Posted on 2014-09-24
3
Medium Priority
?
304 Views
Last Modified: 2014-09-25
The following query runs for about 15 minutes on a 77,000,000 row temporary table. This is too long. I expect there is a more efficient to do this without the replicate() and cast() functions. Any help is appreciated.

 update #sometable set Period = cast(year(StartTime) as char(4)) + '-' + replicate('0', 2 - len(rtrim(cast(month(StartTime) as varchar(2))))) + cast(month(StartTime) as varchar(2))
0
Comment
Question by:dthansen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40342584
For starters, a case can be made that a physical table should be used for 77 million rows.

Before we jump to conclusions, give us a data mockup of what StartDate looks like in the table (datetime?), data type of period, and what period needs to look like.

<wild guess>
UPDATE #sometable
SET Period = CAST(year(StartTime) as char(4)) + '-' + RIGHT('0' + CAST(month(StartTime) as varchar(2)),2)

Also, I have an article out there on How to Build your own SQL calendar table to perform complex date expressions.  If you had a calendar table with this YYYY-MM value as a column, to do an UPDATE on 77 million rows all you'd need to do is JOIN on the date, and not to perform the expression 77 million times.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 1200 total points
ID: 40342936
Period is varchar, right?  Then that UPDATE almost certainly requires every page to be split: yikes, yes, that will take a lot of time.

Just add a computed column for Period that generates the YYYY-DD format you need, but don't actually store it.  (Alternatively, you could pre-pad the Period column with '???????'; then, when you UPDATEd it, the page wouldn't have to split, and it would be much faster, although still not as fast as using a computed column.)


ALTER TABLE #sometable
ADD Period AS CONVERT(varchar(7), StartTime, 120)
0
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 800 total points
ID: 40343725
IF using SQL Server 2012 upward

    format(@StartTime ,'yyyy-MM')

otherwise:
    convert(varchar(7), StartTime ,120)    -- NB same as Scott above


Does this field get indexed?

if yes: why not use an integer instead?
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

771 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