Perl errors while trying to replace words in a large file - perl

I have a huge 1.1 GB File. It contains only 1 line and 1167889705 characters. I want to replace all the occurences of "LFENT" by "\nLFENT".
I am using
perl 5, version 18, subversion 1 (v5.18.1) built for x86_64-linux –
Perl Script
perl -ne 's/LFENT/\nLFENT/g; print;' queries.txt
Sample
22 serialization::archive 10 0 0 0 0 134477 0 0 0 9 LFENT_1_1 6 LFDOC1 20 documents/LFDOC1.xml 6 PERSON 12 barack obama 0 0 1 0 12 barack obama 0 0 6 LFDOC1 0 76 127 0.11599133 151 0.11042373 331 0.057491675 389 0.05621225 396 0.065223008 398 0.12416559 421 0.10331947 502 0.055213902 641 0.08908961 642 0.084330745 690 0.11456303 699 0.16876614 720 0.080576144 791 0.077269703 792 0.077503696 849 0.08224529 854 0.074629158 1028 0.11078026 1039 0.091981322 1300 0.070129424 1359 0.06072164 1390 0.05564769 1424 0.076605573 1461 0.15357952 1467 0.083702087 1583 0.069745384 1960 0.088143557 2118 0.064093992 2192 0.054430917 2304 0.089718938 2417 0.10173298 2436 0.069064997 2683 0.12296619 2700 0.068218805 2715 0.14005794 2786 0.084942013 3016 0.08474879 3113 0.083180025 3183 0.07413847 3202 0.081254445 3351 0.21241546 3587 0.061469007 3711 0.068944298 3949 0.10349752 5284 0.087500088 5775 0.092185706 5816 0.20625457 6320 0.11784423 6435 0.076852165 6794 0.094336778 6805 0.063363358 6946 0.11133534 7277 0.074097082 7588 0.12458359 7590 0.099151991 7629 0.28773388 7790 0.1194713 8137 0.11009126 8214 0.10754209 8889 0.12056628 9326 0.13841057 9701 0.25610805 9863 0.11877144 9908 0.11499001 10500 0.12205271 11917 0.11144774 12750 0.11285295 13453 0.13751692 19236 0.12948024 20390 0.12416928 20795 0.11188922 23777 0.13165452 23887 0.10065161 27644 0.1620737 73361 0.16295943 75521 0.17926371 9 LFENT_1_2 6 LFDOC1 20 documents/LFDOC1.xml 6 PERSON 5 obama 2 0 12 barack obama 5 obama 6 LFDOC1 0 76 127 0.11599133 151 0.11042373 331 0.057491675 389 0.05621225 396 0.065223008 398 0.12416559 421 0.10331947 502 0.055213902 641 0.08908961 642 0.084330745 690 0.11456303 699 0.16876614 720 0.080576144 791 0.077269703 792 0.077503696 849 0.08224529 854 0.074629158 1028 0.11078026 1039 0.091981322 1300 0.070129424 1359 0.06072164 1390 0.05564769 1424 0.076605573 1461 0.15357952 1467 0.083702087 1583 0.069745384 1960 0.088143557 2118 0.064093992 2192 0.054430917 2304 0.089718938 2417 0.10173298 2436 0.069064997 2683 0.12296619 2700 0.068218805 2715 0.14005794 2786 0.084942013 3016 0.08474879 3113 0.083180025 3183 0.07413847 3202 0.081254445 3351 0.21241546 3587 0.061469007 3711 0.068944298 3949 0.10349752 5284 0.087500088 5775 0.092185706 5816 0.20625457 6320 0.11784423 6435 0.076852165 6794 0.094336778 6805 0.063363358 6946 0.11133534 7277 0.074097082 7588 0.12458359 7590 0.099151991 7629 0.28773388 7790 0.1194713 8137 0.11009126 8214 0.10754209 8889 0.12056628 9326 0.13841057 9701 0.25610805 9863 0.11877144 9908 0.11499001 10500 0.12205271 11917 0.11144774 12750 0.11285295 13453 0.13751692 19236 0.12948024 20390 0.12416928 20795 0.11188922 23777 0.13165452 23887 0.10065161 27644 0.1620737 73361 0.16295943 75521 0.17926371 9 LFENT_1_3 6 LFDOC1 20 documents/LFDOC1.xml 4 MISC 8 american 1 0 8 american 6 LFDOC1 0 76 127 0.11599133 151 0.11042373 331 0.057491675 389 0.05621225 396 0.065223008 398 0.12416559 421 0.10331947 502 0.055213902 641 0.08908961 642 0.084330745 690 0.11456303 699 0.16876614 720 0.080576144 791 0.077269703 792 0.077503696 849 0.08224529 854 0.074629158 1028 0.11078026 1039 0.091981322 1300 0.070129424 1359 0.06072164 1390 0.05564769 1424 0.076605573 1461 0.15357952 1467 0.083702087 1583 0.069745384 1960 0.088143557 2118 0.064093992 2192 0.054430917 2304 0.089718938 2417 0.10173298 2436 0.069064997 2683 0.12296619 2700 0.068218805 2715 0.14005794 2786 0.084942013 3016 0.08474879 3113 0.083180025 3183 0.07413847 3202 0.081254445 3351 0.21241546 3587 0.061469007 3711 0.068944298 3949 0.10349752 5284 0.087500088 5775 0.092185706 5816 0.20625457 6320 0.11784423 6435 0.076852165 6794 0.094336778 6805 0.063363358 6946 0.11133534 7277 0.074097082 7588 0.12458359 7590 0.099151991 7629 0.28773388 7790 0.1194713 8137 0.11009126 8214 0.10754209 8889 0.12056628 9326 0.13841057 9701 0.25610805 9863 0.11877144 9908 0.11499001 10500 0.12205271 11917 0.11144774 12750 0.11285295 13453 0.13751692 19236
Problem
The script will work on the sample that I posted. But when I try it on the whole line I get the following error:
Substitution loop at -e line 1, <> line 1.

It is simplest to use LFENT as the record separator. That reduces the blocks to a more manageable size and ensures that there is no more than one LFENT to be replaced in each block
Command-line perl allows for setting the record separator with the -0 modifier, but the value of the parameter must be the code point in octal of a single character, and you can't specify a string. So here you have to set it once in a BEGIN block
Like this
perl -p -e'BEGIN{$/="LFENT"} s/LFENT/\nLFENT/'
output
22 serialization::archive 10 0 0 0 0 134477 0 0 0 9
LFENT_1_1 6 LFDOC1 20 documents/LFDOC1.xml 6 PERSON 12 barack obama 0 0 1 0 12 barack obama 0 0 6 LFDOC1 0 76 127 0.11599133 151 0.11042373 331 0.057491675 389 0.05621225 396 0.065223008 398 0.12416559 421 0.10331947 502 0.055213902 641 0.08908961 642 0.084330745 690 0.11456303 699 0.16876614 720 0.080576144 791 0.077269703 792 0.077503696 849 0.08224529 854 0.074629158 1028 0.11078026 1039 0.091981322 1300 0.070129424 1359 0.06072164 1390 0.05564769 1424 0.076605573 1461 0.15357952 1467 0.083702087 1583 0.069745384 1960 0.088143557 2118 0.064093992 2192 0.054430917 2304 0.089718938 2417 0.10173298 2436 0.069064997 2683 0.12296619 2700 0.068218805 2715 0.14005794 2786 0.084942013 3016 0.08474879 3113 0.083180025 3183 0.07413847 3202 0.081254445 3351 0.21241546 3587 0.061469007 3711 0.068944298 3949 0.10349752 5284 0.087500088 5775 0.092185706 5816 0.20625457 6320 0.11784423 6435 0.076852165 6794 0.094336778 6805 0.063363358 6946 0.11133534 7277 0.074097082 7588 0.12458359 7590 0.099151991 7629 0.28773388 7790 0.1194713 8137 0.11009126 8214 0.10754209 8889 0.12056628 9326 0.13841057 9701 0.25610805 9863 0.11877144 9908 0.11499001 10500 0.12205271 11917 0.11144774 12750 0.11285295 13453 0.13751692 19236 0.12948024 20390 0.12416928 20795 0.11188922 23777 0.13165452 23887 0.10065161 27644 0.1620737 73361 0.16295943 75521 0.17926371 9
LFENT_1_2 6 LFDOC1 20 documents/LFDOC1.xml 6 PERSON 5 obama 2 0 12 barack obama 5 obama 6 LFDOC1 0 76 127 0.11599133 151 0.11042373 331 0.057491675 389 0.05621225 396 0.065223008 398 0.12416559 421 0.10331947 502 0.055213902 641 0.08908961 642 0.084330745 690 0.11456303 699 0.16876614 720 0.080576144 791 0.077269703 792 0.077503696 849 0.08224529 854 0.074629158 1028 0.11078026 1039 0.091981322 1300 0.070129424 1359 0.06072164 1390 0.05564769 1424 0.076605573 1461 0.15357952 1467 0.083702087 1583 0.069745384 1960 0.088143557 2118 0.064093992 2192 0.054430917 2304 0.089718938 2417 0.10173298 2436 0.069064997 2683 0.12296619 2700 0.068218805 2715 0.14005794 2786 0.084942013 3016 0.08474879 3113 0.083180025 3183 0.07413847 3202 0.081254445 3351 0.21241546 3587 0.061469007 3711 0.068944298 3949 0.10349752 5284 0.087500088 5775 0.092185706 5816 0.20625457 6320 0.11784423 6435 0.076852165 6794 0.094336778 6805 0.063363358 6946 0.11133534 7277 0.074097082 7588 0.12458359 7590 0.099151991 7629 0.28773388 7790 0.1194713 8137 0.11009126 8214 0.10754209 8889 0.12056628 9326 0.13841057 9701 0.25610805 9863 0.11877144 9908 0.11499001 10500 0.12205271 11917 0.11144774 12750 0.11285295 13453 0.13751692 19236 0.12948024 20390 0.12416928 20795 0.11188922 23777 0.13165452 23887 0.10065161 27644 0.1620737 73361 0.16295943 75521 0.17926371 9
LFENT_1_3 6 LFDOC1 20 documents/LFDOC1.xml 4 MISC 8 american 1 0 8 american 6 LFDOC1 0 76 127 0.11599133 151 0.11042373 331 0.057491675 389 0.05621225 396 0.065223008 398 0.12416559 421 0.10331947 502 0.055213902 641 0.08908961 642 0.084330745 690 0.11456303 699 0.16876614 720 0.080576144 791 0.077269703 792 0.077503696 849 0.08224529 854 0.074629158 1028 0.11078026 1039 0.091981322 1300 0.070129424 1359 0.06072164 1390 0.05564769 1424 0.076605573 1461 0.15357952 1467 0.083702087 1583 0.069745384 1960 0.088143557 2118 0.064093992 2192 0.054430917 2304 0.089718938 2417 0.10173298 2436 0.069064997 2683 0.12296619 2700 0.068218805 2715 0.14005794 2786 0.084942013 3016 0.08474879 3113 0.083180025 3183 0.07413847 3202 0.081254445 3351 0.21241546 3587 0.061469007 3711 0.068944298 3949 0.10349752 5284 0.087500088 5775 0.092185706 5816 0.20625457 6320 0.11784423 6435 0.076852165 6794 0.094336778 6805 0.063363358 6946 0.11133534 7277 0.074097082 7588 0.12458359 7590 0.099151991 7629 0.28773388 7790 0.1194713 8137 0.11009126 8214 0.10754209 8889 0.12056628 9326 0.13841057 9701 0.25610805 9863 0.11877144 9908 0.11499001 10500 0.12205271 11917 0.11144774 12750 0.11285295 13453 0.13751692 19236[Finished in 0.2s]
By the way, it is unlikely to cause a problem but LFENT is always preceded by a space character in your example data. You may want to remove it by using s/\s*LFENT/\nLFENT for your substitution
And a look-ahead might be more appropriate too s/\s*(?=LFENT)/\n/ so that you don't have to remove and replace the unchanged part of the match

Try updating your perl to a more current version.
This was a known bug for a long time (fixed in v5.22.0): perl #123071: substitution loop issue with long strings
You can do so with the following commands (assuming you are on linux):
wget http://www.cpan.org/src/5.0/perl-5.22.1.tar.gz
tar -xzf perl-5.22.1.tar.gz
cd perl-5.22.1
./Configure -des -Dprefix=$HOME/localperl
make
make test
make install
Then test it by running $HOME/localperl/bin/perl -v and add it to your PATH using this: export PATH=$HOME/localperl/bin/:$PATH.

Related

Why are these two IMAP Search results different from one another?

I'm using imaplib, and trying to fetch emails with a certain subject header value.
My code:
res, tmp = self.mail.uid('search', None, 'HEADER Subject "SUBJECT_HERE"')
print(tmp)
print(res)
print("test 2 goes:")
rr, tt = self.mail.search(None, 'HEADER Subject "SUBJECT_HERE"')
print(tt)
print(rr)
Result:
[b'225 232 323 324 346 366 382 419 420 425 450 463 517 607 670 751 833
911 1043 1129 1133 1134 1287 1350 1799 1854 1957 1960 1962 1991 2005
2040 2071 2110 2119 2121 2153 2158 2182 2188 2189 2228 2230 2239 2249
2334 2335 2372 2378 2396 2435 2497 2567 2568 2573 2574 2575 2632 2633
2634 2648 2649 2709 2785 2819 2821 2828 2829 2868 2885 2895 2902 2906
2920 2993 2997 2998 3000 3001 3009'] OK test 2 goes:
[b'220 227 318 319 340 360 376 413 414 419 444 457 511 601 664 745 827
905 1037 1123 1127 1128 1281 1344 1793 1848 1951 1954 1956 1985 1999
2034 2065 2104 2113 2115 2147 2152 2176 2182 2183 2222 2224 2233 2243
2328 2329 2366 2372 2390 2429 2491 2561 2562 2567 2568 2569 2625 2626
2627 2641 2642 2702 2778 2812 2814 2821 2822 2861 2878 2888 2895 2899
2913 2986 2990 2991 2993 2994 3002'] OK
I thought that those two commands result in the same results.
But as shown above, those two fetch different emails.
What is the difference??
One (SEARCH) returns message sequence numbers (MSN), which are numbered from 1 to N and change as messages are added and deleted. A message that is number 5 now could be number 4 tomorrow if you delete a message before it.
The other (UID SEARCH) returns UIDs, which do not change as messages are deleted. They're two completely different set of identifiers. A message with UID 5 will remain UID 5 until it is deleted it (or moved, etc.).
The UID will never reused in that incarnation of a folder. If the folder is deleted and recreated, or the mail server is rebuilt, the folder's UIDVALIDITY should change so you can detect that your cache is no longer valid.

GIMP get current image reference number to operate on current image

I have more than one image open, and I'd like to run scrip-fu functions on the currently selected image. How can I get the image reference number to the currently selected image so I can use the reference in other functions (e.g. (gimp-image-width 7))? I'm using GIMP 2.10.12.
Using (gimp-image-list) lists all the image references:
> (gimp-image-list)
(12 #(12 11 10 9 8 7 6 5 4 3 2 1))
I've been using (gimp-image-latest) for this, which should work for the general case
(define (gimp-image-list-items)
(cadr (gimp-image-list)))
(define (gimp-image-latest)
(aref (gimp-image-list-items) 0))
(define img (gimp-image-latest))
Then it's in img. E.g.:
> (gimp-image-get-layers img)
(283 #(770 769 768 767 766 765 764 763 762 761 760 759 758 757 756 755 754 753 752 751 750 749 748 747 746 745 744 743 742 741 740 739 738 737 736 735 734 733 732 731 730 729 728 727 726 725 724 723 722 721 720 719 718 717 716 715 714 713 712 711 710 709 708 707 706 705 704 703 702 701 700 699 698 697 696 695 694 693 692 691 690 689 688 687 686 685 684 683 682 681 680 679 678 677 676 675 674 673 672 671 670 669 668 667 666 665 664 663 662 661 660 659 658 657 656 655 654 653 652 651 650 649 648 647 646 645 644 643 642 641 640 639 638 637 636 635 634 633 632 631 630 629 628 627 626 625 624 623 622 621 620 619 618 617 616 615 614 613 612 611 610 609 608 607 606 605 604 603 602 601 600 599 598 597 596 595 594 593 592 591 590 589 588 587 586 585 584 583 582 581 580 579 578 577 576 575 574 573 572 571 570 569 568 567 566 565 564 563 562 561 560 559 558 557 556 555 554 553 552 551 550 549 548 547 546 545 544 543 542 541 540 539 538 537 536 535 534 533 532 531 530 529 528 527 526 525 524 523 522 521 520 519 518 517 516 515 514 513 512 511 510 509 508 507 506 505 504 503 502 501 500 499 498 497 496 495 494 493 492 491 490 489 488))

MATLAB Polygon Self Intersection - Delete Zero Volume Parts

I have a MATLAB polygon (below) which self intersects to make the zero volume part (which I will call a sliver) in the image (also below).
I am having a lot of trouble finding and removing the zero volume sliver.
Hope anyone can help. Thanks.
IMAGE:
enter image description here
Polygon:
newBoundary =
1 1
1 216
8 221
25 239
46 255
60 269
70 282
81 296
92 313
113 323
127 326
145 336
163 349
170 368
174 374
192 388
209 400
229 416
236 433
252 450
266 470
268 488
274 504
284 523
274 518
249 513
246 532
246 538
224 527
216 541
206 554
205 573
192 589
185 590
169 609
148 620
154 641
170 646
172 660
165 683
154 673
140 654
130 648
116 656
105 660
92 679
82 700
69 719
90 730
77 751
87 770
66 763
48 774
37 791
28 804
18 804
9 817
1 834
1 838
1 1049
131 1049
141 1044
164 1029
181 1014
193 1004
208 995
221 979
239 979
239 962
253 951
270 940
290 926
299 908
317 899
332 879
353 869
364 857
379 841
376 856
386 870
383 889
380 913
382 933
383 958
378 975
379 993
378 1016
374 1029
367 1040
366 1049
616 1049
615 1049
610 1037
615 1016
612 993
618 978
612 953
609 936
606 913
608 894
609 874
606 854
622 839
633 853
651 869
670 883
690 900
706 916
721 934
740 951
747 971
765 978
785 992
795 1007
816 1015
831 1028
848 1049
1049 1049
1049 781
1027 784
1003 780
997 798
974 812
973 796
964 780
947 765
930 773
922 760
926 736
917 722
898 713
879 719
883 704
886 679
873 663
853 670
847 649
841 627
839 607
835 621
814 631
794 620
778 604
761 588
744 601
729 592
716 577
705 556
698 537
689 517
698 502
711 486
726 486
730 463
743 446
760 439
764 420
784 404
802 388
816 373
837 366
846 344
866 337
881 324
901 314
912 299
926 283
946 272
951 253
970 242
977 225
998 223
1006 207
1025 192
1049 186
1049 1
789 1
792 6
778 20
774 37
762 49
753 70
736 79
715 90
695 100
672 91
668 109
663 131
668 153
651 156
657 168
648 170
633 153
612 150
613 127
610 114
613 97
615 74
613 54
612 36
613 21
620 1
380 1
379 9
377 34
371 56
379 78
387 98
379 117
376 136
384 151
372 156
350 164
332 174
318 168
327 150
309 139
299 129
281 127
290 108
275 107
261 90
236 76
235 60
219 52
206 35
187 22
168 4
156 1
1 1
Zero volume slivers may occur when there are three or more consecutive points aligned. In this particular case those were:
366 1049
616 1049
615 1049
since they fall on the same horizontal line.
If you want to automate this, you should check every group of three consecutive points to see if they fall on the same line. If they do, you need to discard the one in the middle, according to appearance order.
In this particular case, the one in the middle is:
616 1049

Exception when trying to refresh Clojure code in cider

I am using clojure in Emacs with cider and the cider repl (0.7.0). This is pretty fine, but whenever I run cider-referesh (or hit C-c C-x), I get an exception:
ClassNotFoundException clojure.tools.namespace.repl java.net.URLClassLoader$1.run (URLClassLoader.java:372)
1. Unhandled java.lang.ClassNotFoundException
clojure.tools.namespace.repl
URLClassLoader.java: 372 java.net.URLClassLoader$1/run
URLClassLoader.java: 361 java.net.URLClassLoader$1/run
AccessController.java: -2 java.security.AccessController/doPrivileged
URLClassLoader.java: 360 java.net.URLClassLoader/findClass
DynamicClassLoader.java: 61 clojure.lang.DynamicClassLoader/findClass
ClassLoader.java: 424 java.lang.ClassLoader/loadClass
ClassLoader.java: 357 java.lang.ClassLoader/loadClass
Class.java: -2 java.lang.Class/forName0
Class.java: 340 java.lang.Class/forName
RT.java: 2065 clojure.lang.RT/classForName
Compiler.java: 978 clojure.lang.Compiler$HostExpr/maybeClass
Compiler.java: 756 clojure.lang.Compiler$HostExpr/access$400
Compiler.java: 6583 clojure.lang.Compiler/macroexpand1
Compiler.java: 6613 clojure.lang.Compiler/macroexpand
Compiler.java: 6687 clojure.lang.Compiler/eval
Compiler.java: 6666 clojure.lang.Compiler/eval
core.clj: 2927 clojure.core/eval
main.clj: 239 clojure.main/repl/read-eval-print/fn
main.clj: 239 clojure.main/repl/read-eval-print
main.clj: 257 clojure.main/repl/fn
main.clj: 257 clojure.main/repl
RestFn.java: 1096 clojure.lang.RestFn/invoke
interruptible_eval.clj: 56 clojure.tools.nrepl.middleware.interruptible-eval/evaluate/fn
AFn.java: 152 clojure.lang.AFn/applyToHelper
AFn.java: 144 clojure.lang.AFn/applyTo
core.clj: 624 clojure.core/apply
core.clj: 1862 clojure.core/with-bindings*
RestFn.java: 425 clojure.lang.RestFn/invoke
interruptible_eval.clj: 41 clojure.tools.nrepl.middleware.interruptible-eval/evaluate
interruptible_eval.clj: 171 clojure.tools.nrepl.middleware.interruptible-eval/interruptible-eval/fn/fn
core.clj: 2402 clojure.core/comp/fn
interruptible_eval.clj: 138 clojure.tools.nrepl.middleware.interruptible-eval/run-next/fn
AFn.java: 22 clojure.lang.AFn/run
ThreadPoolExecutor.java: 1142 java.util.concurrent.ThreadPoolExecutor/runWorker
ThreadPoolExecutor.java: 617 java.util.concurrent.ThreadPoolExecutor$Worker/run
Thread.java: 745 java.lang.Thread/run
What is the reason for this, and how can I fix it?
It seems that this exception was a bug, that has now been fixed in cider.
Try adding [org.clojure/tools.namespace "0.2.5"] to your project.clj

To get the Id's between two dates

I have a table
WEEK_ID WEEK_STARTDATE WEEK_YEAR WEEK_MONTH WEEK_CREATEDTS
252 10/26/2008 2008 11 2008-10-07 15:10:00.000
253 11/02/2008 2008 11 2008-10-07 15:10:00.000
254 11/09/2008 2008 11 2008-10-07 15:10:00.000
255 11/16/2008 2008 11 2008-10-07 15:10:00.000
256 11/23/2008 2008 12 2008-10-07 15:10:00.000
257 11/30/2008 2008 12 2008-10-07 15:10:00.000
258 12/07/2008 2008 12 2008-10-07 15:10:00.000
259 12/14/2008 2008 12 2008-10-07 15:10:00.000
260 12/21/2008 2008 12 2008-10-07 15:10:00.000
266 2/1/2009 2009 2 2008-10-07 15:16:00.000
267 02/08/2009 2009 2 2008-10-07 15:16:00.000
268 02/15/2009 2009 2 2008-10-07 15:16:00.000
269 02/22/2009 2009 2 2008-10-07 15:16:00.000
270 3/1/2009 2009 3 2008-10-07 15:17:00.000
271 03/08/2009 2009 3 2008-10-07 15:17:00.000
272 03/15/2009 2009 3 2008-10-07 15:17:00.000
273 03/22/2009 2009 3 2008-10-07 15:17:00.000
274 3/29/2009 2009 4 2008-10-07 15:19:00.000
275 04/05/2009 2009 4 2008-10-07 15:19:00.000
276 04/12/2009 2009 4 2008-10-07 15:19:00.000
277 04/19/2009 2009 4 2008-10-07 15:19:00.000
278 04/26/2009 2009 4 2008-10-07 15:19:00.000
279 5/3/2009 2009 5 2008-10-07 15:20:00.000
280 05/10/2009 2009 5 2008-10-07 15:20:00.000
281 05/17/2009 2009 5 2008-10-07 15:20:00.000
282 05/24/2009 2009 5 2008-10-07 15:20:00.000
283 5/31/2009 2009 6 2008-10-07 15:34:00.000
284 06/07/2009 2009 6 2008-10-07 15:34:00.000
285 06/14/2009 2009 6 2008-10-07 15:34:00.000
286 06/21/2009 2009 6 2008-10-07 15:34:00.000
287 6/28/2009 2009 7 2008-10-07 15:36:00.000
288 07/05/2009 2009 7 2008-10-07 15:36:00.000
289 07/12/2009 2009 7 2008-10-07 15:36:00.000
290 07/19/2009 2009 7 2008-10-07 15:36:00.000
291 07/26/2009 2009 7 2008-10-07 15:36:00.000
297 12/28/2008 2009 1 2008-10-08 03:45:00.000
298 01/04/2009 2009 1 2008-10-08 03:45:00.000
299 01/11/2009 2009 1 2008-10-08 03:45:00.000
300 01/18/2009 2009 1 2008-10-08 03:45:00.000
301 01/25/2009 2009 1 2008-10-08 03:45:00.000
302 8/2/2009 2009 8 2008-10-28 12:52:00.000
303 08/09/2009 2009 8 2008-10-28 12:52:00.000
304 08/16/2009 2009 8 2008-10-28 12:52:00.000
305 08/23/2009 2009 8 2008-10-28 12:52:00.000
306 8/30/2009 2009 9 2008-10-28 12:54:00.000
307 09/06/2009 2009 9 2008-10-28 12:54:00.000
308 09/13/2009 2009 9 2008-10-28 12:54:00.000
309 09/20/2009 2009 9 2008-10-28 12:54:00.000
310 9/27/2009 2009 10 2008-10-28 12:54:00.000
311 10/04/2009 2009 10 2008-10-28 12:54:00.000
312 10/11/2009 2009 10 2008-10-28 12:54:00.000
313 10/18/2009 2009 10 2008-10-28 12:54:00.000
314 10/25/2009 2009 10 2008-10-28 12:54:00.000
315 11/1/2009 2009 11 2008-10-28 12:55:00.000
316 11/08/2009 2009 11 2008-10-28 12:55:00.000
317 11/15/2009 2009 11 2008-10-28 12:55:00.000
318 11/22/2009 2009 11 2008-10-28 12:55:00.000
319 11/29/2009 2009 12 2008-10-28 12:55:00.000
320 12/06/2009 2009 12 2008-10-28 12:55:00.000
321 12/13/2009 2009 12 2008-10-28 12:55:00.000
322 12/20/2009 2009 12 2008-10-28 12:55:00.000
323 12/28/2009 2010 1 2008-10-28 12:56:00.000
324 01/04/2010 2010 1 2008-10-28 12:56:00.000
325 01/11/2010 2010 1 2008-10-28 12:56:00.000
326 01/18/2010 2010 1 2008-10-28 12:56:00.000
327 01/25/2010 2010 1 2008-10-28 12:56:00.000
329 02/01/2010 2010 2 2010-01-18 08:10:00.000
330 02/08/2010 2010 2 2010-01-18 08:10:00.000
331 02/15/2010 2010 2 2010-01-18 08:10:00.000
332 02/22/2010 2010 2 2010-01-18 08:10:00.000
333 03/01/2010 2010 3 2010-01-18 08:11:00.000
334 03/08/2010 2010 3 2010-01-18 08:11:00.000
335 03/15/2010 2010 3 2010-01-18 08:11:00.000
336 03/22/2010 2010 3 2010-01-18 08:11:00.000
337 03/29/2010 2010 4 2010-03-17 11:00:00.000
338 04/05/2010 2010 4 2010-03-17 11:00:00.000
339 04/12/2010 2010 4 2010-03-17 11:00:00.000
340 04/19/2010 2010 4 2010-03-17 11:00:00.000
341 04/26/2010 2010 4 2010-03-17 11:00:00.000
342 05/03/2010 2010 5 2010-03-17 11:01:00.000
343 05/10/2010 2010 5 2010-03-17 11:01:00.000
344 05/17/2010 2010 5 2010-03-17 11:01:00.000
345 05/24/2010 2010 5 2010-03-17 11:01:00.000
346 05/31/2010 2010 6 2010-03-17 11:03:00.000
347 06/07/2010 2010 6 2010-03-17 11:03:00.000
348 06/14/2010 2010 6 2010-03-17 11:03:00.000
349 06/21/2010 2010 6 2010-03-17 11:03:00.000
350 06/28/2010 2010 7 2010-03-17 11:03:00.000
351 07/05/2010 2010 7 2010-03-17 11:03:00.000
352 07/12/2010 2010 7 2010-03-17 11:03:00.000
353 07/19/2010 2010 7 2010-03-17 11:03:00.000
354 07/26/2010 2010 7 2010-03-17 11:03:00.000
355 08/02/2010 2010 8 2010-03-17 11:04:00.000
356 08/09/2010 2010 8 2010-03-17 11:04:00.000
357 08/16/2010 2010 8 2010-03-17 11:04:00.000
358 08/23/2010 2010 8 2010-03-17 11:04:00.000
359 08/30/2010 2010 9 2010-03-17 11:05:00.000
360 09/06/2010 2010 9 2010-03-17 11:05:00.000
361 09/13/2010 2010 9 2010-03-17 11:05:00.000
362 09/20/2010 2010 9 2010-03-17 11:05:00.000
363 09/27/2010 2010 10 2010-03-17 11:06:00.000
364 10/04/2010 2010 10 2010-03-17 11:06:00.000
365 10/11/2010 2010 10 2010-03-17 11:06:00.000
366 10/18/2010 2010 10 2010-03-17 11:06:00.000
367 10/25/2010 2010 10 2010-03-17 11:06:00.000
368 11/01/2010 2010 11 2010-03-17 11:09:00.000
369 11/08/2010 2010 11 2010-03-17 11:09:00.000
370 11/15/2010 2010 11 2010-03-17 11:09:00.000
371 11/22/2010 2010 11 2010-03-17 11:09:00.000
372 11/29/2010 2010 12 2010-03-17 11:10:00.000
373 12/06/2010 2010 12 2010-03-17 11:10:00.000
374 12/13/2010 2010 12 2010-03-17 11:10:00.000
375 12/20/2010 2010 12 2010-03-17 11:10:00.000
376 12/27/2010 2011 1 2010-03-17 11:11:00.000
377 01/03/2011 2011 1 2010-03-17 11:11:00.000
378 01/10/2011 2011 1 2010-03-17 11:11:00.000
379 01/17/2011 2011 1 2010-03-17 11:11:00.000
380 01/24/2011 2011 1 2010-03-17 11:11:00.000
381 01/31/2011 2011 2 NULL
382 02/07/2011 2011 2 NULL
383 02/14/2011 2011 2 NULL
384 02/21/2011 2011 2 NULL
441 02/28/2011 2011 3 NULL
442 03/07/2011 2011 3 NULL
443 03/14/2011 2011 3 NULL
444 03/21/2011 2011 3 NULL
451 03/28/2011 2011 4 NULL
452 04/04/2011 2011 4 NULL
453 04/11/2011 2011 4 NULL
454 04/18/2011 2011 4 NULL
455 04/25/2011 2011 4 NULL
456 05/02/2011 2011 5 NULL
457 05/09/2011 2011 5 NULL
458 05/16/2011 2011 5 NULL
459 05/23/2011 2011 5 NULL
460 05/30/2011 2011 6 NULL
461 06/06/2011 2011 6 NULL
462 06/13/2011 2011 6 NULL
463 06/20/2011 2011 6 NULL
464 06/27/2011 2011 7 NULL
465 07/04/2011 2011 7 NULL
466 07/11/2011 2011 7 NULL
467 07/18/2011 2011 7 NULL
468 07/25/2011 2011 7 NULL
469 08/01/2011 2011 8 NULL
470 08/08/2011 2011 8 NULL
471 08/15/2011 2011 8 NULL
472 08/22/2011 2011 8 NULL
473 08/29/2011 2011 9 NULL
474 09/05/2011 2011 9 NULL
475 09/12/2011 2011 9 NULL
476 09/19/2011 2011 9 NULL
477 09/26/2011 2011 10 NULL
478 10/03/2011 2011 10 NULL
479 10/10/2011 2011 10 NULL
480 10/17/2011 2011 10 NULL
481 10/24/2011 2011 10 NULL
482 10/31/2011 2011 11 NULL
483 11/07/2011 2011 11 NULL
484 11/14/2011 2011 11 NULL
485 11/21/2011 2011 11 NULL
486 11/28/2011 2011 12 NULL
487 12/05/2011 2011 12 NULL
488 12/12/2011 2011 12 NULL
489 12/19/2011 2011 12 NULL
491 12/26/2011 2012 1 NULL
492 01/02/2012 2012 1 NULL
493 01/09/2012 2012 1 NULL
494 01/16/2012 2012 1 NULL
495 01/23/2012 2012 1 NULL
496 01/30/2012 2012 2 NULL
497 02/06/2012 2012 2 NULL
498 02/13/2012 2012 2 NULL
499 02/20/2012 2012 2 NULL
500 02/27/2012 2012 3 NULL
501 03/05/2012 2012 3 NULL
502 03/12/2012 2012 3 NULL
503 03/19/2012 2012 3 NULL
504 03/26/2012 2012 4 NULL
505 04/02/2012 2012 4 NULL
506 04/09/2012 2012 4 NULL
507 04/16/2012 2012 4 NULL
508 04/23/2012 2012 4 NULL
509 04/30/2012 2012 5 NULL
510 05/07/2012 2012 5 NULL
511 05/14/2012 2012 5 NULL
512 05/21/2012 2012 5 NULL
513 05/28/2012 2012 6 NULL
514 06/04/2012 2012 6 NULL
515 06/11/2012 2012 6 NULL
516 06/18/2012 2012 6 NULL
517 06/25/2012 2012 7 NULL
518 07/02/2012 2012 7 NULL
519 07/09/2012 2012 7 NULL
520 07/16/2012 2012 7 NULL
521 07/23/2012 2012 7 NULL
522 07/30/2012 2012 8 NULL
523 08/06/2012 2012 8 NULL
524 08/13/2012 2012 8 NULL
525 08/20/2012 2012 8 NULL
526 08/27/2012 2012 9 NULL
527 09/03/2012 2012 9 NULL
528 09/10/2012 2012 9 NULL
529 09/17/2012 2012 9 NULL
530 09/24/2012 2012 10 NULL
531 10/01/2012 2012 10 NULL
532 10/08/2012 2012 10 NULL
533 10/15/2012 2012 10 NULL
534 10/22/2012 2012 10 NULL
535 10/29/2012 2012 11 NULL
536 11/05/2012 2012 11 NULL
537 11/12/2012 2012 11 NULL
538 11/19/2012 2012 11 NULL
539 11/26/2012 2012 12 NULL
540 12/03/2012 2012 12 NULL
541 12/10/2012 2012 12 NULL
542 12/17/2012 2012 12 NULL
543 12/24/2012 2013 1 NULL
544 12/31/2012 2013 1 NULL
545 01/07/2013 2013 1 NULL
546 01/14/2013 2013 1 NULL
547 01/21/2013 2013 1 NULL
548 01/28/2012 2013 1 NULL
and I create a query by adding 7 days to week_startdate
select
week_id, week_startdate,
convert(varchar, dateadd(day, 7, week_startdate), 101) as week_enddate
from dbo.TBL_S3_FISCALWEEKS_1
and output of the above query is
week_id week_startdate week_enddate
252 10/26/2008 11/02/2008
253 11/02/2008 11/09/2008
254 11/09/2008 11/16/2008
255 11/16/2008 11/23/2008
256 11/23/2008 11/30/2008
257 11/30/2008 12/07/2008
258 12/07/2008 12/14/2008
259 12/14/2008 12/21/2008
260 12/21/2008 12/28/2008
266 2/1/2009 02/08/2009
267 02/08/2009 02/15/2009
268 02/15/2009 02/22/2009
269 02/22/2009 03/01/2009
270 3/1/2009 03/08/2009
271 03/08/2009 03/15/2009
272 03/15/2009 03/22/2009
273 03/22/2009 03/29/2009
274 3/29/2009 04/05/2009
275 04/05/2009 04/12/2009
276 04/12/2009 04/19/2009
277 04/19/2009 04/26/2009
278 04/26/2009 05/03/2009
279 5/3/2009 05/10/2009
280 05/10/2009 05/17/2009
281 05/17/2009 05/24/2009
282 05/24/2009 05/31/2009
283 5/31/2009 06/07/2009
284 06/07/2009 06/14/2009
285 06/14/2009 06/21/2009
286 06/21/2009 06/28/2009
287 6/28/2009 07/05/2009
288 07/05/2009 07/12/2009
289 07/12/2009 07/19/2009
290 07/19/2009 07/26/2009
291 07/26/2009 08/02/2009
297 12/28/2008 01/04/2009
298 01/04/2009 01/11/2009
299 01/11/2009 01/18/2009
300 01/18/2009 01/25/2009
301 01/25/2009 02/01/2009
302 8/2/2009 08/09/2009
303 08/09/2009 08/16/2009
304 08/16/2009 08/23/2009
305 08/23/2009 08/30/2009
306 8/30/2009 09/06/2009
307 09/06/2009 09/13/2009
308 09/13/2009 09/20/2009
309 09/20/2009 09/27/2009
310 9/27/2009 10/04/2009
311 10/04/2009 10/11/2009
312 10/11/2009 10/18/2009
313 10/18/2009 10/25/2009
314 10/25/2009 11/01/2009
315 11/1/2009 11/08/2009
316 11/08/2009 11/15/2009
317 11/15/2009 11/22/2009
318 11/22/2009 11/29/2009
319 11/29/2009 12/06/2009
320 12/06/2009 12/13/2009
321 12/13/2009 12/20/2009
322 12/20/2009 12/27/2009
323 12/28/2009 01/04/2010
324 01/04/2010 01/11/2010
325 01/11/2010 01/18/2010
326 01/18/2010 01/25/2010
327 01/25/2010 02/01/2010
329 02/01/2010 02/08/2010
330 02/08/2010 02/15/2010
331 02/15/2010 02/22/2010
332 02/22/2010 03/01/2010
333 03/01/2010 03/08/2010
334 03/08/2010 03/15/2010
335 03/15/2010 03/22/2010
336 03/22/2010 03/29/2010
337 03/29/2010 04/05/2010
338 04/05/2010 04/12/2010
339 04/12/2010 04/19/2010
340 04/19/2010 04/26/2010
341 04/26/2010 05/03/2010
342 05/03/2010 05/10/2010
343 05/10/2010 05/17/2010
344 05/17/2010 05/24/2010
345 05/24/2010 05/31/2010
346 05/31/2010 06/07/2010
347 06/07/2010 06/14/2010
348 06/14/2010 06/21/2010
349 06/21/2010 06/28/2010
350 06/28/2010 07/05/2010
351 07/05/2010 07/12/2010
352 07/12/2010 07/19/2010
353 07/19/2010 07/26/2010
354 07/26/2010 08/02/2010
355 08/02/2010 08/09/2010
356 08/09/2010 08/16/2010
357 08/16/2010 08/23/2010
358 08/23/2010 08/30/2010
359 08/30/2010 09/06/2010
360 09/06/2010 09/13/2010
361 09/13/2010 09/20/2010
362 09/20/2010 09/27/2010
363 09/27/2010 10/04/2010
364 10/04/2010 10/11/2010
365 10/11/2010 10/18/2010
366 10/18/2010 10/25/2010
367 10/25/2010 11/01/2010
368 11/01/2010 11/08/2010
369 11/08/2010 11/15/2010
370 11/15/2010 11/22/2010
371 11/22/2010 11/29/2010
372 11/29/2010 12/06/2010
373 12/06/2010 12/13/2010
374 12/13/2010 12/20/2010
375 12/20/2010 12/27/2010
376 12/27/2010 01/03/2011
377 01/03/2011 01/10/2011
378 01/10/2011 01/17/2011
379 01/17/2011 01/24/2011
380 01/24/2011 01/31/2011
381 01/31/2011 02/07/2011
382 02/07/2011 02/14/2011
383 02/14/2011 02/21/2011
384 02/21/2011 02/28/2011
441 02/28/2011 03/07/2011
442 03/07/2011 03/14/2011
443 03/14/2011 03/21/2011
444 03/21/2011 03/28/2011
451 03/28/2011 04/04/2011
452 04/04/2011 04/11/2011
453 04/11/2011 04/18/2011
454 04/18/2011 04/25/2011
455 04/25/2011 05/02/2011
456 05/02/2011 05/09/2011
457 05/09/2011 05/16/2011
458 05/16/2011 05/23/2011
459 05/23/2011 05/30/2011
460 05/30/2011 06/06/2011
461 06/06/2011 06/13/2011
462 06/13/2011 06/20/2011
463 06/20/2011 06/27/2011
464 06/27/2011 07/04/2011
465 07/04/2011 07/11/2011
466 07/11/2011 07/18/2011
467 07/18/2011 07/25/2011
468 07/25/2011 08/01/2011
469 08/01/2011 08/08/2011
470 08/08/2011 08/15/2011
471 08/15/2011 08/22/2011
472 08/22/2011 08/29/2011
473 08/29/2011 09/05/2011
474 09/05/2011 09/12/2011
475 09/12/2011 09/19/2011
476 09/19/2011 09/26/2011
477 09/26/2011 10/03/2011
478 10/03/2011 10/10/2011
479 10/10/2011 10/17/2011
480 10/17/2011 10/24/2011
481 10/24/2011 10/31/2011
482 10/31/2011 11/07/2011
483 11/07/2011 11/14/2011
484 11/14/2011 11/21/2011
485 11/21/2011 11/28/2011
486 11/28/2011 12/05/2011
487 12/05/2011 12/12/2011
488 12/12/2011 12/19/2011
489 12/19/2011 12/26/2011
491 12/26/2011 01/02/2012
492 01/02/2012 01/09/2012
493 01/09/2012 01/16/2012
494 01/16/2012 01/23/2012
495 01/23/2012 01/30/2012
496 01/30/2012 02/06/2012
497 02/06/2012 02/13/2012
498 02/13/2012 02/20/2012
499 02/20/2012 02/27/2012
500 02/27/2012 03/05/2012
501 03/05/2012 03/12/2012
502 03/12/2012 03/19/2012
503 03/19/2012 03/26/2012
504 03/26/2012 04/02/2012
505 04/02/2012 04/09/2012
506 04/09/2012 04/16/2012
507 04/16/2012 04/23/2012
508 04/23/2012 04/30/2012
509 04/30/2012 05/07/2012
510 05/07/2012 05/14/2012
511 05/14/2012 05/21/2012
512 05/21/2012 05/28/2012
513 05/28/2012 06/04/2012
514 06/04/2012 06/11/2012
515 06/11/2012 06/18/2012
516 06/18/2012 06/25/2012
517 06/25/2012 07/02/2012
518 07/02/2012 07/09/2012
519 07/09/2012 07/16/2012
520 07/16/2012 07/23/2012
521 07/23/2012 07/30/2012
522 07/30/2012 08/06/2012
523 08/06/2012 08/13/2012
524 08/13/2012 08/20/2012
525 08/20/2012 08/27/2012
526 08/27/2012 09/03/2012
527 09/03/2012 09/10/2012
528 09/10/2012 09/17/2012
529 09/17/2012 09/24/2012
530 09/24/2012 10/01/2012
531 10/01/2012 10/08/2012
532 10/08/2012 10/15/2012
533 10/15/2012 10/22/2012
534 10/22/2012 10/29/2012
535 10/29/2012 11/05/2012
536 11/05/2012 11/12/2012
537 11/12/2012 11/19/2012
538 11/19/2012 11/26/2012
539 11/26/2012 12/03/2012
540 12/03/2012 12/10/2012
541 12/10/2012 12/17/2012
542 12/17/2012 12/24/2012
543 12/24/2012 12/31/2012
544 12/31/2012 01/07/2013
545 01/07/2013 01/14/2013
546 01/14/2013 01/21/2013
547 01/21/2013 01/28/2013
548 01/28/2012 02/04/2012
The thing is I need to get week_ids between two days. I need it in function
or procedure would u please help me out as soon as possible
A simple SELECT WHERE BETWEEN should do the job for you. Pass in the two dates as parameters, and
SELECT week_id FROM TBL_S3_FISCALWEEKS_1
WHERE WeekStartDate BETWEEN #DateParam1 AND #DateParam2
This will work even if the exact date doesn't appear in WeekStartDate
So you want a query or function that returns the week_id if you feed it a date (even if it is a date that falls between your week_startdate and week_enddate), right?
You might want to try something like the following query:
SELECT week_id
FROM weeks
WHERE DATEDIFF(dd, week_startdate, input_date) < 7
AND DATEDIFF(dd, week_startdate, input_date) > 0
Check the MSDN DATEDIFF reference for more information on DATEDIFF.