使用LEFT查询不起作用

时间:2017-06-13 20:54:58

标签: sql database

我的2017-6-14 AM 12:00:00列中有此值:2017-6-14

我想只选择日期(LEFT ())部分,因此我在查询中使用了Select a.Name, b.Restaurant, a.City, c.[Postal Code] as 'State', LEFT(a.[Open Date],CHAIRINDEX(' ',a.[Open Date]-1)), a.CO as 'T Date', (SELECT Count(Taskid) FROM Tasks WHERE Complete = 'True' AND SID = a.ID GROUP BY SID) AS 'Tasks Complete', (SELECT COUNT(Complete) FROM Tasks WHERE Complete = 'false' AND T_Date <= cast(cast(getdate() as date) as datetime) AND SID = a.ID GROUP BY SID) AS 'Tasks Overdue' FROM Stores a JOIN Concepts b ON a.ConceptID = b.R_id JOIN States c ON a.State = c.Stateid WHERE a.[Open Date] >= cast(cast(getdate() as date) as datetime) ORDER BY a.[Open Date] ASC 。不幸的是,展示的不仅仅是2017-6-14&#39;在我的结果中。

这是我的查询。

public class Tab1History extends Fragment implements SwipeRefreshLayout.OnRefreshListener
{

SwipeRefreshLayout mSwipeRefreshLayout;
private RecyclerView recyclerView;
private CespiteAdapter adapter;
UserSessionManager session;
ConnectionDetector cd;

private static final String URL_DATA = "http://myfile.php";

private List<CespiteOgg> cespiteOggList;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    cd = new ConnectionDetector(getActivity());
    View rootView = inflater.inflate(R.layout.tab1history, container, false);

    recyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
    recyclerView.setHasFixedSize(true);//every item of the RecyclerView has a fix size
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    cespiteOggList = new ArrayList<>();

    // SwipeRefreshLayout
    mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container);
    mSwipeRefreshLayout.setOnRefreshListener(this);
    mSwipeRefreshLayout.setColorSchemeResources(
            R.color.colorAccent
            );

    /**
     * Showing Swipe Refresh animation on activity create
     * As animation won't start on onCreate, post runnable is used
     */
    mSwipeRefreshLayout.post(new Runnable() {

        @Override
        public void run() {

            mSwipeRefreshLayout.setRefreshing(true);

            // Fetching data from server
            loadRecyclerViewData();
        }
    });

    return rootView;
}
/**
 * This method is called when swipe refresh is pulled down
 */
@Override
public void onRefresh()
{
    if(cd.isNetworkAvailable())
    {
        // Fetching data from server
        loadRecyclerViewData();
    }
    else
    {
        Toast.makeText(getActivity(), "Nessuna connessione ad internet!", Toast.LENGTH_SHORT).show();
        mSwipeRefreshLayout.setRefreshing(false);
    }

}

private void loadRecyclerViewData()
{

    cespiteOggList.clear();
    //cespiteOggList = new ArrayList<>();
    // Showing refresh animation before making http call
    mSwipeRefreshLayout.setRefreshing(true);

    // Session class instance
    session = new UserSessionManager(getActivity());
    //get user data from session
    HashMap<String, String> user = session.getUserDetails();
    //get name
    String name = user.get(UserSessionManager.KEY_NAME);
    // get username
    final String usernameUtente = user.get(UserSessionManager.KEY_USERNAME);


    StringRequest stringRequest = new StringRequest(Request.Method.POST,
            URL_DATA,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {

                    try {
                        JSONObject jsonObject = new JSONObject(s);
                        JSONArray array = jsonObject.getJSONArray("dates");

                        for(int i=0; i<array.length(); i++)
                        {
                            JSONObject o = array.getJSONObject(i);
                            CespiteOgg item = new CespiteOgg(
                                    o.getString("CodNumInventario"),
                                    o.getString("Nome"),
                                    o.getString("DtCatalogazione"),
                                    o.getString("CodIdA"),
                                    o.getString("username")
                            );
                            cespiteOggList.add(item);
                        }

                        adapter = new CespiteAdapter(cespiteOggList, getActivity());
                        recyclerView.setAdapter(adapter);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    // Stopping swipe refresh
                    mSwipeRefreshLayout.setRefreshing(false);

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    // Stopping swipe refresh
                    mSwipeRefreshLayout.setRefreshing(false);

                }
            })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Username", usernameUtente);
            return params;
        }
    };

    RegisterRequest.getmInstance(getActivity()).addToRequestque(stringRequest);


}


}

1 个答案:

答案 0 :(得分:0)

如果[Open Data]是varchar类型,那么左边的效果会很好但是如果它是datime那么你只能通过转换来提取日期部分,例如:

SELECT CONVERT(date, [Open Date])

注意:在任何情况下,如果[打开日期]是varchar,那么-1应该在第一个括号之后:

LEFT(a.[Open Date],CHAIRINDEX(' ',a.[Open Date])-1)