Sql Query එක හදාගන්න උදව්වක්

rathnapure

Well-known member
  • Sep 11, 2012
    521
    270
    63
    SELECT [petty_cashes].[effective_date] FROM [account_codes]
    INNER JOIN [petty_cashes] ON [petty_cashes].[id] = [account_codes].[petty_cash_id] AND petty_cashes.is_disabled = 0 WHERE
    [account_codes].[is_disabled] = 0

    AND CONVERT(VARCHAR(7), dateadd(s, petty_cashes.effective_date, '1970/01/01'), 120) = '2023-05'

    ඔන්න ඔහොම Query තියෙයි.මේකේ petty_cashes.effective_date කියන column එකේ ඩේට තියෙන්නේ Timestamp විදියට.
    ඔක රන් කලාම එන්නේ පහල විදියට රිසාල්ට් එක

    Effective_Date No Column
    1685385000 2023-05
    1685559800 2023-05

    හරියට බැලුවොත් මේ කුවරිය රන් වෙද්දි මේ යට රෙකෝඩ් එක (1685559800 2023-05 )එන්ඩ බෑ.. ඒත් එනවා.. පේන විදියට කියන වැලිව් එක GMT

    Timestamp 1685559800​

    Assuming that this timestamp is in seconds:
    GMT: Wednesday, May 31, 2023 7:03:20 PM
    Your time zone: Thursday, June 1, 2023 12:33:20 AM GMT+05:30

    වලින් එද්දි වැරදි අගයක් එන්නේ .. මේක හදාගන්න උදව්වක් දෙන්ඩකෝ
     

    හෙළයෙක්

    Well-known member
  • Apr 26, 2014
    49,261
    100,040
    113
    kawda machan bard
    Angry Aubrey Plaza GIF by Parks and Recreation
     
    • Haha
    Reactions: Error365!

    Devendora_San

    Well-known member
  • Nov 7, 2016
    494
    979
    93
    The Jasmine Dragon Tea Shop
    The issue seems to be that the CONVERT function is translating the effective_date timestamp into a `VARCHAR` that only matches the year and month, not taking into account the specific date range of May 2023.

    To ensure that the effective_date only includes dates within May 2023, you should compare the date part only, after converting the Unix timestamp to an actual datetime. In SQL Server, you could use the `DATEFROMPARTS` function to compare the year and month without converting to a `VARCHAR`

    Try

    SELECT
    [petty_cashes].[effective_date]
    FROM
    [account_codes]
    INNER JOIN
    [petty_cashes] ON [petty_cashes].[id] = [account_codes].[petty_cash_id]
    AND [petty_cashes].[is_disabled] = 0
    WHERE
    [account_codes].[is_disabled] = 0
    AND YEAR(DATEADD(SECOND, [petty_cashes].[effective_date], '19700101')) = 2023
    AND MONTH(DATEADD(SECOND, [petty_cashes].[effective_date], '19700101')) = 5
     

    rathnapure

    Well-known member
  • Sep 11, 2012
    521
    270
    63
    The issue seems to be that the CONVERT function is translating the effective_date timestamp into a `VARCHAR` that only matches the year and month, not taking into account the specific date range of May 2023.

    To ensure that the effective_date only includes dates within May 2023, you should compare the date part only, after converting the Unix timestamp to an actual datetime. In SQL Server, you could use the `DATEFROMPARTS` function to compare the year and month without converting to a `VARCHAR`

    Try

    SELECT
    [petty_cashes].[effective_date]
    FROM
    [account_codes]
    INNER JOIN
    [petty_cashes] ON [petty_cashes].[id] = [account_codes].[petty_cash_id]
    AND [petty_cashes].[is_disabled] = 0
    WHERE
    [account_codes].[is_disabled] = 0
    AND YEAR(DATEADD(SECOND, [petty_cashes].[effective_date], '19700101')) = 2023
    AND MONTH(DATEADD(SECOND, [petty_cashes].[effective_date], '19700101')) = 5
    mekath not woking machan.. same result
     
    • Sad
    Reactions: Devendora_San

    ruchakashri

    Well-known member
  • Jan 21, 2010
    401
    384
    63
    CONVERT(VARCHAR(7), dateadd(s, petty_cashes.effective_date, '1970/01/01') AT TIME ZONE 'Asia/Colombo'), 120)

    SELECT [petty_cashes].[effective_date] AT TIME ZONE 'UTC' AS EffectiveDate FROM [account_codes] INNER JOIN [petty_cashes] ON [petty_cashes].[id] = [account_codes].[petty_cash_id] AND petty_cashes.is_disabled = 0 WHERE [account_codes].[is_disabled] = 0 AND CONVERT(VARCHAR(7), dateadd(s, petty_cashes.effective_date, '1970/01/01')AT TIME ZONE 'Asia/Colombo'), 120) = '2023-05';
    ------ Post added on Feb 13, 2024 at 11:57 AM
     

    hasithayad

    Well-known member
  • Sep 28, 2011
    30,793
    1
    45,000
    113
    SELECT DATE_FORMAT(FROM_UNIXTIME([petty_cashes].[effective_date]), '%Y-%m-%d') FROM [account_codes]
    INNER JOIN [petty_cashes] ON [petty_cashes].[id] = [account_codes].[petty_cash_id] AND petty_cashes.is_disabled = 0 WHERE
    [account_codes].[is_disabled] = 0
     
    • Like
    Reactions: rathnapure

    rathnapure

    Well-known member
  • Sep 11, 2012
    521
    270
    63
    CONVERT(VARCHAR(7), dateadd(s, petty_cashes.effective_date, '1970/01/01') AT TIME ZONE 'Asia/Colombo'), 120)

    SELECT [petty_cashes].[effective_date] AT TIME ZONE 'UTC' AS EffectiveDate FROM [account_codes] INNER JOIN [petty_cashes] ON [petty_cashes].[id] = [account_codes].[petty_cash_id] AND petty_cashes.is_disabled = 0 WHERE [account_codes].[is_disabled] = 0 AND CONVERT(VARCHAR(7), dateadd(s, petty_cashes.effective_date, '1970/01/01')AT TIME ZONE 'Asia/Colombo'), 120) = '2023-05';
    ------ Post added on Feb 13, 2024 at 11:57 AM
    me wage ekak try kala.. IST dammahama wada.. but sql server 11 ta wada na.. sql server 14 ta witharayi wada

    SELECT DATE_FORMAT(FROM_UNIXTIME([petty_cashes].[effective_date]), '%Y-%m-%d') FROM [account_codes]
    INNER JOIN [petty_cashes] ON [petty_cashes].[id] = [account_codes].[petty_cash_id] AND petty_cashes.is_disabled = 0 WHERE
    [account_codes].[is_disabled] = 0
    petty_cashes].[effective_date] eka thiyenne timestamp format eken habayi
    ------ Post added on Feb 13, 2024 at 12:07 PM
     

    leet

    Member
    Feb 13, 2024
    1
    0
    1
    Below is the correct way to use the condition "DATE_FORMAT(convert_tz(from_unixtime(effective_date), '+00:00','+5:30'),'%Y-%m') = '2023-05'" in your case I'm just giving you an example below.

    SELECT effective_date, DATE_FORMAT(convert_tz(from_unixtime(effective_date), '+00:00','+5:30'),'%Y-%m') as dd FROM petty_cashes
    where DATE_FORMAT(convert_tz(from_unixtime(effective_date), '+00:00','+5:30'),'%Y-%m') = '2023-05';
     
    Last edited:

    ShalikaAshan

    Junior member
  • Dec 25, 2008
    40
    24
    8
    mo
    SELECT [petty_cashes].[effective_date] FROM [account_codes]
    INNER JOIN [petty_cashes] ON [petty_cashes].[id] = [account_codes].[petty_cash_id] AND petty_cashes.is_disabled = 0 WHERE
    [account_codes].[is_disabled] = 0

    AND CONVERT(VARCHAR(7), dateadd(s, petty_cashes.effective_date, '1970/01/01'), 120) = '2023-05'

    ඔන්න ඔහොම Query තියෙයි.මේකේ petty_cashes.effective_date කියන column එකේ ඩේට තියෙන්නේ Timestamp විදියට.
    ඔක රන් කලාම එන්නේ පහල විදියට රිසාල්ට් එක

    Effective_Date No Column
    1685385000 2023-05
    1685559800 2023-05

    හරියට බැලුවොත් මේ කුවරිය රන් වෙද්දි මේ යට රෙකෝඩ් එක (1685559800 2023-05 )එන්ඩ බෑ.. ඒත් එනවා.. පේන විදියට කියන වැලිව් එක GMT

    Timestamp 1685559800​

    Assuming that this timestamp is in seconds:
    GMT: Wednesday, May 31, 2023 7:03:20 PM
    Your time zone: Thursday, June 1, 2023 12:33:20 AM GMT+05:30

    වලින් එද්දි වැරදි අගයක් එන්නේ .. මේක හදාගන්න උදව්වක් දෙන්ඩකෝ

    Mokakda wenna ona? data base dump ekk dapan query eka liyala daanna
     

    EKGuest

    Well-known member
  • Nov 16, 2022
    3,206
    5,703
    113
    Effective_Date එක තියෙන්නේ GMT වලින්නම් ඔයාට ඕනේ ලංකාවේ වෙලාවනම් DateAdd Function එකට Effective_Date + 19800 කියලා දෙන්න. පහලින් තියෙන්නේ වෙනස් කරපු query එක.

    SQL:
    SELECT [petty_cashes].[effective_date] FROM [account_codes]
    INNER JOIN [petty_cashes] ON [petty_cashes].[id] = [account_codes].[petty_cash_id] AND petty_cashes.is_disabled = 0 WHERE
    [account_codes].[is_disabled] = 0
    
    AND CONVERT(VARCHAR(7), dateadd(s, petty_cashes.effective_date + 19800, '1970/01/01'), 120) = '2023-05'



    ඔය විදියට වෙනස් කලාම Effective_Date එකේ අගය 1685559800 වෙනකොට CONVERT ෆන්ක්ෂන් එකෙන් රිටර්න් කරන්නෙ 2023-06
     

    hasithayad

    Well-known member
  • Sep 28, 2011
    30,793
    1
    45,000
    113
    me wage ekak try kala.. IST dammahama wada.. but sql server 11 ta wada na.. sql server 14 ta witharayi wada


    petty_cashes].[effective_date] eka thiyenne timestamp format eken habayi
    ------ Post added on Feb 13, 2024 at 12:07 PM
    දැන් ඔයාට ඕනේ දවස හරියට ප්‍රින්ට් වෙන්න නේද? dateformat() එකෙන් ඒක තමා කරල තියෙන්නෙ. අර උඩ තියන convert() එකෙනුත් ඒකම කරන්න පුලුවන්.

    time zone එකට අදාලව වෙලාව හදාගන්න ඕනෙ නම් අදාල පැය ගාන තත්පර කරලා ඒක timestamp එකට එකතු කරලා ගන්න. format එකත් වෙලාව පේන විදියට හදාගන්න