Datediff ua dab tsi hauv SQL?
Datediff ua dab tsi hauv SQL?

Video: Datediff ua dab tsi hauv SQL?

Video: Datediff ua dab tsi hauv SQL?
Video: MySQL : MySQL CONVERT_TZ() 2024, Kaum ib hlis
Anonim

Hauv SQL ntawv Server, koj tuaj yeem siv T- SQL DATEDIFF () ua haujlwm kom rov qab qhov sib txawv ntawm ob hnub / sijhawm. Nws ua haujlwm ntawm txhua qhov kev qhia uas tuaj yeem daws tau rau lub sijhawm, hnub tim, lub sijhawm me me, hnub tim, hnub tim 2, lossis hnub tim offset tus nqi.

Tsis tas li ntawd, datediff ua haujlwm li cas hauv SQL?

Cov DATEDIFF () muaj nuj nqi xa rov qab tus nqi ntawm tus lej qhia qhov sib txawv ntawm start_date thiab end_date, nrog rau chav tsev teev los ntawm date_part. Cov DATEDIFF () kev ua haujlwm rov qab ua yuam kev yog tias qhov tshwm sim yog tawm ntawm ntau yam rau integer (-2, 147, 483, 648 txog +2, 147, 483, 647).

Tsis tas li ntawd, koj puas tuaj yeem rho tawm hnub hauv SQL? Yuav ua li cas rho tawm hnub hauv SQL Server – Querychat. SQL ntawv Server tsis txhawb tus neeg teb xov tooj rho tawm tab sis muaj cov npe ntev ntawm cov haujlwm uas tso cai rau peb ua haujlwm nrog hnub hom teb xws li DATEADD, DATEDIFF, DATENAME, DATEPART, HNUB, GETDATE, MONTH, YEAR, thiab lwm yam.

Yog li ntawd, koj siv datediff li cas?

Txhawm rau xam cov hnub ntawm hnub tim 1 thiab hnub tim 2, koj tuaj yeem ua tau siv Hnub ntawm lub xyoo ("y") lossis Hnub ("d"). Thaum lub sij hawm yog hnub Monday ("w"), Hnub timDiff rov qab tus naj npawb ntawm lub lis piam ntawm ob hnub. Yog tias hnub tim 1 poob rau hnub Monday, Hnub timDiff suav cov hnub Monday txog hnub tim 2.

Kuv tuaj yeem tau txais hnub ntawm ob hnub hauv SQL Server li cas?

PRINT DATEDIFF(HNUB, '1/1/2011', '3/1/2011') yuav muab koj li cas tom qab. Qhov no muab pes tsawg zaus ntawm ib tag hmo ciam teb hla nruab nrab tus ob hnub . Koj tuaj yeem txiav txim siab xav tau ntxiv ib qho rau qhov no yog tias koj suav nrog ob qho tib si hnub tim hauv suav - lossis rho tawm ib qho yog tias koj tsis xav suav nrog hnub.

Pom zoo: