| |

VerySource

 Forgot password?
 Register
Search
View: 1610|Reply: 15

Small question, how to get the number of days from a certain date in SQL?

[Copy link]

2

Threads

7

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-2-8 07:30:01
| Show all posts |Read mode
There is a birthday date A (2000-02-01), how do I get the number of days before this birthday date (February-01)?
Reply

Use magic Report

0

Threads

52

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-4-1 19:45:01
| Show all posts
For example, use the datediff function in SQL Server.
Reply

Use magic Report

2

Threads

7

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-4-2 09:15:01
| Show all posts
This will not work.
Reply

Use magic Report

0

Threads

119

Posts

67.00

Credits

Newbie

Rank: 1

Credits
67.00

 China

Post time: 2020-4-2 11:00:02
| Show all posts
select datediff (day, '2017-01-03', getdate ())
Reply

Use magic Report

0

Threads

26

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-4-2 15:00:01
| Show all posts
SQL Server
select DATEDIFF (day, convert (datetime, '20170101', 112), getdate ())

Oracle
select to_date (to_char (sysdate, 'yyyy-mm-dd'), 'yyyy-mm-dd')-to_date ('2017-01-01', 'yyyy-mm-dd') from dual
Reply

Use magic Report

0

Threads

52

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-4-2 18:00:02
| Show all posts
SELECT DAY (BirthDate)-DAY (GETDATE ()) AS Expr1
FROM Employees
Reply

Use magic Report

2

Threads

7

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-4-3 02:30:01
| Show all posts
thank you very much!
Reply

Use magic Report

0

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-4-3 15:00:01
| Show all posts
This question is not so simple.
For example, if the birthday is June 1.
If the current date is May 1, 2017, then it should be 30 days.
If the current date is June 2, 2017, then it should be 364 days. Instead of 1 day.
Reply

Use magic Report

0

Threads

26

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-4-3 20:15:01
| Show all posts
To process only the month, it is OK to add a string to the current year.
select DATEDIFF (day, convert (datetime, substring (convert (varchar, getdate (), 112), 1,4) + substring ('20000101', 5, len ('20000101')), 112), getdate ())
Reply

Use magic Report

1

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-4-8 09:15:01
| Show all posts
Then convert it to a string? Substring is only available for strings
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list