SQL - 将Datepart转换为tinyint

时间:2015-09-30 21:05:58

标签: sql sql-server

WHERE t_stamp >= '2015-09-28' AND t_stamp < ADDDATE('2015-09-28', INTERVAL 1 DAY)

我需要帮助将Declare @Date date = getdate() select DATEpart(WEEKDAY,@Date)as day32 SELECT CAST(@Date AS tinyint) If @Date !=7 and @Date!=1 数据类型转换为date

这是我尝试执行时收到的错误消息

  

不允许从数据类型日期到int的显式转换。

3 个答案:

答案 0 :(得分:0)

尝试使用

SELECT DATEPART(WEEKDAY,GETDATE())

但请注意您的系统文化的影响。有些国家星期一在星期天开始他们的一周...

您应该阅读一下DATEFIRST-option

SELECT @@DATEFIRST --Your current setting

答案 1 :(得分:0)

应该是这样的:

<script>

    // function that hides/shows field_four based upon field_three value
    function check_field_value(new_val) {
        if(new_val != 'A value') {
            // #id_field_four should be actually the id of the HTML element
            // that surrounds everything you want to hide.  Since you did
            // not post your HTML I can't finish this part for you.
            $('#field_four').removeClass('hidden');
        } else {
            $('#field_four').addClass('hidden');
        }
    }



    // this is executed once when the page loads
    $(document).ready(function() {

        // set things up so my function will be called when field_three changes
        $('#field_three').change( function() {
            check_field_value(this.value);
        });

    });

</script>
<form action= "/send_email/" method="post" class='col-sm-5'>
    {% csrf_token %}
    {% bootstrap_field form.field_one%}
    {% bootstrap_field form.field_two%}
    <div id="field_three">{% bootstrap_field form.field_three%}</div>
    <div id="field_four">{% bootstrap_field form.additional_notes %}</div>
    {% buttons %}
        <button type="submit" class="btn btn-primary">
            {% bootstrap_icon "glyphicon glyphicon-ok-circle" %} Submit
        </button>
    {% endbuttons %}
</form>

DATE无法转换为INT,因此您有错误。

答案 2 :(得分:0)

您没有对CAST()的结果使用DATEPART(),您对数据类型为CAST()的变量使用DATE ,您可以CAST( AS TINYINT)DATEPART()

位于同一行
Declare @Date date = getdate()  
select CAST(DATEpart(WEEKDAY,@Date)AS TINYINT) as day32