我在我的數據庫,它表示在這樣的工作日字符串:查找字符串的任何部分在另一個字符串處理SQL
1234567 (all days of the week)
1230567 (all days but Thursday, EU standard - day 1 is Monday)
0000067 (no day apart from Saturday and Sunday)
,我需要寫一個SQL疑問,檢查重疊。 例如:
1234500 and 0000067 are NOT overlapping.
while 1234500 and 0030000 are overlapping (the 3).
and 1234500 and 0000567 IS overlapping (the 5).
每個條目都有一個ID,客戶號碼,並且該平日表示。
我的想法是這樣的:
SELECT
*
FROM dbo.Customers c
JOIN dbo.Customers c2 ON c.CustomerNumber = c2.CustomerNumber
AND c.Days <> c2.Days
WHERE 1 = 1
AND ...?
要獲得兩個條目是相同的客戶,但是當我來到WHERE語句我打了一個空白。在兩個Days字段中查找子字符串(例如3)是非常容易的,但是當7個條目中的任何一個可以重疊並且我必須排除0(不活躍的一天)時,我會感到困惑。
我需要一些幫助。
也可能有多個重疊,我只需要確定它是否重疊,我其實不需要知道多少天或哪幾天。 –
*嘆息*按下輸入太快:1234560和0234567理論上可能存在於數據庫中。 –
跳躍想法的不好主意:(1)將所有非零字符更改爲1,將二進制數字字符串轉換爲整數,然後['&'](https://docs.microsoft.com/en -us/sql/t-sql/language-elements/bitwise-and-transact-sql)(按位與)整數來檢測重疊。 (2)將字符串從數字推到XML和...。 (如果持續計算的列與星期的位圖的整數值一起保存,那麼二進制方法可能不會那麼糟糕。) – HABO