473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to update DATE column (month only)in a table

I just used the script that someone posted to change just the year in a date field and it worked great. I want to do the samething by just changing the month only
thanks
Jan 8 '08 #1
5 25895
ck9663
2,878 Expert 2GB
I just used the script that someone posted to change just the year in a date field and it worked great. I want to do the samething by just changing the month only
thanks

use dateadd to add (or subtract) specific unit of date/time to a date time field...

for hardcoding the date, fix the month as literal string and use datepart to get the day and year...

this script will force today's month to december (12)

select cast('12/' + cast(datepart(day,getdate()) as char(2)) + '/' + cast(datepart(year,getdate()) as char(4)) as datetime)

-- CK
Jan 8 '08 #2
Thanks for your reply. Sorry i'm pretty new to SQL and dont fully understand your script. Here is the script that i ran to change the year from 07 to 08. I want to run the same script, but change all the months 1,2,3,4,5,6,7,8,910,11,12 to 1 for January.

update orderline
set ord_date='2008-'+RIGHT('0'+MONTH(ord_date),2)+'-'+RIGHT('0'+DAY(ord_date),2)
Jan 8 '08 #3
ck9663
2,878 Expert 2GB
Thanks for your reply. Sorry i'm pretty new to SQL and dont fully understand your script. Here is the script that i ran to change the year from 07 to 08. I want to run the same script, but change all the months 1,2,3,4,5,6,7,8,910,11,12 to 1 for January.

update orderline
set ord_date='2008-'+RIGHT('0'+MONTH(ord_date),2)+'-'+RIGHT('0'+DAY(ord_date),2)

are you trying to change the format of your date? from 2008/01/08 to 2008 January 08 ? i recommend you handle that on the client-side, not in the db-side. if you really have to, format your field into something else, you can keep the datetime field on your table, just change the way your date looks...

here's a good reference


-- CK
Jan 9 '08 #4
No i dont want to change the format. All i want to do is change the month of the date for all records in a table ex:

02/02/2008
03/04/2008
04/12/2008
05/15/2008

I want to leave the date in the same format. I just want to change the months 02,03,04,05 to be 01 thats it so when the script is done it will look like this.

01/02/2008
01/04/2008
01/12/2008
0115/2008

Thanks for your help
Jan 9 '08 #5
ck9663
2,878 Expert 2GB
No i dont want to change the format. All i want to do is change the month of the date for all records in a table ex:

02/02/2008
03/04/2008
04/12/2008
05/15/2008

I want to leave the date in the same format. I just want to change the months 02,03,04,05 to be 01 thats it so when the script is done it will look like this.

01/02/2008
01/04/2008
01/12/2008
0115/2008

Thanks for your help
try this first:

select mycolumn as fromthis, '01/' + right('0' + cast(datepart(day,mycolumn) as char(2)),2) +
cast(datepart(year,mycolumn) as char(4) as tothis
from mytable

see if your this is what you want..once you run the update, there's not turning back unless you have transaction defined or backup tables...if you're sure about the result, run this:

update mytable
set mycolumn = '01/' + right('0' + cast(datepart(day,mycolumn) as char(2)),2) +
cast(datepart(year,mycolumn) as char(4)


-- CK
Jan 9 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Jeff Kowalczyk | last post by:
I need to adapt this an update statement to a general form that will iterate over multiple orderids for a given customerinvoiceid. My first concern is a form that will work for a given orderid,...
5
by: news.swissonline.ch | last post by:
Hi, DB2 7 FP 13 Windows XP I have a table in which on of the columns has the data type DATE. The date column is indexed (asc non-unique index). Every SQL I create which tries to filter by...
0
by: M Bourgon | last post by:
I am trying to update the contents of table A (I'll use Northwind..Products as an example) with the data from any changed fields in Table B (which is a copy of Northwind..Products, but with some...
11
by: surya | last post by:
hello sir, i have a table emp ,it has three fields one is empno int ,second is ename varchar(20). and last is salary , emp empno ename salary ----------- ------------...
2
by: syntego | last post by:
We commonly use triggers to log changes to our main tables to historical log tables. In the trigger, we create a concatenated string of the old values by casting them as follows: ...
2
by: mayur123creation | last post by:
Hi to All Experts.... I am new in DB side. I need to update all rows for column (fees) in table (MarketValue). This table has around 1500 rows. TB : MarketValue has following colums id - int...
2
by: ssakhamuri | last post by:
hai guys, i want to update the date in a table like i just want to update the year of the of thie date in table for example if rows are like this, 2006-5-14 00:00:00,2005-4-14 00:00:00 then i...
25
by: rekhasc | last post by:
hi...... Its very urgent.............. how can i take only the year and month part and it should save in the database in the month and year field separately......... when i enter the date in the...
1
by: sibusiso | last post by:
HI Can Any one help I have extra field on a table like FDate, FYear, FMonth, FDay, FDatename I have a triger that I will update this field every time transaction hapened, this field must...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.