| Author |
Topic |
|
verybrightstar
Starting Member
16 Posts |
Posted - 2004-03-04 : 01:15:12
|
| hi all , does SQL server able to input the date that is originally from 2/6/2004 11:07:46 AM to DD-MM-YY 06-02-04 and store in the DB. For example , this is the SQL insert queryINSERT INTO ADMIN ( ID , NAME , DATE ) VALUES ( 'A001' , 'karen' ,'2/6/2004 11:07:46 AM')the date will automatically converted to 06-02-04 and store in DB .Does it need to use SQL Rule or user-defined data type.kt |
|
|
Mathias
Posting Yak Master
119 Posts |
Posted - 2004-03-04 : 02:04:15
|
| Yes, you can use this functionALTER function dbo.usf_FormatDate ( @dDate datetime, --Date value to be formatted @sFormat varchar(40)) --Format for date value returns varchar(40) as begin -- Insert the Month -- set @sFormat = replace(@sFormat,'MMMM',datename(month,@dDate)) set @sFormat = replace(@sFormat,'MMM',convert(char(3),datename(month,@dDate))) set @sFormat = replace(@sFormat,'MM',right(convert(char(4),@dDate,12),2)) set @sFormat = replace(@sFormat,'M1',convert(varchar(2),convert(int,right(convert(char(4),@dDate,12),2)))) -- Insert the Day -- set @sFormat = replace(@sFormat,'DDDD',datename(weekday,@dDate)) set @sFormat = replace(@sFormat,'DDD',convert(char(3),datename(weekday,@dDate))) set @sFormat = replace(@sFormat,'DD',right(convert(char(6),@dDate,12),2)) set @sFormat = replace(@sFormat,'D1',convert(varchar(2),convert(int,right(convert(char(6),@dDate,12),2)))) -- Insert the Year -- set @sFormat = replace(@sFormat,'YYYY',convert(char(4),@dDate,112)) set @sFormat = replace(@sFormat,'YY',convert(char(2),@dDate,12)) --Insert Hours and minutesset @sFormat = replace(@sFormat,'HH:NN:SS',convert(char(8),@dDate,108)) set @sFormat = replace(@sFormat,'HH:NN',convert(char(5),@dDate,114)) -- Return the function's value -- return @sFormat endRegards, Paul |
 |
|
|
harshal_in
Aged Yak Warrior
633 Posts |
Posted - 2004-03-04 : 02:07:28
|
quote: Originally posted by verybrightstar hi all , does SQL server able to input the date that is originally from 2/6/2004 11:07:46 AM to DD-MM-YY 06-02-04 and store in the DB. For example , this is the SQL insert queryINSERT INTO ADMIN ( ID , NAME , DATE ) VALUES ( 'A001' , 'karen' ,'2/6/2004 11:07:46 AM')the date will automatically converted to 06-02-04 and store in DB .Does it need to use SQL Rule or user-defined data type.kt
INSERT INTO ADMIN ( ID , NAME , DATE ) VALUES ( 'A001' , 'karen' ,convert(varchar,'2/6/2004 11:07:46 AM',105))He is a fool for five minutes who asks , but who does not ask remains a fool for life! |
 |
|
|
|
|
|