How to set a DateTime variable in SQL Server 2008?

前端 未结 10 1851
暗喜
暗喜 2020-12-30 20:15

SQL Server 2008 is not doing what I expected with DateTime. It doesn\'t let me set DateTime variables, no matter what date format I use.

Wh

相关标签:
10条回答
  • 2020-12-30 20:50
     1. I create new Date() and convert her in String .
     2. This string I set in insert.
    
     **Example:**  insert into newDate(date_create) VALUES (?)";
    
     ...
     PreparedStatement ps = con.prepareStatement(CREATE))
            ps.setString(1, getData());
            ps.executeUpdate();
      ...}
    
       private String getData() {
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-dd hh:mm:ss");
         return  sdf.format(new java.util.Date());
        }
    
     **It is very important format** = "yyyy-M-dd hh:mm:ss"
    
    0 讨论(0)
  • 2020-12-30 20:51

    Just to explain:

    2011-02-15 is being interpreted literally as a mathematical operation, to which the answer is 1994.

    This, then, is being interpreted as 1994 days since the origin of date (Jan 1st 1900).

    1994 days = 5 years, 6 months, 18 days = June 18th 1905

    So, if you don't want to to the calculation each time you want compare a date to a particular value use the standard: Compare the value of the toString() function of date object to the string like this :

    set @TEST  ='2011-02-05'
    
    0 讨论(0)
  • 2020-12-30 20:52

    The CONVERT function helps.Check this:

    declare @erro_event_timestamp as Timestamp;
    set @erro_event_timestamp = CONVERT(Timestamp,  '2020-07-06 05:19:44.380',  121);
    

    The magic number 121 I found here: https://www.w3schools.com/SQL/func_sqlserver_convert.asp

    0 讨论(0)
  • 2020-12-30 20:52

    You want to make the format/style explicit and don't rely on interpretation based on local settings (which may vary among your clients infrastructure).

    DECLARE @Test AS DATETIME
    SET @Test = CONVERT(DATETIME, '2011-02-15 00:00:00', 120) -- yyyy-MM-dd hh:mm:ss
    SELECT @Test
    

    While there is a plethora of styles, you may want to remember few

    • 126 (ISO 8601): yyyy-MM-ddThh:mm:ss(.mmm)
    • 120: yyyy-MM-dd hh:mm:ss
    • 112: yyyyMMdd

    Note that the T in the ISO 8601 is actually the letter T and not a variable.

    0 讨论(0)
  • 2020-12-30 21:01

    Check This:

    DECLARE
        @_month TINYINT         = 5,
        @_year SMALLINT         = 2020,
        @date_ref DATETIME      = NULL 
    
    
        IF @_year IS NULL
            SET @date_ref = GETDATE() - 430
        ELSE
        BEGIN
            SELECT @date_ref = CAST ( CAST ( @_year AS VARCHAR (4)) 
                + 
                CASE 
                    WHEN @_month < 10 THEN '0' + CAST ( @_month AS VARCHAR(1)) 
                    ELSE CAST ( @_month AS VARCHAR(2)) 
                END 
                + 
                '01' AS DATETIME )
        END
    
    0 讨论(0)
  • 2020-12-30 21:04

    2011-01-15 = 2011-16 = 1995. This is then being implicitly converted from an integer to a date, giving you the 1995th day, starting from 1st Jan 1900.

    You need to use SET @test = '2011-02-15'

    0 讨论(0)
提交回复
热议问题