从日期中减去一个月,但要确保它是该月的第一个工作日

时间:2019-09-30 16:00:35

标签: sql sql-server

我需要从称为ArchiveDate的日期中减去一个月,但我希望该月的第一个工作日。例如,如果我的ArchiveDate是9/2/2018,我想拥有8/1/2019。

这就是我所拥有的:

DECLARE @ArchiveDate date = '9/2/2019'

SELECT ArchiveDate = DATEADD(day, 
        CASE WHEN 
            DATEPART(weekday, DATEADD(MONTH, -1, @ArchiveDate)) = 1
            THEN 1
        WHEN DATEPART(weekday, DATEADD(MONTH, -1, @ArchiveDate)) = 7 THEN 2
        ELSE 0
        END
    , DATEADD(MONTH, -1, @ArchiveDate))

我从中得到的是8/2/2019,但是正如您所看到的,我想要的是8/1/2019。

3 个答案:

答案 0 :(得分:3)

SELECT 
    CASE 
        WHEN DATENAME(WEEKDAY, DATEADD(mm, DATEDIFF(mm, 0, @ArchiveDate) - 1, 0)) = 'Saturday'
            THEN DATEADD(mm, DATEDIFF(mm, 0, @ArchiveDate) - 1, 0) + 2
        WHEN DATENAME(WEEKDAY, DATEADD(mm, DATEDIFF(mm, 0, @ArchiveDate) - 1, 0)) = 'Sunday'
            THEN DATEADD(mm, DATEDIFF(mm, 0, @ArchiveDate) - 1, 0) + 1
        ELSE 
            DATEADD(mm, DATEDIFF(mm, 0, @ArchiveDate) - 1, 0)
    END

这将返回上个月的第一个工作日。

答案 1 :(得分:1)

您可以使用eomonth以及自己的逻辑来获取上个月的第一天,如下所示:

//capture config
- (void)initialize {
    AVCaptureDevice *frontCameraDevice;
    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for (AVCaptureDevice *device in devices) {
        if (device.position == AVCaptureDevicePositionFront) {
            frontCameraDevice = device;
        }
    }
    if (frontCameraDevice == nil) {
        NSLog(@"Front camera device not found");
        return;
    }

    _session = [[AVCaptureSession alloc] init];
    _session.sessionPreset = AVCaptureSessionPreset640x480;

    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:frontCameraDevice error: &error];
    if (error != nil) {
        NSLog(@"Error getting front camera device input: %@", error);
    }
    if ([_session canAddInput:input]) {
        [_session addInput:input];
    } else {
        NSLog(@"Could not add front camera device input to session");
    }

    AVCaptureVideoDataOutput *videoOutput = [[AVCaptureVideoDataOutput alloc] init];
    // This is the default, but making it explicit
    videoOutput.alwaysDiscardsLateVideoFrames = YES;

    if ([videoOutput.availableVideoCVPixelFormatTypes containsObject:
                      [NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange]]) {
        OSType format = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
        videoOutput.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedInt:format]
                                                                 forKey:(id)kCVPixelBufferPixelFormatTypeKey];
    } else {
        NSLog(@"YUV format not available");
    }

    [videoOutput setSampleBufferDelegate:self queue:dispatch_queue_create("extrapage.camera.capture.sample.buffer.delegate", DISPATCH_QUEUE_SERIAL)];
    if ([_session canAddOutput:videoOutput]) {
        [_session addOutput:videoOutput];
    } else {
        NSLog(@"Could not add video output to session");
    }

    AVCaptureConnection *captureConnection = [videoOutput connectionWithMediaType:AVMediaTypeVideo];
    captureConnection.videoOrientation = AVCaptureVideoOrientationPortrait;
}

//acquisition code 
- (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {

    if (_listener != nil) {
        CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
        OSType format = CVPixelBufferGetPixelFormatType(pixelBuffer);

        NSAssert(format == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange, @"Only YUV is supported");

        // The first plane / channel (at index 0) is the grayscale plane
        // See more infomation about the YUV format
        // http://en.wikipedia.org/wiki/YUV
        CVPixelBufferLockBaseAddress(pixelBuffer, 0);
        void *baseaddress = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);

        CGFloat width = CVPixelBufferGetWidth(pixelBuffer);
        CGFloat height = CVPixelBufferGetHeight(pixelBuffer);

        cv::Mat frame(height, width, CV_8UC1, baseaddress, 0);

        [_listener onNewFrame:frame];

        CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
    }
}

答案 2 :(得分:1)

您可以将EOMONTHDATEADD()一起使用:

SELECT DATEADD(DAY, 1, EOMONTH(@ArchiveDate, -2)) AS ArchiveDate