SQL中如何取得系统时间和日期(时间要求返回6位,比如152910;日期要求返回8位,比如20070517)
是不是有什么特定的函数?如果有,那取得系统时间和日期的函数返回类型是字符型吗?(要求是)

解决方案 »

  1.   

    SQL中CONVERT转化函数的用法CONVERT的使用方法:////////////////////////////////////////////////////////////////////////////////////////格式:
    CONVERT(data_type,expression[,style])说明:
    此样式一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,char,varchar)
    相互转换的时候才用到.例子:
    SELECT CONVERT(varchar(30),getdate(),102) now
    结果为
    now
    ---------------------------------------
    09/15/2001/////////////////////////////////////////////////////////////////////////////////////style数字在转换时间时的含义如下-------------------------------------------------------------------------------------------------
    Style(2位表示年份) | Style(4位表示年份) | 输入输出格式 
    -------------------------------------------------------------------------------------------------
    - | 0 or 100 | mon dd yyyy hh:miAM(或PM) 
    -------------------------------------------------------------------------------------------------
    1 | 101 | mm/dd/yy 
    -------------------------------------------------------------------------------------------------
    2 | 102 | yy-mm-dd 
    -------------------------------------------------------------------------------------------------
    3 | 103 | dd/mm/yy 
    -------------------------------------------------------------------------------------------------
    4 | 104 | dd-mm-yy 
    -------------------------------------------------------------------------------------------------
    5 | 105 | dd-mm-yy 
    -------------------------------------------------------------------------------------------------
    6 | 106 | dd mon yy 
    -------------------------------------------------------------------------------------------------
    7 | 107 | mon dd,yy 
    -------------------------------------------------------------------------------------------------
    8 | 108 | hh:mm:ss 
    -------------------------------------------------------------------------------------------------
    - | 9 or 109 | mon dd yyyy hh:mi:ss:mmmmAM(或PM)
    -------------------------------------------------------------------------------------------------
    10 | 110 | mm-dd-yy 
    -------------------------------------------------------------------------------------------------
    11 | 111 | yy/mm/dd 
    -------------------------------------------------------------------------------------------------
    12 | 112 | yymmdd 
    -------------------------------------------------------------------------------------------------
    - | 13 or 113 | dd mon yyyy hh:mi:ss:mmm(24小时制) 
    -------------------------------------------------------------------------------------------------
    14 | 114 | hh:mi:ss:mmm(24小时制) 
    -------------------------------------------------------------------------------------------------
    - | 20 or 120 | yyyy-mm-dd hh:mi:ss(24小时制) 
    -------------------------------------------------------------------------------------------------
    - | 21 or 121 | yyyy-mm-dd hh:mi:ss:mmm(24小时制) 
    -------------------------------------------------------------------------------------------------
      

  2.   

    SELECT replace(CONVERT(varchar(30),getdate(),102),'.','') now
    SELECT replace(CONVERT(varchar(30),getdate(),108),':','') now
      

  3.   

    select replace(replace(replace(convert(char(19),getdate(),120),'-',''),':',''),' ','')
    /*
    ------------------------
    20070517154032
    (所影响的行数为 1 行)
    */getdate()返回是datetime型,需用convert函数转为字符型
      

  4.   

    如果时间是15:26:12,
    则时间要求返回6位的样式,比如:152612,有什么办法吗?各位大哥能帮我写一个例子吗?
    SELECT CONVERT(varchar(30),getdate(),102)得到的日期是不是一个总共30位的变量?
    如果要得到的是8位,是不是这样写的:
    select convert(varchar(8),getdate(),112)不胜感激!
      

  5.   

    select replace(convert(varchar(8),getdate(),108),':','')
      

  6.   

    select replace(convert(varchar(8),getdate(),108),':','')
    我能不能把这个得到的值赋给一个6位的字符变量呢?谢谢!如果要把得到的时间值赋给一个6位的又该怎么做呢?
    declare 
    @systime varchar(6)select @systime=replace(convert(varchar(6),getdate(),108),':','')能这样写吗?
      

  7.   

    IF EXISTS(SELECT NAME FROM sysobjects
            WHERE NAME = N'funGetNewDate' )
    DROP FUNCTION funGetNewDate
    GO
    CREATE FUNCTION funGetNewDate
       (
    @Date DATETIME
       )
    RETURNS NVARCHAR(10)
    AS
    BEGIN
    DECLARE @Year  INT
    DECLARE @Month INT
    DECLARE @Day INT
    DECLARE @NewDate NVARCHAR(10)
    SET @Year = DATEPART( yyyy,@Date )
    SET @Month = DATEPART( mm,@Date )
    SET @Day = DATEPART( dd,@Date )
    SELECT @NewDate = ( STR( @Year,4,0 )
    + N'-'
    + ( SELECT
    CASE
    WHEN @Month >= 10 THEN STR( @Month, 2, 0 )
    WHEN @Month <10   THEN '0' + STR( @Month, 1, 0)
    END

    + N'-'
    + ( SELECT
    CASE
    WHEN @Day >= 10 THEN STR( @Day, 2, 0 )
    WHEN @Day <10   THEN '0' + STR( @Day, 1, 0)
    END 
    )
    )
    RETURN @NewDate
    END
    GO
    select dbo.funGetNewDate(getdate()) as 当前日期
    参考下
      

  8.   

    declare 
    @systime varchar(6)select @systime=replace(convert(varchar(6),getdate(),108),':','')
    有点写错了,应该是:
    select @systime=replace(convert(varchar(8),getdate(),108),':','')
      

  9.   

    create table name
    (
    id   nvarchar2(10) primary key,
    card nvarchar2(10),
    dat  date default ******,
    sig  nvarchar2(2),
    check(sig='y' or sig='n')
    );如何将dat 的默认值取系统时间呢?**** 出代码该如何写?