Cov txheej txheem:

Yuav ua li cas hloov ib hnub rau ib txoj hlua hauv SQL?
Yuav ua li cas hloov ib hnub rau ib txoj hlua hauv SQL?

Video: Yuav ua li cas hloov ib hnub rau ib txoj hlua hauv SQL?

Video: Yuav ua li cas hloov ib hnub rau ib txoj hlua hauv SQL?
Video: Yuav ua li cas thiaj khiav dim lub ntiajteb no 2024, Kaum ib hlis
Anonim

Yuav ua li cas kom tau txais ntau hom SQL Server hnub tim

  1. Siv cov hnub hom kev xaiv nrog Hloov pauv muaj nuj nqi.
  2. Kom tau YYYY-MM-DD siv SELECT Hloov pauv (varchar, getdate(), 23)
  3. Kom tau MM/DD/YYYY siv SELECT Hloov pauv (varchar, getdate(), 1)
  4. Tshawb xyuas daim ntawv qhia kom tau txais cov npe ntawm txhua hom kev xaiv.

Yog li ntawd, yuav ua li cas kuv hloov hnub rau ib txoj hlua hauv Java?

Cia peb pom cov lej yooj yim los hloov Hnub rau Txoj hlua hauv java

  1. Hnub tim = Calendar.getInstance().getTime();
  2. DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
  3. String strDate = dateFormat.format(date);

Tsis tas li ntawd, yuav ua li cas hloov hnub tim rau DD MMM YYYY hauv SQL? Yuav ua li cas format SQL Server hnub nrog FORMAT muaj nuj nqi

  1. Siv FORMAT muaj nuj nqi los format hnub thiab sijhawm.
  2. Kom tau DD/MM/YYYY siv SELECT FORMAT (getdate(), 'dd/MM/yyyy ') raws li hnub tim.
  3. Kom tau MM-DD-YY siv SELECT FORMAT (getdate(), 'MM-dd-yy') raws li hnub tim.
  4. Tshawb xyuas cov piv txwv ntxiv hauv qab no.

Kuj kom paub yog, peb puas tuaj yeem hloov varchar rau hnub tim hauv SQL?

Cov hloov dua siab tshiab ntawm a varchar hom ntaub ntawv rau a hnub tim hom ntaub ntawv ua rau muaj qhov tsis muaj nuj nqis. Koj xav tau cov separators rau cov hnub zoo li "/", "." los yog "-". Peb siv substring los concatenate lub "-" siv tau hnub format thiab ces peb siv cov Hloov pauv ua haujlwm rau hloov cov cim rau sql hnub.

Hnub tim hom ntawv hauv SQL yog dab tsi?

SQL Server los nrog cov ntaub ntawv hauv qab no rau kev khaws cia hnub lossis hnub / sijhawm tus nqi hauv cov ntaub ntawv: DATE - hom ntawv YYYY-MM-DD . DATETIME - hom ntawv: YYYY-MM-DD HH:MI:SS . TSEEM CEEB - hom ntawv: YYYY-MM-DD HH:MI:SS.

Pom zoo: