tsql - How to get a week number from a date in SQL Server 2005 -
I have a date and a week number field for each row in Table A. The date field has already been populated and I was hoping that instead of doing it manually, there will be a way to automatically fill the week number area.
An example of the date is as follows: 20111007
Hope you can help, Cheers :)
With this syntax, you can retrieve the week for a date.
SELECT datepart (week, '20111007'), date format (iso_week, '20111007') Although iso_week only works from sqlserver 2008 though.
You really should consider creating a field of field in a math column. This will make your life so easy (I will use this solution).
This will be your syntax:
Changing the table & lt; Yourtable & gt; If you are prevented from changing the database for any reason, you can use it:
update < Table & gt; SET & lt; Column of the week & gt; = Date format (week, & lt; yourdate & gt;) WHERE week is zero and & lt; Yourdate & gt; The problem is that if the date change, the week will still be the same and the week will not be available until this script is used, unless the week update with the trigger You can also create a view to show all the columns and to add the week as a math field, it will also be a valid solution. Although it is a good practice to list columns instead of using voucher (*), I use it because I do not find your columns.
create v_yourview Select SELECT *, datepart (week, & lt; yourdate & gt;) from week 1 & lt; Table & gt;
Comments
Post a Comment